-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.php
239 lines (205 loc) · 8.19 KB
/
test.php
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
// This file will have all the tests. First it will test the generated language files.
// print the length of the badWords and badPhrases arrays for each language
foreach (glob("./languages/*.php") as $filename) {
if (strpos($filename, "availableLanguages") !== false) {
continue;
}
$languageCode = explode(".", explode("/", $filename)[1])[0];
// include the file
include $filename;
// print the length of the arrays
echo "Language code: $languageCode\n";
echo "badWordsLength: " . count($badWords["badWords"]) . "\n";
echo "badPhrasesLength: " . $badPhrases["badPhrasesLength"] . "\n";
echo "badPhrasesLength: " . count($badPhrases["badPhrases"]) . "\n";
echo "\n\n\n";
// reset the variables using unset
unset($badWords);
unset($badPhrases);
}
// include all files in the languages directory
foreach (glob("./languages/*.php") as $filename) {
include $filename;
}
// also include the three letter language codes and countryToSupportedLanguageCodes
include "./countryToSupportedLanguageCodes.php";
// assertions for the language files
$assertions = [
'$badPhrases["badPhrasesLength"] === count($badPhrases["badPhrases"])',
'is_array($badWords)',
'is_array($badPhrases)',
'is_array($badWords["badWords"])',
'is_array($badPhrases["badPhrases"])',
'is_int($badPhrases["badPhrasesLength"])',
'is_array($availableLanguages)',
'!empty($availableLanguages)',
'is_array($countryToSupportedLanguageCodes)',
'!empty($countryToSupportedLanguageCodes)',
];
foreach ($assertions as $assertion) {
try {
assert(eval("return $assertion;"));
if (mb_strpos("===", $assertion) !== false) {
list($left, $right) = explode('===', $assertion);
// Evaluate each part
$leftValue = eval("return $left;");
$rightValue = eval("return $right;");
// Print the values
echo "✅ Assertion passed: $assertion. $left: $leftValue, $right: $rightValue\n";
} else {
echo "✅ Assertion passed: $assertion\n";
}
} catch (AssertionError $e) {
// Split the assertion into two parts
if (mb_strpos("===", $assertion) !== false) {
list($left, $right) = explode('===', $assertion);
// Evaluate each part
$leftValue = eval("return $left;");
$rightValue = eval("return $right;");
// Print the values
echo "❌ Assertion failed: $assertion. $left: $leftValue, $right: $rightValue\n";
} else {
echo "❌ Assertion failed: $assertion\n";
}
}
}
$assertions = [
// check if all the function are set
'function_exists("getSupportedLanguages")',
'function_exists("getSupportedLanguageCode")',
'function_exists("countryCodeToLanguage")',
'function_exists("getHttpAcceptLanguages")',
'function_exists("isProfanity")',
'function_exists("boyerMooreSearch")',
'function_exists("is_profanity")',
// check if the global variables are set
'isset($supportedLanguages)',
'isset($availableLanguages)',
];
// Check Profanity Filter for different languages
$parameters = array();
$parameter = array(
'text' => '',
'detectedLanguages' => [],
'exploded' => [],
'explodedLength' => 0,
'all' => false,
);
$returnValue = false;
$gibberishSentence = "Lorem ffug fiusagfiu siufguas uisfusaf ugisgfias gfsugf aius sfiasgfiuasgfi isfgiaufgas iufgaiusgfas asiufgsufga fssfsguf asfuisgf";
$gibberishSentenceArray = explode(" ", $gibberishSentence);
// Increase the length of the gibberish sentence
for ($i = 0; $i < 10; $i++) {
$gibberishSentenceArray = array_merge($gibberishSentenceArray, $gibberishSentenceArray);
}
// logic to generate the True tests
for ($i = 0; $i <= 50; $i++) {
$badWord = array_rand($badWords["badWords"]);
$randomPosition = mt_rand(0, count($gibberishSentenceArray) - 1);
$modifiedSentence = implode(" ", array_merge(array_slice($gibberishSentenceArray, 0, $randomPosition), array($badWord), array_slice($gibberishSentenceArray, $randomPosition)));
$parameter['text'] = $modifiedSentence;
$parameter['detectedLanguages'] = [];
$parameter['exploded'] = [];
$parameter['explodedLength'] = 0;
$parameter['all'] = true;
$parameters[] = array(
'test' => $parameter,
'returnValue' => true,
);
$randomBadPhrase = $badPhrases["badPhrases"][mt_rand(0, $badPhrases["badPhrasesLength"] - 1)]["badPhrase"];
$modifiedSentence = implode(" ", array_merge(array_slice($gibberishSentenceArray, 0, $randomPosition), $randomBadPhrase, array_slice($gibberishSentenceArray, $randomPosition)));
$parameter['text'] = $modifiedSentence;
$parameters[] = array(
'test' => $parameter,
'returnValue' => true,
);
}
// one false test using gibberish sentence
$parameter['text'] = $gibberishSentence;
$parameter['detectedLanguages'] = [];
$parameter['exploded'] = [];
$parameter['explodedLength'] = 0;
$parameter['all'] = true;
$parameters[] = array(
'test' => $parameter,
'returnValue' => false,
);
foreach ($parameters as $key => $parameter) {
$assertions[] = 'isProfanity($parameters[' . $key . ']["test"]["text"], $parameters[' . $key . ']["test"]["detectedLanguages"], $parameters[' . $key . ']["test"]["exploded"], $parameters[' . $key . ']["test"]["explodedLength"], $parameters[' . $key . ']["test"]["all"]) === $parameters[' . $key . ']["returnValue"]';
}
// reset included variables
unset($badPhrases);
unset($badWords);
unset($availableLanguages);
unset($countryToSupportedLanguageCodes);
// Include the main.php and test it's functions
// include "./main.php";
// var_dump($assertions);
foreach ($assertions as $assertion) {
try {
$start = microtime(true);
assert(eval("global \$parameters, \$availableLanguages, \$supportedLanguages; include_once './main.php' ; return $assertion;"));
$end = microtime(true);
// append to the time file
$file = fopen("./time.txt", "a");
fwrite($file, $end - $start . "\n");
fclose($file);
if (mb_strpos("===", $assertion) !== false) {
list($left, $right) = explode('===', $assertion);
// Evaluate each part
$leftValue = eval("global \$parameters, \$availableLanguages, \$supportedLanguages; return $left;");
$rightValue = eval("global \$parameters, \$availableLanguages, \$supportedLanguages; return $right;");
// Print the values
echo "✅ Assertion passed: $assertion. $left: $leftValue, $right: $rightValue\n";
} else {
echo "✅ Assertion passed: $assertion\n";
}
} catch (AssertionError $e) {
$end = microtime(true);
// append to the time file
$file = fopen("./time.txt", "a");
fwrite($file, floatval($end - $start) . "\n");
fclose($file);
// Split the assertion into two parts
if (mb_strpos("===", $assertion) !== false) {
list($left, $right) = explode('===', $assertion);
// Evaluate each part
$leftValue = eval("global \$parameters, \$availableLanguages, \$supportedLanguages; return $left;");
$rightValue = eval("global \$parameters, \$availableLanguages, \$supportedLanguages; return $right;");
// Print the values
echo "❌ Assertion failed: $assertion. $left: $leftValue, $right: $rightValue\n";
} else {
echo "❌ Assertion failed: $assertion\n";
}
}
}
$file = fopen("./time.txt", "r");
if (!$file) {
echo "Error opening file";
exit;
}
$times = array();
while (!feof($file)) {
$times[] = floatval(fgets($file));
}
fclose($file); // Close the file after reading
// Calculate mean and median in ms
$mean = array_sum($times) / count($times);
sort($times);
$median = $times[round((count($times) - 1) / 2)];
// max time taken
$max = max($times);
// p95
$p95 = $times[round(count($times) * 0.95)];
// p99
$p99 = $times[round(count($times) * 0.99)];
// Reopen the file in append mode
$file = fopen("./time.txt", "a");
if (!$file) {
echo "Error opening file";
exit;
}
// Append it to the time.txt file
fwrite($file, "\nMean: $mean\nMedian: $median\nMax: $max\np95: $p95\np99: $p99\n");
fclose($file);