1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/TestUTF.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,189 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "mozilla/ArrayUtils.h" 1.9 + 1.10 +#include <stdio.h> 1.11 +#include <stdlib.h> 1.12 +#include "nsString.h" 1.13 +#include "nsStringBuffer.h" 1.14 +#include "nsReadableUtils.h" 1.15 +#include "UTFStrings.h" 1.16 +#include "nsUnicharUtils.h" 1.17 +#include "mozilla/HashFunctions.h" 1.18 + 1.19 +using namespace mozilla; 1.20 + 1.21 +namespace TestUTF { 1.22 + 1.23 +bool 1.24 +test_valid() 1.25 +{ 1.26 + for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { 1.27 + nsDependentCString str8(ValidStrings[i].m8); 1.28 + nsDependentString str16(ValidStrings[i].m16); 1.29 + 1.30 + if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) 1.31 + return false; 1.32 + 1.33 + if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) 1.34 + return false; 1.35 + 1.36 + nsCString tmp8("string "); 1.37 + AppendUTF16toUTF8(str16, tmp8); 1.38 + if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) 1.39 + return false; 1.40 + 1.41 + nsString tmp16(NS_LITERAL_STRING("string ")); 1.42 + AppendUTF8toUTF16(str8, tmp16); 1.43 + if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) 1.44 + return false; 1.45 + 1.46 + if (CompareUTF8toUTF16(str8, str16) != 0) 1.47 + return false; 1.48 + } 1.49 + 1.50 + return true; 1.51 +} 1.52 + 1.53 +bool 1.54 +test_invalid16() 1.55 +{ 1.56 + for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) { 1.57 + nsDependentString str16(Invalid16Strings[i].m16); 1.58 + nsDependentCString str8(Invalid16Strings[i].m8); 1.59 + 1.60 + if (!NS_ConvertUTF16toUTF8(str16).Equals(str8)) 1.61 + return false; 1.62 + 1.63 + nsCString tmp8("string "); 1.64 + AppendUTF16toUTF8(str16, tmp8); 1.65 + if (!tmp8.Equals(NS_LITERAL_CSTRING("string ") + str8)) 1.66 + return false; 1.67 + 1.68 + if (CompareUTF8toUTF16(str8, str16) != 0) 1.69 + return false; 1.70 + } 1.71 + 1.72 + return true; 1.73 +} 1.74 + 1.75 +bool 1.76 +test_invalid8() 1.77 +{ 1.78 + for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { 1.79 + nsDependentString str16(Invalid8Strings[i].m16); 1.80 + nsDependentCString str8(Invalid8Strings[i].m8); 1.81 + 1.82 + if (!NS_ConvertUTF8toUTF16(str8).Equals(str16)) 1.83 + return false; 1.84 + 1.85 + nsString tmp16(NS_LITERAL_STRING("string ")); 1.86 + AppendUTF8toUTF16(str8, tmp16); 1.87 + if (!tmp16.Equals(NS_LITERAL_STRING("string ") + str16)) 1.88 + return false; 1.89 + 1.90 + if (CompareUTF8toUTF16(str8, str16) != 0) 1.91 + return false; 1.92 + } 1.93 + 1.94 + return true; 1.95 +} 1.96 + 1.97 +bool 1.98 +test_malformed8() 1.99 +{ 1.100 +// Don't run this test in debug builds as that intentionally asserts. 1.101 +#ifndef DEBUG 1.102 + for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { 1.103 + nsDependentCString str8(Malformed8Strings[i]); 1.104 + 1.105 + if (!NS_ConvertUTF8toUTF16(str8).IsEmpty()) 1.106 + return false; 1.107 + 1.108 + nsString tmp16(NS_LITERAL_STRING("string")); 1.109 + AppendUTF8toUTF16(str8, tmp16); 1.110 + if (!tmp16.Equals(NS_LITERAL_STRING("string"))) 1.111 + return false; 1.112 + 1.113 + if (CompareUTF8toUTF16(str8, EmptyString()) == 0) 1.114 + return false; 1.115 + } 1.116 +#endif 1.117 + 1.118 + return true; 1.119 +} 1.120 + 1.121 +bool 1.122 +test_hashas16() 1.123 +{ 1.124 + for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) { 1.125 + nsDependentCString str8(ValidStrings[i].m8); 1.126 + bool err; 1.127 + if (HashString(ValidStrings[i].m16) != 1.128 + HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || 1.129 + err) 1.130 + return false; 1.131 + } 1.132 + 1.133 + for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) { 1.134 + nsDependentCString str8(Invalid8Strings[i].m8); 1.135 + bool err; 1.136 + if (HashString(Invalid8Strings[i].m16) != 1.137 + HashUTF8AsUTF16(str8.get(), str8.Length(), &err) || 1.138 + err) 1.139 + return false; 1.140 + } 1.141 + 1.142 +// Don't run this test in debug builds as that intentionally asserts. 1.143 +#ifndef DEBUG 1.144 + for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) { 1.145 + nsDependentCString str8(Malformed8Strings[i]); 1.146 + bool err; 1.147 + if (HashUTF8AsUTF16(str8.get(), str8.Length(), &err) != 0 || 1.148 + !err) 1.149 + return false; 1.150 + } 1.151 +#endif 1.152 + 1.153 + return true; 1.154 +} 1.155 + 1.156 +typedef bool (*TestFunc)(); 1.157 + 1.158 +static const struct Test 1.159 + { 1.160 + const char* name; 1.161 + TestFunc func; 1.162 + } 1.163 +tests[] = 1.164 + { 1.165 + { "test_valid", test_valid }, 1.166 + { "test_invalid16", test_invalid16 }, 1.167 + { "test_invalid8", test_invalid8 }, 1.168 + { "test_malformed8", test_malformed8 }, 1.169 + { "test_hashas16", test_hashas16 }, 1.170 + { nullptr, nullptr } 1.171 + }; 1.172 + 1.173 +} 1.174 + 1.175 +using namespace TestUTF; 1.176 + 1.177 +int main(int argc, char **argv) 1.178 + { 1.179 + int count = 1; 1.180 + if (argc > 1) 1.181 + count = atoi(argv[1]); 1.182 + 1.183 + while (count--) 1.184 + { 1.185 + for (const Test* t = tests; t->name != nullptr; ++t) 1.186 + { 1.187 + printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE <--"); 1.188 + } 1.189 + } 1.190 + 1.191 + return 0; 1.192 + }