forked from CloverHackyColor/CloverBootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrcmp_test.cpp
More file actions
executable file
·167 lines (140 loc) · 4.1 KB
/
Copy pathstrcmp_test.cpp
File metadata and controls
executable file
·167 lines (140 loc) · 4.1 KB
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
#include <Platform.h> // Only use angled for Platform, else, xcode project won't compile
#include "../cpp_foundation/XString.h"
#include "../cpp_foundation/unicode_conversions.h"
#include "global_test.h"
static float rndf() //expected 0..1
{
static UINT32 seed = 12345;
// UINT16 Rand = 0;
// AsmRdRand16(&Rand); //it's a pity panic
// return (float)Rand / 65536.f;
seed = seed * 214013 + 2531011;
float x = (float)seed / 4294967296.0f;
return x;
}
static int sign(int ret) {
if ( ret < 0 ) return -1;
if ( ret > 0 ) return 1;
return 0;
}
//#include <wchar.h>
static int strcmp_reference(const char *p1, const char *p2)
{
const unsigned char *s1 = (const unsigned char *) p1;
const unsigned char *s2 = (const unsigned char *) p2;
unsigned char c1, c2;
do
{
c1 = (unsigned char) *s1++;
c2 = (unsigned char) *s2++;
if (c1 == '\0')
return c1 - c2;
}
while (c1 == c2);
return c1 - c2;
}
static int compare(const char*s1, const char*s2)
{
int ret1 = strcmp(s1, s2);
int ret2 = strcmp_reference(s1, s2);
if ( sign(ret1) != sign(ret2) ) {
printf("Comparing '%s' and '%s' gives %d and should have given %d\n", s1, s2, ret1, ret2);
return 1; // whatever if not 0
}
return 0;
}
static int symetric_compare(const char*s1, const char*s2, int code)
{
int ret;
ret = compare(s1, s1);
if ( ret != 0 ) return code;
ret = compare(s2, s2);
if ( ret != 0 ) return code;
ret = compare(s1, s2);
if ( ret != 0 ) return code;
ret = compare(s2, s1);
if ( ret != 0 ) return code;
ret = compare(s1, "");
if ( ret != 0 ) return code;
ret = compare("", s1);
if ( ret != 0 ) return code;
ret = compare(s2, "");
if ( ret != 0 ) return code;
ret = compare("", s2);
if ( ret != 0 ) return code;
return 0;
}
// ATENTION : s must count+1 in size
static void fillRandom(char* s, size_t count)
{
size_t i;
for ( i = 0 ; i<count ; i++ ) { // not using strcpy to not depend of another test.
((unsigned char *)s)[i] = (unsigned char)(rndf()*255.0);
}
s[i] = 0;
}
#define NB_ITERATIONS 515
static int compare_s1_with_variable_sizes(const char* s1, int code)
{
char s2[515];
for ( size_t i=0 ; i < NB_ITERATIONS ; i++ )
{
size_t count = (size_t)(rndf()*sizeof(s2)-1);
if ( count >= sizeof(s2) ) {
printf("compare_s1_with_variable_sizes, BUG : sizeof=%zu, count=%zu\n", sizeof(s2), count);
continue;
}
fillRandom(s2, count);
int ret = symetric_compare(s2, s1, code+(int)i); // s2 is always 'superior'
if ( ret != 0 ) return ret;
}
return 0;
}
static int compare_with_variable_sizes(int code)
{
unsigned char s1[514];
for ( size_t i=0 ; i < sizeof(s1) ; i++ ) {
size_t j=0;
for ( j = 0 ; j < sizeof(s1) && j < i ; j++ ) { // not using strcpy to not depend of another test.
s1[j] = (unsigned char)(rndf()*255.0);
}
s1[i] = 0;
int ret = compare_s1_with_variable_sizes((const char*)s1, code+(int)i);
if ( ret != 0 ) return ret;
}
return 0;
}
int strcmp_tests()
{
int ret;
// Efficient version of strcmp may do with blocks
// So there maybe a bug when some string has some specific length
ret = symetric_compare("", "z", 1);
if ( ret != 0 ) return ret;
ret = symetric_compare("a", "b", 10);
if ( ret != 0 ) return ret;
ret = symetric_compare("a", "c", 20);
if ( ret != 0 ) return ret;
ret = symetric_compare("aa", "ad", 30);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaa", "aae", 40);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaa", "aaaf", 50);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaaa", "aaaag", 60);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaaaa", "aaaaaah", 70);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaaaaa", "aaaaaaai", 80);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaaaaaa", "aaaaaaaaj", 90);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaaaaaaa", "aaaaaaaaak", 100);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaaaaaaaa", "aaaaaaaaaal", 110);
if ( ret != 0 ) return ret;
ret = symetric_compare("aaaabbbbccccddddeeeeffffA", "aaaabbbbccccddddeeeeffffM", 120);
if ( ret != 0 ) return ret;
ret = compare_with_variable_sizes(1000000);
return 0;
}