changeset 1088:6fd4434c1bcf

unit tests for encodings
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Aug 2014 19:24:28 +0200
parents e07b90fb00eb
children 5ea0b56e850d
files Resources/EncodingTests.h Resources/EncodingTests.py UnitTestsSources/FromDcmtkTests.cpp
diffstat 3 files changed, 94 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/EncodingTests.h	Mon Aug 04 19:24:28 2014 +0200
@@ -0,0 +1,40 @@
+static const unsigned int testEncodingsCount = 11;
+static const Orthanc::Encoding testEncodings[] = {
+  Orthanc::Encoding_Latin5,
+  Orthanc::Encoding_Hebrew,
+  Orthanc::Encoding_Greek,
+  Orthanc::Encoding_Arabic,
+  Orthanc::Encoding_Cyrillic,
+  Orthanc::Encoding_Latin4,
+  Orthanc::Encoding_Latin3,
+  Orthanc::Encoding_Latin2,
+  Orthanc::Encoding_Latin1,
+  Orthanc::Encoding_Utf8,
+  Orthanc::Encoding_Ascii
+};
+static const char *testEncodingsEncoded[11] = {
+  "\xe9\xe4\xf6\xf2\x3f\x3f\x3f\x3f\x3f\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\xe3\x3f\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\x3f\xc8\x3f\x3f\x3f\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\xd5\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\xb4\x3f\x3f\x3f\x3f\x3f\xfb",
+  "\xe9\xe4\xf6\x3f\x3f\x3f\x3f\x3f\x3f\xf3\x3f",
+  "\xe9\xe4\xf6\xf2\x3f\x3f\xf8\x3f\x3f\x3f\x3f",
+  "\xe9\xe4\xf6\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f",
+  "\xe9\xe4\xf6\xf2\x3f\x3f\x3f\x3f\x3f\x3f\x3f",
+  "\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\xd0\x94\xce\x98\xc4\x9d\xd7\x93\xd8\xb5\xc4\xb7\xd1\x9b",
+  "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f"
+};
+static const char *testEncodingsExpected[11] = {
+  "\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\x3f\x3f\x3f\x3f\x3f\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\xd7\x93\x3f\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\x3f\xce\x98\x3f\x3f\x3f\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\xd8\xb5\x3f\x3f",
+  "\x3f\x3f\x3f\x3f\xd0\x94\x3f\x3f\x3f\x3f\x3f\xd1\x9b",
+  "\xc3\xa9\xc3\xa4\xc3\xb6\x3f\x3f\x3f\x3f\x3f\x3f\xc4\xb7\x3f",
+  "\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\x3f\x3f\xc4\x9d\x3f\x3f\x3f\x3f",
+  "\xc3\xa9\xc3\xa4\xc3\xb6\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f",
+  "\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\x3f\x3f\x3f\x3f\x3f\x3f\x3f",
+  "\xc3\xa9\xc3\xa4\xc3\xb6\xc3\xb2\xd0\x94\xce\x98\xc4\x9d\xd7\x93\xd8\xb5\xc4\xb7\xd1\x9b",
+  "\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f\x3f"
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/EncodingTests.py	Mon Aug 04 19:24:28 2014 +0200
@@ -0,0 +1,41 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+source = u'éäöòДΘĝדصķћ'
+
+encodings = {
+    'UTF-8' : 'Utf8',
+    'ASCII' : 'Ascii',
+    'ISO-8859-1' : 'Latin1',
+    'ISO-8859-2' : 'Latin2',
+    'ISO-8859-3' : 'Latin3',
+    'ISO-8859-4' : 'Latin4',
+    'ISO-8859-9' : 'Latin5',
+    'ISO-8859-5' : 'Cyrillic',
+    'ISO-8859-6' : 'Arabic',
+    'ISO-8859-7' : 'Greek',
+    'ISO-8859-8' : 'Hebrew',
+}
+
+# "63" corresponds to "?"
+l = []
+encoded = []
+expected = []
+
+def ToArray(source):
+    result = ''
+    for byte in bytearray(source):
+        result += '\\x%02x' % byte
+    return '"%s"' % result
+    
+
+for encoding, orthancEnumeration in encodings.iteritems():
+    l.append('Orthanc::Encoding_%s' % orthancEnumeration)
+    s = source.encode(encoding, 'replace')
+    encoded.append(ToArray(s))
+    expected.append(ToArray(s.decode(encoding).encode('utf-8')))
+
+print 'static const unsigned int testEncodingsCount = %d;' % len(encodings)
+print 'static const Orthanc::Encoding testEncodings[] = {\n  %s\n};' % (',\n  '.join(l))
+print 'static const char *testEncodingsEncoded[%d] = {\n  %s\n};' % (len(encodings), ',\n  '.join(encoded))
+print 'static const char *testEncodingsExpected[%d] = {\n  %s\n};' % (len(encodings), ',\n  '.join(expected))
--- a/UnitTestsSources/FromDcmtkTests.cpp	Mon Aug 04 18:12:21 2014 +0200
+++ b/UnitTestsSources/FromDcmtkTests.cpp	Mon Aug 04 19:24:28 2014 +0200
@@ -41,6 +41,7 @@
 #include "../Core/ImageFormats/PngReader.h"
 #include "../Core/ImageFormats/PngWriter.h"
 #include "../Core/Uuid.h"
+#include "../Resources/EncodingTests.h"
 
 using namespace Orthanc;
 
@@ -176,3 +177,15 @@
     o.SaveToFile("UnitTestsResults/png4.dcm");
   }
 }
+
+
+TEST(Toolbox, Encodings1)
+{
+  for (unsigned int i = 0; i < testEncodingsCount; i++)
+  {
+    std::string source(testEncodingsEncoded[i]);
+    std::string expected(testEncodingsExpected[i]);
+    std::string s = Toolbox::ConvertToUtf8(source, testEncodings[i]);
+    ASSERT_EQ(expected, s);
+  }
+}