cfad47cfa3/t3compiler/tads3/test/test_utf8.cpp

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
#ifdef RCSID
2
static char RCSid[] =
3
"$Header: d:/cvsroot/tads/tads3/test/test_utf8.cpp,v 1.2 1999/05/17 02:52:31 MJRoberts Exp $";
4
#endif
5
6
/* 
7
 *   Copyright (c) 1998, 2002 Michael J. Roberts.  All Rights Reserved.
8
 *   
9
 *   Please see the accompanying license file, LICENSE.TXT, for information
10
 *   on using and copying this software.  
11
 */
12
/*
13
Name
14
  test_utf8.cpp - test for UTF8 class
15
Function
16
  
17
Notes
18
  
19
Modified
20
  10/16/98 MJRoberts  - Creation
21
*/
22
23
#include <stdlib.h>
24
#include <stdio.h>
25
26
#include "utf8.h"
27
#include "t3test.h"
28
29
int main()
30
{
31
    static wchar_t test1[] =
32
    {
33
        0x0010, 0x0020, 0x0030, 0x0040, 0x0050, 0x0060, 0x0070, 0x007f,
34
        0x0080, 0x0100, 0x0200, 0x0400, 0x0500, 0x0600, 0x0700, 0x07ff,
35
        0x0800, 0x1000, 0x2000, 0x4000, 0x8000, 0xA000, 0xF000, 0xFFFF,
36
        0
37
    };
38
    wchar_t *wp;
39
    char buf[256];
40
    utf8_ptr p;
41
    size_t len;
42
    size_t i;
43
    wchar_t ch;
44
45
    /* initialize for testing */
46
    test_init();
47
48
    /* encode the test string */
49
    p.set(buf);
50
    p.setwcharsz(test1, sizeof(buf));
51
52
    /* go back to the start of the buffer */
53
    p.set(buf);
54
55
    /* compute the length */
56
    len = p.lenz();
57
    printf("test1: len = %d - %s\n", len,
58
           (len == sizeof(test1)/sizeof(test1[0]) - 1) ? "ok" : "ERROR");
59
60
    /* 
61
     *   run through the string, decode characters, and compare the
62
     *   results to the original 
63
     */
64
    printf("-- incrementing --\n");
65
    for (i = 0, wp = test1 ; i < len ; p.inc(), ++wp, ++i)
66
    {
67
        /* decode the current character */
68
        ch = p.getch();
69
70
        /* check it */
71
        printf("ch[%d] = %04x - %s\n", i, ch,
72
               (ch == *wp ? "ok" : "ERROR"));
73
    }
74
75
    /* go backwards through the buffer */
76
    printf("-- decrementing --\n");
77
    do
78
    {
79
        /* move back one character */
80
        --wp;
81
        p.dec();
82
        --i;
83
84
        /* decode the current character */
85
        ch = p.getch();
86
87
        /* check it */
88
        printf("ch[%d] = %04x - %s\n", i, ch,
89
               (ch == *wp ? "ok" : "ERROR"));
90
    } while (i != 0);
91
92
    /* success */
93
    return 0;
94
}
95