testucs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr.h"
  17. #include "arch/win32/fspr_arch_utf8.h"
  18. #include <wchar.h>
  19. #include <string.h>
  20. struct testval {
  21. unsigned char n[8];
  22. wchar_t w[4];
  23. int nl;
  24. int wl;
  25. };
  26. void displaynw(struct testval *f, struct testval *l)
  27. {
  28. char x[80], *t = x;
  29. int i;
  30. for (i = 0; i < f->nl; ++i)
  31. t += sprintf(t, "%02X ", f->n[i]);
  32. *(t++) = '-';
  33. for (i = 0; i < l->nl; ++i)
  34. t += sprintf(t, " %02X", l->n[i]);
  35. *(t++) = ' ';
  36. *(t++) = '=';
  37. *(t++) = ' ';
  38. for (i = 0; i < f->wl; ++i)
  39. t += sprintf(t, "%04X ", f->w[i]);
  40. *(t++) = '-';
  41. for (i = 0; i < l->wl; ++i)
  42. t += sprintf(t, " %04X", l->w[i]);
  43. puts(x);
  44. }
  45. /*
  46. * Test every possible byte value.
  47. * If the test passes or fails at this byte value we are done.
  48. * Otherwise iterate test_nrange again, appending another byte.
  49. */
  50. void test_nrange(struct testval *p)
  51. {
  52. struct testval f, l, s;
  53. fspr_status_t rc;
  54. int success = 0;
  55. memcpy (&s, p, sizeof(s));
  56. ++s.nl;
  57. do {
  58. fspr_size_t nl = s.nl, wl = sizeof(s.w) / 2;
  59. rc = fspr_conv_utf8_to_ucs2(s.n, &nl, s.w, &wl);
  60. s.wl = (sizeof(s.w) / 2) - wl;
  61. if (!nl && rc == APR_SUCCESS) {
  62. if (!success) {
  63. memcpy(&f, &s, sizeof(s));
  64. success = -1;
  65. }
  66. else {
  67. if (s.wl != l.wl
  68. || memcmp(s.w, l.w, (s.wl - 1) * 2) != 0
  69. || s.w[s.wl - 1] != l.w[l.wl - 1] + 1) {
  70. displaynw(&f, &l);
  71. memcpy(&f, &s, sizeof(s));
  72. }
  73. }
  74. memcpy(&l, &s, sizeof(s));
  75. }
  76. else {
  77. if (success) {
  78. displaynw(&f, &l);
  79. success = 0;
  80. }
  81. if (rc == APR_INCOMPLETE) {
  82. test_nrange(&s);
  83. }
  84. }
  85. } while (++s.n[s.nl - 1]);
  86. if (success) {
  87. displaynw(&f, &l);
  88. success = 0;
  89. }
  90. }
  91. /*
  92. * Test every possible word value.
  93. * Once we are finished, retest every possible word value.
  94. * if the test fails on the following null word, iterate test_nrange
  95. * again, appending another word.
  96. * This assures the output order of the two tests are in sync.
  97. */
  98. void test_wrange(struct testval *p)
  99. {
  100. struct testval f, l, s;
  101. fspr_status_t rc;
  102. int success = 0;
  103. memcpy (&s, p, sizeof(s));
  104. ++s.wl;
  105. do {
  106. fspr_size_t nl = sizeof(s.n), wl = s.wl;
  107. rc = fspr_conv_ucs2_to_utf8(s.w, &wl, s.n, &nl);
  108. s.nl = sizeof(s.n) - nl;
  109. if (!wl && rc == APR_SUCCESS) {
  110. if (!success) {
  111. memcpy(&f, &s, sizeof(s));
  112. success = -1;
  113. }
  114. else {
  115. if (s.nl != l.nl
  116. || memcmp(s.n, l.n, s.nl - 1) != 0
  117. || s.n[s.nl - 1] != l.n[l.nl - 1] + 1) {
  118. displaynw(&f, &l);
  119. memcpy(&f, &s, sizeof(s));
  120. }
  121. }
  122. memcpy(&l, &s, sizeof(s));
  123. }
  124. else {
  125. if (success) {
  126. displaynw(&f, &l);
  127. success = 0;
  128. }
  129. }
  130. } while (++s.w[s.wl - 1]);
  131. if (success) {
  132. displaynw(&f, &l);
  133. success = 0;
  134. }
  135. do {
  136. int wl = s.wl, nl = sizeof(s.n);
  137. rc = fspr_conv_ucs2_to_utf8(s.w, &wl, s.n, &nl);
  138. s.nl = sizeof(s.n) - s.nl;
  139. if (rc == APR_INCOMPLETE) {
  140. test_wrange(&s);
  141. }
  142. } while (++s.w[s.wl - 1]);
  143. }
  144. /*
  145. * Syntax: testucs [w|n]
  146. *
  147. * If arg is not recognized, run both tests.
  148. */
  149. int main(int argc, char **argv)
  150. {
  151. struct testval s;
  152. memset (&s, 0, sizeof(s));
  153. if (argc < 2 || fspr_tolower(*argv[1]) != 'w') {
  154. printf ("\n\nTesting Narrow Char Ranges\n");
  155. test_nrange(&s);
  156. }
  157. if (argc < 2 || fspr_tolower(*argv[1]) != 'n') {
  158. printf ("\n\nTesting Wide Char Ranges\n");
  159. test_wrange(&s);
  160. }
  161. return 0;
  162. }