EncodingConverterTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <fstream>
  2. #include <boost/test/unit_test.hpp>
  3. #include "../EncodingConverter.h"
  4. using namespace vconnect;
  5. using namespace std;
  6. BOOST_AUTO_TEST_SUITE(EncodingConverterTest)
  7. /**
  8. * 指定されたパスに置かれたファイルを文字列に読み込む
  9. * @param path 読み込むファイルのパス
  10. * @return 読み込んだデータ
  11. */
  12. static string getFixture( string path )
  13. {
  14. ifstream stream(path.c_str(), std::ios::binary);
  15. string result;
  16. while (!stream.eof()) {
  17. int c = stream.get();
  18. if (c == EOF) {
  19. break;
  20. }
  21. result.append(1, (char)(0xFF & c));
  22. }
  23. return result;
  24. }
  25. struct ExtendedEncodingConverter : public EncodingConverter
  26. {
  27. static string getCodeset(string locale)
  28. {
  29. return EncodingConverter::getCodeset(locale);
  30. }
  31. };
  32. BOOST_AUTO_TEST_CASE(testConvert)
  33. {
  34. string const encoding = EncodingConverter::getInternalEncoding();
  35. {
  36. EncodingConverter shiftJISConverter( "Shift_JIS", encoding );
  37. string fixture = getFixture("fixture/EncodingConverter/shift_jis.txt");
  38. string actual = shiftJISConverter.convert(fixture);
  39. string expected = "だ・い・じ・け・ん";
  40. BOOST_CHECK_EQUAL( expected, actual );
  41. }
  42. {
  43. EncodingConverter utf16leConverter( "UTF-16LE", encoding );
  44. string fixture = getFixture( "fixture/EncodingConverter/utf16le.txt" );
  45. string actual = utf16leConverter.convert(fixture);
  46. string expected = "社会復帰できなくなっちゃうよ";
  47. BOOST_CHECK_EQUAL( expected, actual );
  48. }
  49. // Skip this test in windows, because "骶" can't be encoded in Shift_JIS.
  50. if (encoding != "CP932") {
  51. EncodingConverter utf32leConverter( "UTF-32LE", encoding );
  52. string fixture = getFixture("fixture/EncodingConverter/utf32le.txt");
  53. string actual = utf32leConverter.convert(fixture);
  54. string expected = "尾骶骨";
  55. BOOST_CHECK_EQUAL( expected, actual );
  56. }
  57. }
  58. BOOST_AUTO_TEST_CASE(testGetCodeset)
  59. {
  60. string expected = "codeset";
  61. string actual = ExtendedEncodingConverter::getCodeset( "language_territory.codeset@modifier" );
  62. BOOST_CHECK_EQUAL( expected, actual );
  63. actual = ExtendedEncodingConverter::getCodeset( "language_territory.codeset" );
  64. BOOST_CHECK_EQUAL( expected, actual );
  65. expected = "";
  66. actual = ExtendedEncodingConverter::getCodeset( "language_territory" );
  67. BOOST_CHECK_EQUAL( expected, actual );
  68. }
  69. BOOST_AUTO_TEST_SUITE_END()