summaryrefslogtreecommitdiff
path: root/Source/Tests/winchar.cpp
blob: bd6b780de3306befa02fc754829b49859fad5401 (plain)
1
2
3
4
5
6
7
8
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
35
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
// Reviewed for Unicode support by Jim Park -- 08/13/2007
#include <cppunit/extensions/HelperMacros.h>
#include "../util.h"
#include "../winchar.h"

#include <time.h>
#include <stdlib.h>

// macro for fixing endianity
#define _x(x) FIX_ENDIAN_INT16(WCHAR(x))

// BUGBUG: These tests currently run as Ansi, it would be better if it respected defenv['UNICODE']

// BUGBUG: WinWStrDupFromWC is unable to test WCToUTF16LEHlpr because it is behind #ifdef MAKENSIS

// TODO write equal() for WINWCHAR -- http://subcommanderblog.wordpress.com/2009/01/10/cppunit_assert_equal-and-custom-data-types/

class WinCharTest : public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE( WinCharTest );
  CPPUNIT_TEST( testFromTchar );
  CPPUNIT_TEST( testStrCpy );
  CPPUNIT_TEST( testStrNCpy );
  CPPUNIT_TEST( testStrLen );
  CPPUNIT_TEST( testStrCmp );
  CPPUNIT_TEST( testStrDup );
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp() {
  }

  void testFromTchar() {
    WINWCHAR test[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };

    WINWCHAR *dyn = WinWStrDupFromTChar(_T("test"));
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, sizeof(test)) );
    free(dyn);

    dyn = WinWStrDupFromChar("test");
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, sizeof(test)) );
    free(dyn);

    dyn = WinWStrDupFromWC(L"test");
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(test, dyn, sizeof(test)) );
    free(dyn);
  }

  void testStrCpy() {
    WINWCHAR a[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };
    WINWCHAR b[5];

    CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrCpy(b, a)) );
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(a, b, 5 * sizeof(WINWCHAR)) );
  }

  void testStrNCpy() {
    WINWCHAR a1[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };
    WINWCHAR b[5];

    CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrNCpy(b, a1, 5)) );
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(a1, b, 5 * sizeof(WINWCHAR)) );

    WINWCHAR a2[] = { _x('t'), _x('e'), 0, 0, 0 };

    CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrNCpy(b, a2, 5)) );
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(a2, b, 5 * sizeof(WINWCHAR)) );

    CPPUNIT_ASSERT( !WinWStrCmp(b, WinWStrNCpy(b, a1, 2)) );
    CPPUNIT_ASSERT_EQUAL( 0, memcmp(a2, b, 5 * sizeof(WINWCHAR)) );
  }

  void testStrLen() {
    WINWCHAR test[] = { _x('t'), _x('e'), _x('s'), _x('t'), 0 };

    CPPUNIT_ASSERT_EQUAL( (size_t) 4, WinWStrLen(test) );
  }

  static int simplifyNumber(int n) {
    if (n < 0)
      return -1;
    if (n > 0)
      return 1;
    return 0;
  }

  void testStrCmp() {
    char a[] = "a";
    WINWCHAR wa[] = { _x('a'), 0 };
    char b[] = "b";
    WINWCHAR wb[] = { _x('b'), 0 };
    char empty[] = "";
    WINWCHAR wempty[] = { 0 };

    #define TEST_STR_CMP(x, y) \
      CPPUNIT_ASSERT_EQUAL(\
        simplifyNumber(strcmp(x, y)), \
        simplifyNumber(WinWStrCmp(w##x, w##y)) \
      )

    TEST_STR_CMP(a, b);
    TEST_STR_CMP(b, a);
    TEST_STR_CMP(a, a);
    TEST_STR_CMP(b, b);
    TEST_STR_CMP(a, empty);
    TEST_STR_CMP(empty, b);
    TEST_STR_CMP(empty, empty);
  }

  void testStrDup() {
    WINWCHAR a[] = { _x('a'), _x('b'), _x('c'), 0 };

    WINWCHAR *b = WinWStrDupFromWinWStr(a);

    CPPUNIT_ASSERT_EQUAL( 0, WinWStrCmp(a, b) );

    free(b);
  }

};

CPPUNIT_TEST_SUITE_REGISTRATION( WinCharTest );