OSDN Git Service

tests初コミット
[yurina/yurina.git] / tests / core / base / EncodingTest.cpp
diff --git a/tests/core/base/EncodingTest.cpp b/tests/core/base/EncodingTest.cpp
new file mode 100755 (executable)
index 0000000..bd6cb68
--- /dev/null
@@ -0,0 +1,130 @@
+/*\r
+ *     “yurina”: yurina unpretentious renderer is not an anathema\r
+ *\r
+ *     This software is distributed under a BSD-style license.\r
+ *     See license.txt for more information.\r
+ */\r
+\r
+#include <cppunit/extensions/HelperMacros.h>\r
+#include "base/Encoding.h"\r
+\r
+using namespace yurina;\r
+\r
+class EncodingTest : public CPPUNIT_NS::TestFixture\r
+{\r
+       CPPUNIT_TEST_SUITE( EncodingTest );\r
+       CPPUNIT_TEST( testDetectDefaultEncoding );\r
+       CPPUNIT_TEST( testCountBytes );\r
+       CPPUNIT_TEST( testConvert );\r
+       CPPUNIT_TEST_SUITE_END();\r
+\r
+public:\r
+       void setUp();\r
+       void tearDown();\r
+\r
+protected:\r
+       void testDetectDefaultEncoding();\r
+       void testCountBytes();\r
+       void testConvert();\r
+private:\r
+       static const unsigned char utf8[64];\r
+       static const unsigned char utf16le[64];\r
+       static const unsigned char utf16be[64];\r
+       static const unsigned char utf32le[64];\r
+       static const unsigned char utf32be[64];\r
+};\r
+\r
+CPPUNIT_TEST_SUITE_REGISTRATION( EncodingTest );\r
+\r
+// L"Alpha漢字\n0123\0!"\r
+const unsigned char EncodingTest::utf8[] = {\r
+       0x41, 0x6C, 0x70, 0x68, 0x61, 0xE6, 0xBC, 0xA2,\r
+       0xE5, 0xAD, 0x97, 0x0A, 0x30, 0x31, 0x32, 0x33,\r
+       0x00, 0x21\r
+};\r
+const unsigned char EncodingTest::utf16le[] = {\r
+       0x41, 0x00, 0x6C, 0x00, 0x70, 0x00, 0x68, 0x00, \r
+       0x61, 0x00, 0x22, 0x6F, 0x57, 0x5B, 0x0A, 0x00, \r
+       0x30, 0x00, 0x31, 0x00, 0x32, 0x00, 0x33, 0x00,\r
+       0x00, 0x00, 0x21, 0x00\r
+};\r
+const unsigned char EncodingTest::utf16be[] = {\r
+       0x00, 0x41, 0x00, 0x6C, 0x00, 0x70, 0x00, 0x68,\r
+       0x00, 0x61, 0x6F, 0x22, 0x5B, 0x57, 0x00, 0x0A, \r
+       0x00, 0x30, 0x00, 0x31, 0x00, 0x32, 0x00, 0x33,\r
+       0x00, 0x00, 0x00, 0x21\r
+};\r
+\r
+const unsigned char EncodingTest::utf32le[] = {\r
+       0x41, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, \r
+       0x70, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x00, \r
+       0x61, 0x00, 0x00, 0x00, 0x22, 0x6F, 0x00, 0x00, \r
+       0x57, 0x5B, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, \r
+       0x30, 0x00, 0x00, 0x00, 0x31, 0x00, 0x00, 0x00, \r
+       0x32, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,\r
+       0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00\r
+};\r
+\r
+const unsigned char EncodingTest::utf32be[] = {\r
+       0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, 0x6C,\r
+       0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x68,\r
+       0x00, 0x00, 0x00, 0x61, 0x00, 0x00, 0x6F, 0x22,\r
+       0x00, 0x00, 0x5B, 0x57, 0x00, 0x00, 0x00, 0x0A, \r
+       0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x31,\r
+       0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x33,\r
+       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21\r
+};\r
+\r
+\r
+void EncodingTest::setUp()\r
+{\r
+\r
+}\r
+\r
+void EncodingTest::tearDown()\r
+{\r
+\r
+}\r
+\r
+\r
+void EncodingTest::testDetectDefaultEncoding()\r
+{\r
+       // コンパイラのwchar_tの内部表現の判定が出来るか\r
+       // 尤も未知であればUNKNOWNを返すのは正しい\r
+       Encoding encoding = Encoding::detectDefaultEncoding();\r
+       CPPUNIT_ASSERT( Encoding::UNKNOWN != encoding.getType() );\r
+}\r
+\r
+void EncodingTest::testCountBytes()\r
+{\r
+\r
+       Encoding encUtf8( Encoding::UTF8 );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-8", size_t( 16 ), encUtf8.countBytes( utf8 ) );\r
+\r
+       Encoding encUtf16le( Encoding::UTF16LE );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-16LE", size_t( 24 ), encUtf16le.countBytes( utf16le ) );\r
+\r
+       Encoding encUtf16be( Encoding::UTF16BE );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-16BE", size_t( 24 ), encUtf16be.countBytes( utf16be ) );\r
+\r
+       Encoding encUtf32le( Encoding::UTF32LE );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-32LE", size_t( 48 ), encUtf32le.countBytes( utf32le ) );\r
+\r
+       Encoding encUtf32be( Encoding::UTF32BE );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-32BE", size_t( 48 ), encUtf32be.countBytes( utf32be ) );\r
+\r
+}\r
+void EncodingTest::testConvert()\r
+{\r
+       unsigned char dest[100];\r
+       size_t len;\r
+       len = Encoding::convert(Encoding::UTF8, Encoding::UTF8, utf8, dest, 18, sizeof(dest) );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-8 to UTF-8 (Length)", size_t( 18 ), len );\r
+\r
+       len = Encoding::convert(Encoding::UTF8, Encoding::UTF16LE, utf8, dest, 18, sizeof(dest) );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-8 to UTF-16LE (Length)", size_t( 28 ), len );\r
+\r
+       len = Encoding::convert(Encoding::UTF8, Encoding::UTF16BE, utf8, dest, 18, sizeof(dest) );\r
+       CPPUNIT_ASSERT_EQUAL_MESSAGE( "UTF-8 to UTF-16BE (Length)", size_t( 28 ), len );\r
+\r
+}\r