android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / test / src / TestUTF8.cpp
blob541e32af44ca20156444f8ade940b6e7e9e538de
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Util/UTF8.hpp"
24 #include "Util/Macros.hpp"
25 #include "TestUtil.hpp"
27 #include <string.h>
29 static const char *const valid[] = {
30 "",
31 " ",
32 "foo",
33 "\x01",
34 "\x7f",
35 "\xc3\xbc",
36 "\xd7\x9e",
37 "\xe7\x9b\xae",
40 static const char *const invalid[] = {
41 "\x80",
42 "\xff",
43 "\xc3",
44 "\xd7",
45 "\xe7",
46 "\xe7\x9b",
49 static const struct {
50 const char *value;
51 size_t length;
52 } length[] = {
53 { "", 0 },
54 { " ", 1 },
55 { "foo", 3 },
56 { "\xc3\xbc", 1 },
57 { "\xd7\x9e", 1 },
58 { "\xe7\x9b\xae", 1 },
61 static const struct {
62 char ch;
63 const char *utf8;
64 } latin1_chars[] = {
65 { 0, "" },
66 { ' ', " " },
67 { '\xfc', "\xc3\xbc", },
70 static const struct {
71 const char *input, *output;
72 } crop[] = {
73 { "", "" },
74 { " ", " " },
75 { "foo\xc3\xbc", "foo\xc3\xbc", },
76 { "foo\xc3", "foo", },
77 { "foo\xe7\x9b\xae", "foo\xe7\x9b\xae", },
78 { "foo\xe7\x9b", "foo", },
79 { "foo\xe7", "foo", },
82 #include <stdio.h>
83 int main(int argc, char **argv)
85 plan_tests(ARRAY_SIZE(valid) + ARRAY_SIZE(invalid) +
86 ARRAY_SIZE(length) +
87 ARRAY_SIZE(crop) +
88 ARRAY_SIZE(latin1_chars));
90 for (auto i : valid)
91 ok1(ValidateUTF8(i));
93 for (auto i : invalid)
94 ok1(!ValidateUTF8(i));
96 for (auto &l : length)
97 ok1(l.length == LengthUTF8(l.value));
99 char buffer[64];
101 for (auto &l : latin1_chars) {
102 *Latin1ToUTF8(l.ch, buffer) = 0;
103 ok1(strcmp(l.utf8, buffer) == 0);
106 for (auto &c : crop) {
107 strcpy(buffer, c.input);
108 CropIncompleteUTF8(buffer);
109 ok1(strcmp(c.output, buffer) == 0);
112 return exit_status();