Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 3.84 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  5.  * of this software and associated documentation files (the "Software"), to deal
  6.  * in the Software without restriction, including without limitation the rights
  7.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8.  * copies of the Software, and to permit persons to whom the Software is
  9.  * furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice shall be included in
  12.  * all copies or substantial portions of the Software.
  13.  *
  14.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20.  * THE SOFTWARE.
  21.  */
  22. #include "utf8.h"
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30.  
  31. static void test_enc(const int input[], const int size, const char **expected) {
  32.   char buf[4];
  33.   int i, len;
  34.  
  35.   for (i = 0; i < size; i++) {
  36.     len = utf8_enc((unsigned long) input[i], buf, 4);
  37.     if (len < 0) {
  38.       assert((const char *) (size_t) len == expected[i]);
  39.     }
  40.     else {
  41.       assert(len == (int) strlen(expected[i]));
  42.       assert(0 == memcmp(expected[i], buf, len));
  43.     }
  44.   }
  45. }
  46.  
  47. static void test_dec(const char *input, const int expected[], const int size) {
  48.   const char *s, *se;
  49.   int i;
  50.  
  51.   s = input, se = input + strlen(input);
  52.   for (i = 0; i < size; i++) {
  53.     const int c = utf8_dec(&s, se - s);
  54.     assert(c == expected[i]);
  55.   }
  56. }
  57.  
  58. int main(void) {
  59.   /*
  60.    * encoder tests
  61.    */
  62.   {
  63.     const int input[] = { 't', 'e', 's', 't' };
  64.     const char *expected[] = { "t", "e", "s", "t", 0 };
  65.     test_enc(input, sizeof(input) / sizeof(input[0]), expected);
  66.   }
  67.   {
  68.     const int input[] = { 'b', 252, 'c', 'h', 'e', 'r' };
  69.     const char *expected[] = { "b", "ü", "c", "h", "e", "r", 0 };
  70.     test_enc(input, sizeof(input) / sizeof(input[0]), expected);
  71.   }
  72.   {
  73.     const int input[] = { 0x20AC };
  74.     const char *expected[] = { "€" };
  75.     test_enc(input, sizeof(input) / sizeof(input[0]), expected);
  76.   }
  77.   {
  78.     const int input[] = { 0x024B62 };
  79.     const char *expected[] = { "\xF0\xA4\xAD\xA2" };
  80.     test_enc(input, sizeof(input) / sizeof(input[0]), expected);
  81.   }
  82.   {
  83.     const int input[] = { -1 };
  84.     const char *expected[] = { (const char *) -EINVAL };
  85.     test_enc(input, sizeof(input) / sizeof(input[0]), expected);
  86.   }
  87.  
  88.   /*
  89.    * decoder tests
  90.    */
  91.   {
  92.     const int expected[] = { 't', 'e', 's', 't' };
  93.     test_dec("test", expected, sizeof(expected) / sizeof(expected[0]));
  94.   }
  95.   {
  96.     const int expected[] = { 'b', 252, 'c', 'h', 'e', 'r' };
  97.     test_dec("bücher", expected, sizeof(expected) / sizeof(expected[0]));
  98.   }
  99.   {
  100.     const int expected[] = { 0x20AC };
  101.     test_dec("€", expected, sizeof(expected) / sizeof(expected[0]));
  102.   }
  103.   {
  104.     const int expected[] = { 0x024B62 };
  105.     test_dec("\xF0\xA4\xAD\xA2", expected, sizeof(expected) / sizeof(expected[0]));
  106.   }
  107.   {
  108.     const int expected[] = { -1 };
  109.     test_dec("\xFF", expected, sizeof(expected) / sizeof(expected[0]));
  110.   }
  111.   {
  112.     const int expected[] = { -1 };
  113.     test_dec("\xFF\xFF", expected, sizeof(expected) / sizeof(expected[0]));
  114.   }
  115.   {
  116.     const int expected[] = { -1 };
  117.     test_dec("\xFF\xFF\xFF", expected, sizeof(expected) / sizeof(expected[0]));
  118.   }
  119.   /* outside any plane, should fail
  120.   {
  121.     const int expected[] = { -1 };
  122.     test_dec("\xFF\xFF\xFF\xFF", expected, sizeof(expected) / sizeof(expected[0]));
  123.   }
  124.   */
  125.   return 0;
  126. }