File: li_std_wstring_runme.cs

package info (click to toggle)
swig 4.1.0-0.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 47,992 kB
  • sloc: cpp: 50,555; ansic: 27,840; java: 15,366; python: 11,221; cs: 8,852; ruby: 6,307; yacc: 6,290; makefile: 5,702; sh: 5,492; perl: 3,818; php: 3,046; ml: 2,094; lisp: 1,756; javascript: 1,751; tcl: 1,499; xml: 115
file content (194 lines) | stat: -rw-r--r-- 6,593 bytes parent folder | download | duplicates (3)
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
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
189
190
191
192
193
194
// This file has a BOM for UTF-8
// Notes for displaying UTF-8 properly in Windows: https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell

using System;
using li_std_wstringNamespace;

public class runme
{
    static private void check_equal(char a, char b)
    {
        if (a != b)
            throw new Exception("char failed '" + a + "' != '" + b + "'");
    }

    static private void display_bytes(string s)
    {
        Console.Write("[");
        if (s != null)
        {
            foreach (char x in s)
            {
                int n = Convert.ToInt32(x);
                Console.Write(n.ToString("X") + " ");
            }
        }
        else
            Console.Write("null");
        Console.WriteLine("]");
    }

    static private void check_equal(string a, string b)
    {
        if (li_std_wstring.trace) {
            Console.WriteLine("check_equal {0} {1}", a, b);
            display_bytes(a);
            display_bytes(b);
        }
        if (a != b)
          throw new Exception("string failed '" + a + "' != '" + b + "'");
    }

    static void Main()
    {
        char h = 'h';
        check_equal(li_std_wstring.test_wcvalue(h), h);

        string x = "abc";
        check_equal(li_std_wstring.test_ccvalue(x), x);
        check_equal(li_std_wstring.test_cvalue(x), x);

        check_equal(li_std_wstring.test_wchar_overload(x), x);
        check_equal(li_std_wstring.test_wchar_overload(), null);

        li_std_wstring.test_pointer(null);
        li_std_wstring.test_const_pointer(null);

        check_equal(li_std_wstring.test_ccvalue(null), null);

        try {
            li_std_wstring.test_value(null);
            throw new Exception("NULL check failed");
        } catch (ArgumentNullException) {
        }

        try {
            li_std_wstring.test_reference(null);
            throw new Exception("NULL check failed");
        } catch (ArgumentNullException e) {
            if (!e.Message.Contains("is null"))
                throw new Exception("Missing text " + e);
        }
        try {
            li_std_wstring.test_const_reference(null);
            throw new Exception("NULL check failed");
        } catch (ArgumentNullException e) {
            if (!e.Message.Contains("null wstring"))
                throw new Exception("Missing text " + e);
        }

        x = "hello";
        check_equal(li_std_wstring.test_const_reference(x), x);

        /* Tricky, std::wstring portability problem.
         * std::wstring is 2 bytes on Windows, 4 bytes on Linux, LPWSTR is 2 bytes.
         */
        string ss = "abc";
        if (!li_std_wstring.test_equal_abc(ss))
            throw new Exception("Not equal " + ss);

        ss = "JP: 日本語";
        if (!li_std_wstring.test_equal_jp(ss))
            throw new Exception("Not equal " + ss);

        ss = "DE: Kröpeliner Straße";
        if (!li_std_wstring.test_equal_de(ss))
            throw new Exception("Not equal " + ss);

        ss = "RU: Война и мир";
        if (!li_std_wstring.test_equal_ru(ss))
            throw new Exception("Not equal " + ss);

        try {
            li_std_wstring.test_throw();
        } catch (Exception e) {
            check_equal(e.Message, "throwing test_throw");
        }

        try {
            li_std_wstring.test_throw_jp();
        } catch (Exception e) {
            check_equal(e.Message, "JP: 日本語");
        }

        try {
            li_std_wstring.test_throw_ref_jp();
        } catch (Exception e) {
            check_equal(e.Message, "JP: 日本語");
        }

        try {
            li_std_wstring.test_throw_wchar_t_ptr();
        } catch (Exception e) {
            check_equal(e.Message, "JP: 日本語");
        }

        x = "abc\0def";
        // Unlike other languages, embedded NULL in std::string not supported
        // check_equal(li_std_wstring.test_value(x), x);
        check_equal(li_std_wstring.test_value(x), "abc");
        check_equal(li_std_wstring.test_ccvalue(x), "abc");
        check_equal(li_std_wstring.test_wchar_overload(x), "abc");

        // Member variables
        var s = new wchar_test_struct();
        s.wchar_t_member = h;
        check_equal(s.wchar_t_member, h);
        s.wchar_t_ptr_member = x;
        check_equal(s.wchar_t_ptr_member, "abc");
        s.wchar_t_ptr_member = ss;
        check_equal(s.wchar_t_ptr_member, ss);

        {
            // Unicode strings
            // Strings below are UTF8 in this file, but .NET holds them internally as UTF16
            // DE: https://www.utf8-chartable.de/
            // RU: https://www.utf8-chartable.de/unicode-utf8-table.pl?start=1024
            string[] test_strings = {
                "JP: 日本語", "DE: Kröpeliner Straße" , "RU: Война и мир", "EN: War and Peace"
            };

            foreach (string expected in test_strings)
            {
                if (li_std_wstring.trace)
                    Console.WriteLine("expected (C#): " + expected);
                string received = li_std_wstring.test_value(expected);
                check_equal(received, expected);
            }

            foreach (string expected in test_strings)
            {
                string received = li_std_wstring.test_const_reference(expected);
                check_equal(received, expected);
            }

            foreach (string expected in test_strings)
            {
                string received = li_std_wstring.test_ccvalue(expected);
                check_equal(received, expected);
            }

            foreach (string expected in test_strings)
            {
                s.wchar_t_ptr_member = expected;
                string received = s.wchar_t_ptr_member;
                check_equal(received, expected);
            }

            /* Not working for Japanese and Russian characters on Windows, okay on Linux
             * Is fixed by adding CharSet=CharSet.Unicode to the DllImport, so change to:
             * [global::System.Runtime.InteropServices.DllImport("li_std_wstring", CharSet=global::System.Runtime.InteropServices.CharSet.Unicode, EntryPoint="CSharp_li_std_wstringNamespace_test_wcvalue")]
             * Needs a SWIG code change to support this
            foreach (string test_string in test_strings)
            {
                foreach (char expected in test_string)
                {
                    char received = li_std_wstring.test_wcvalue(expected);
                    check_equal(received, expected);
                }
            }
            */
        }
    }
}