UnicodeTextTests.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "stdafx.h"
  6. #pragma warning(disable:26434) // Function definition hides non-virtual function in base class
  7. #pragma warning(disable:26439) // Implicit noexcept
  8. #pragma warning(disable:26451) // Arithmetic overflow
  9. #pragma warning(disable:26495) // Uninitialized member variable
  10. #include "catch.hpp"
  11. namespace UnicodeTextTest
  12. {
  13. void Test(const WCHAR* str1, const WCHAR* str2, int expected)
  14. {
  15. REQUIRE(g_testHooksLoaded);
  16. // there are two tests, one to validate the expected value and validate the result of the call
  17. int compareStringResult = CompareStringEx(LOCALE_NAME_USER_DEFAULT, NORM_IGNORECASE | SORT_DIGITSASNUMBERS, str1, -1, str2, -1, NULL, NULL, 0);
  18. CHECK(compareStringResult != 0);
  19. compareStringResult = compareStringResult - CSTR_EQUAL;
  20. int res = g_testHooks.pfLogicalCompareStringImpl(str1, static_cast<int>(wcslen(str1)), str2, static_cast<int>(wcslen(str2)));
  21. //test to check that expected value passed is correct
  22. REQUIRE(compareStringResult == expected);
  23. //test to check that the result from call to LogicalStringCompareImpl is the expected value
  24. REQUIRE(res == expected);
  25. }
  26. void TestNullCharacters(const WCHAR* str1, int str1size, const WCHAR* str2, int str2size, int expected)
  27. {
  28. REQUIRE(g_testHooksLoaded);
  29. // there are two tests, one to validate the expected value and validate the result of the call
  30. int compareStringResult = CompareStringEx(LOCALE_NAME_USER_DEFAULT, NORM_IGNORECASE | SORT_DIGITSASNUMBERS, str1, str1size, str2, str2size, NULL, NULL, 0);
  31. CHECK(compareStringResult != 0);
  32. compareStringResult = compareStringResult - CSTR_EQUAL;
  33. int res = g_testHooks.pfLogicalCompareStringImpl(str1, str1size, str2, str2size);
  34. //test to check that expected value passed is correct
  35. REQUIRE(compareStringResult == expected);
  36. //test to check that the result from call to LogicalStringCompareImpl is the expected value
  37. REQUIRE(res == expected);
  38. }
  39. TEST_CASE("LogicalCompareString_BasicLatinLowercase", "[UnicodeText]")
  40. {
  41. Test(_u("a"), _u("a"), 0);
  42. Test(_u("a"), _u("b"), -1);
  43. Test(_u("b"), _u("a"), 1);
  44. }
  45. TEST_CASE("LogicalCompareString_BasicNumbers", "[UnicodeText]")
  46. {
  47. Test(_u("1"), _u("2"), -1);
  48. Test(_u("2"), _u("1"), 1);
  49. Test(_u("10"), _u("01"), 1);
  50. Test(_u("01"), _u("10"), -1);
  51. Test(_u("01"), _u("1"), -1);
  52. Test(_u("1"), _u("01"), 1);
  53. }
  54. TEST_CASE("LogicalCompareString_Alphanumeric", "[UnicodeText]")
  55. {
  56. Test(_u("1a"), _u("a1"), -1);
  57. Test(_u("aa1"), _u("a1"), 1);
  58. Test(_u("a1"), _u("a1"), 0);
  59. Test(_u("a1"), _u("b1"), -1);
  60. Test(_u("b1"), _u("a1"), 1);
  61. Test(_u("a1"), _u("a2"), -1);
  62. Test(_u("a10"), _u("a2"), 1);
  63. Test(_u("a2"), _u("a10"), -1);
  64. }
  65. TEST_CASE("LogicalCompareString_ComplexAlphanumeric", "[UnicodeText]")
  66. {
  67. Test(_u("A1"), _u("a1"), 0);
  68. Test(_u("A1"), _u("b1"), -1);
  69. Test(_u("B1"), _u("a1"), 1);
  70. Test(_u("A1"), _u("a2"), -1);
  71. Test(_u("A10"), _u("a2"), 1);
  72. Test(_u("A2"), _u("a10"), -1);
  73. Test(_u("123"), _u("456"), -1);
  74. Test(_u("456"), _u("123"), 1);
  75. Test(_u("abc123"), _u("def123"), -1);
  76. Test(_u("abc123"), _u("abc123"), 0);
  77. Test(_u("abc123"), _u("abc0123"), 1);
  78. Test(_u("abc123"), _u("abc124"), -1);
  79. Test(_u("abc124"), _u("abc123"), 1);
  80. Test(_u("abc123def"), _u("abc123def"), 0);
  81. Test(_u("abc123def"), _u("abc123eef"), -1);
  82. Test(_u("abc123eef"), _u("abc123def"), 1);
  83. Test(_u("abc1def"), _u("abc10def"), -1);
  84. Test(_u("abc1def1"), _u("abc1def12"), -1);
  85. Test(_u("2string"), _u("3string"), -1);
  86. Test(_u("20string"), _u("3string"), 1);
  87. Test(_u("20string"), _u("st2ring"), -1);
  88. Test(_u("st3ring"), _u("st2ring"), 1);
  89. Test(_u("2String"), _u("3string"), -1);
  90. Test(_u("20String"), _u("3string"), 1);
  91. Test(_u("20sTRing"), _u("st2ring"), -1);
  92. Test(_u("st3rING"), _u("st2riNG"), 1);
  93. }
  94. TEST_CASE("LogicalCompareString_EmbeddedNullCharacters", "[UnicodeText]")
  95. {
  96. TestNullCharacters(_u("\0\0ab\0\0c123def\0\0"), 15, _u("abc123def"), 9, 0);
  97. }
  98. }