This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.c
85 lines (75 loc) · 2.07 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
#define CUTF_IMPLEMENTATION 1
#include "cutf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
uint8_t* readfile(const char* path, size_t* size)
{
FILE* file = fopen(path, "rb");
uint8_t* result = 0;
if(file) {
size_t file_size = 0;
fseek(file, 0L, SEEK_END);
file_size = ftell(file);
fseek(file, 0L, SEEK_SET);
result = (uint8_t*)calloc(file_size, sizeof(uint8_t));
*size = fread(result, sizeof(uint8_t), file_size, file);
fclose(file);
}
return result;
}
void check(const char* name, uint8_t* original, uint8_t* copy, size_t size)
{
printf(" %s: %s\n", name, memcmp(original, copy, size) == 0 ? "pass" : "fail");
}
void roundtrip_16(uint8_t* original, uint8_t* copy, size_t size, size_t count)
{
size_t written;
uint16_t* temp = calloc(count*2, sizeof(uint16_t));
written = cutf_8to16(original, original+size, temp);
written = cutf_16to8(temp, temp+written, copy);
free(temp);
check("roundtrip_16", original, copy, size);
}
void roundtrip_32(uint8_t* original, uint8_t* copy, size_t size, size_t count)
{
size_t written;
uint32_t* temp = calloc(count, sizeof(uint32_t));
written = cutf_8to32(original, original+size, temp);
written = cutf_32to8(temp, temp+written, copy);
free(temp);
check("roundtrip_32", original, copy, size);
}
void testfile(const char* path)
{
size_t size;
uint8_t* original = readfile(path, &size);
printf("%s:\n", path);
if(original) {
uint8_t* copy = calloc(size*2, sizeof(uint8_t));
size_t count;
if(!cutf_is_valid(original, original+size)) {
cutf_default_replace_invalid(original, original+size, copy, size);
memcpy(original, copy, size);
printf(" invalid\n", path);
}
count = cutf_distance(original, original+size);
printf(" # of cp: %d\n", count);
if(count) {
roundtrip_16(original, copy, size, count);
roundtrip_32(original, copy, size, count);
}
free(copy);
free(original);
} else {
printf(" file read failed\n", path);
}
}
int main(int argc, char** argv)
{
testfile("utf8_invalid.txt");
testfile("quickbrown.txt");
testfile("UTF-8-demo.txt");
testfile("big.txt");
return 0;
}