utf8test.c 4.57 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@internal
 * @CFILE utf8test.c UTF8 encoding - decoding tests
 *
 * @author Pekka Pessi <pessi@research.nokia.com>
 *
 * @date Created: Tue Apr 21 15:32:38 1998 pessi
 */

#include "config.h"

35
#include <sofia-sip/utf8.h>
Pekka Pessi's avatar
Pekka Pessi committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
  static ucs4 ucs4test0[] = { 
    0x41u, 0xC1u, 0x841u, 0x10041u, 0x200041u, 0x4000041u, 0
  };
  static utf8 ucs4test1[] = 
    "A" 
    "\303\201"
    "\340\241\201"
    "\360\220\201\201"
    "\370\210\200\201\201"
    "\374\204\200\200\201\201";

  static ucs2 ucs2test0[] = { 
    0x41u, 0xC1u, 0x841u, 0
  };
  static utf8 ucs2test1[] = 
    "A" 
    "\303\201"
    "\340\241\201";

  ucs4 ucs4s[1024] = { 0 };
  ucs2 ucs2s[1024] = { 0 }; 
  utf8 utf8s[1024] = { 0 };

  size_t len;
  int result = 0;
  int i;

  puts("testing ucs4len(ucs4test0)");

  len = ucs4len(ucs4test0);
  if (len != 6) {
    printf("ucs4len(ucs4test0) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs4declen(ucs4test1)");

  len = ucs4declen(ucs4test1);
  if (len != 6) {
    printf("ucs4declen(ucs4test1) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs4enclen(ucs4test0, 6, NULL)");

  len = ucs4enclen(ucs4test0, 6, NULL);
  if (len != 22) {
    printf("ucs4enclen(ucs4test0, 6, NULL) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs4enclen(ucs4test0, 5, NULL)");

  len = ucs4enclen(ucs4test0, 5, NULL);
  if (len != 16) {
    printf("ucs4enclen(ucs4test0, 5, NULL) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs4encode(utf8s, ucs4test0, 6, NULL)");

  if (ucs4encode(utf8s, ucs4test0, 6, NULL) != 22 ||
      strcmp((char *)utf8s, (char*)ucs4test1)) {
    printf("ucs4encode(utf8s, ucs4test0, 6, NULL) fails\n");
    result = 1;
    printf("\tutf8s=\"%s\"\n", utf8s);
  }
  else puts("OK");

  puts("testing ucs4decode(ucs4s, sizeof(ucs4s), ucs4test1)");

  if (ucs4decode(ucs4s, sizeof(ucs4s)/sizeof(*ucs4s), ucs4test1) != 6
      || ucs4cmp(ucs4s, ucs4test0)) {
    printf("ucs4decode(ucs4s, sizeof(ucs4s), ucs4test1) fails\n");
    result = 1;
    for (i = 0; i < 8; i++) {
      printf("\tucs4s[%d]=0x%x\n", i, ucs4s[i]);
    }
  }
  else puts("OK");

  /* UCS2 */
  
  puts("testing ucs2len(ucs2test0)");

  len = ucs2len(ucs2test0);
  if (len != 3) {
    printf("ucs2len(ucs2test0) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs2declen(ucs2test1)");

  len = ucs2declen(ucs2test1);
  if (len != 3) {
    printf("ucs2declen(ucs2test1) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs2enclen(ucs2test0, 3, NULL)");

  len = ucs2enclen(ucs2test0, 3, NULL);
  if (len != 7) {
    printf("ucs2enclen(ucs2test1, 3, NULL) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs2enclen(ucs2test0, 2, NULL)");

  len = ucs2enclen(ucs2test0, 2, NULL);
  if (len != 4) {
    printf("ucs2enclen(ucs2test1, NULL) returns %u\n", len);
    result = 1;
  }
  else puts("OK");

  puts("testing ucs2encode(utf8s, ucs2test0, 3, NULL)");

  if (ucs2encode(utf8s, ucs2test0, 3, NULL) != 7 ||
      strcmp((char *)utf8s, (char*)ucs2test1)) {
    printf("ucs2encode(utf8s, ucs2test0, 3, NULL) fails\n");
    result = 1;
    printf("\tutf8s=\"%s\"\n", utf8s);
  }
  else puts("OK");

  puts("testing ucs2decode(ucs2s, sizeof(ucs2s)/sizeof(*ucs2s), ucs2test1)");

  if (ucs2decode(ucs2s, sizeof(ucs2s)/sizeof(*ucs2s), ucs2test1) != 3 ||
      ucs2cmp(ucs2s, ucs2test0)) {
    printf("ucs2decode(ucs2s, sizeof(ucs2s)/sizeof(*ucs2s), ucs2test1) fails\n");
    result = 1;
    for (i = 0; i < 8; i++) {
      printf("\tucs2s[%d]=0x%x\n", i, ucs2s[i]);
    }
  }
  else puts("OK");


  return result;
}