-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
141 lines (115 loc) · 4.58 KB
/
test.c
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
/* Библиотека на языке C для конвертации строк из кодировки UTF-8 в CP1251
* Адрес проекта: https://github.com/bravikov/utf8_to_cp1251
* */
#include "utf8_to_cp1251.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
const char * name;
const char * utf8;
const char * cp1251;
const Cp1251 * customTable;
const size_t customTableSize;
} TestData;
int main(void) {
static const Cp1251 cp1251Table[] = {
{0x98, 0x03C6}, /* φ, GREEK SMALL LETTER PHI */
{0x80, 0x2211}, /* ∑, N-ARY SUMMATION */
{0x81, 0x25BA}, /* ►, BLACK RIGHT-POINTING POINTER */
};
size_t i;
const TestData testStrings[] = {
{
"Пустая строка",
"",
"",
0, 0
},
{
"US-ASCII",
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F !\""
"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`a"
"bcdefghijklmnopqrstuvwxyz{|}~\x7F",
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20"
"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30"
"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F\x40"
"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50"
"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60"
"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F\x70"
"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F",
0, 0
},
{
"Кириллица",
"АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя",
"\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF"
"\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF"
"\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF"
"\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF",
0, 0
},
{
"Cимволы разных размеров",
"яzяzя",
"\xFF\x7A\xFF\x7A\xFF",
0, 0
},
{
"Символы CP1251 0x80...0xBF",
"ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏ"
"ђ‘’“”•–—™љ›њќћџ"
"\xC2\xA0ЎўЈ¤Ґ¦§Ё©Є«¬\xC2\xAD®Ї"
"°±Ііґµ¶·ё№є»јЅѕї",
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
"\x90\x91\x92\x93\x94\x95\x96\x97" "\x99\x9A\x9B\x9C\x9D\x9E\x9F"
"\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
"\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF",
0, 0
},
{
"Пользовательская таблица",
"φ∑►±эюя",
"\x98\x80\x81\xB1\xFD\xFE\xFF",
cp1251Table, sizeof(cp1251Table) / sizeof(Cp1251)
}
/*{
"Шаблон",
"",
"",
0,
0
}*/
};
const size_t testCount = sizeof(testStrings) / sizeof(TestData);
for (i = 0; i < testCount; i++) {
const TestData test = testStrings[i];
if (test.customTable != 0) {
setCustomCp1251Table(test.customTable, test.customTableSize);
}
const int cp1251Size = strlen(test.cp1251) + 1;
char * cp1251 = malloc(cp1251Size);
const int r1 = convertUtf8ToCp1251(test.utf8, cp1251);
const int r2 = strcmp(cp1251, test.cp1251);
char * result = "ПРОВАЛЕН :(";
const bool success = (r1 == cp1251Size - 1) && (r2 == 0);
if (success) {
result = "пройден :)";
}
printf("Тест № %zi \"%s\": %s\n", i, test.name, result);
if (success == false) {
printf("\"Эталон\" | \"Результат преобразования\"\n");
for (i = 0; i < strlen(test.cp1251) && i < strlen(cp1251); i++) {
printf("%02hhX | %02hhX\n", test.cp1251[i], cp1251[i]);
}
}
free(cp1251);
if (success == false) {
break;
}
}
return 0;
}