Skip to content

Commit 03022b9

Browse files
committedDec 13, 2024
data creation algorithm is added
1 parent 80aeb49 commit 03022b9

File tree

1 file changed

+95
-27
lines changed

1 file changed

+95
-27
lines changed
 

‎test_js/main.ts

+95-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import LZString from "./node_modules/lz-string/libs/lz-string.min.js";
22

3+
const data_folder: string = "../test_src/data";
4+
35
enum CompressOption {
46
INVALID_UTF16 = "INVALID_UTF16",
57
VALID_UTF16 = "VALID_UTF16",
@@ -8,6 +10,39 @@ enum CompressOption {
810
UINT8ARRAY = "UINT8ARRAY",
911
}
1012

13+
function convert_to_Uint8Array(data: string): Uint8Array {
14+
const is_odd = data.charCodeAt(data.length - 1) % 256 === 0;
15+
16+
const buffer = new Uint8Array(data.length * 2 - (is_odd ? 1 : 0));
17+
18+
for (let i = 0; i < data.length; ++i) {
19+
const current_value = data.charCodeAt(i);
20+
21+
buffer[i * 2] = current_value >>> 8;
22+
23+
if (!is_odd || i < data.length - 1) {
24+
buffer[i * 2 + 1] = current_value % 256;
25+
}
26+
}
27+
28+
return buffer;
29+
}
30+
31+
function convert_from_Uint8Array(data: Uint8Array): string {
32+
const length = Math.floor(data.byteLength / 2);
33+
const arr = [];
34+
35+
for (let i = 0; i < length; ++i) {
36+
arr.push(String.fromCharCode(data[i * 2] * 256 + data[i * 2 + 1]));
37+
}
38+
39+
if (data.byteLength & 1) {
40+
arr.push(String.fromCharCode(data[data.byteLength - 1] * 256));
41+
}
42+
43+
return arr.join("");
44+
}
45+
1146
const hello_world = "Hello World !!!";
1247

1348
const all_ascii =
@@ -62,15 +97,26 @@ const temp_json_float = {
6297
},
6398
};
6499

65-
function get_file_info(
100+
function write_result(
66101
test_name: string,
67102
value: string,
68103
option: CompressOption,
69-
): string {
70-
let print_string = `${test_name}\n{`;
71-
104+
) {
72105
const char_codes: number[] = [];
73-
let result: string = "";
106+
const test_folder = `${data_folder}/${test_name}`;
107+
108+
try {
109+
Deno.mkdirSync(test_folder);
110+
} catch (error) {
111+
}
112+
113+
Deno.writeTextFileSync(`${test_folder}/data.bin`, value);
114+
115+
const test_filepath = `${test_folder}/${
116+
CompressOption[option].toLowerCase()
117+
}.bin`;
118+
119+
let result: string | Uint8Array;
74120

75121
switch (option) {
76122
case CompressOption.INVALID_UTF16:
@@ -85,47 +131,69 @@ function get_file_info(
85131
case CompressOption.URI:
86132
result = LZString.compressToEncodedURIComponent(value);
87133
break;
134+
case CompressOption.UINT8ARRAY:
135+
result = new Uint8Array(LZString.compressToUint8Array(value));
136+
137+
Deno.writeFileSync(
138+
test_filepath,
139+
result,
140+
);
141+
return;
88142
}
89143

90-
if (option !== CompressOption.UINT8ARRAY) {
144+
if (
145+
option === CompressOption.BASE64 ||
146+
option === CompressOption.URI
147+
) {
91148
for (let i = 0; i < result.length; ++i) {
92149
char_codes.push(result.charCodeAt(i));
93150
}
94151

95-
print_string += new Uint16Array(char_codes).join(", ");
96-
} else {
97-
print_string += new Uint8Array(LZString.compressToUint8Array(value)).join(
98-
", ",
152+
Deno.writeFileSync(
153+
test_filepath,
154+
new Uint8Array(char_codes),
99155
);
100-
}
101156

102-
print_string += "}";
157+
return;
158+
}
103159

104-
return print_string;
160+
const converted = convert_to_Uint8Array(result);
161+
Deno.writeFileSync(test_filepath, converted);
105162
}
106163

107164
if (import.meta.main) {
108165
const options: Set<string> = new Set<string>(Object.keys(CompressOption));
109166

110167
const text_strings: Map<string, string> = new Map<string, string>();
111-
text_strings.set("hello world", hello_world);
112-
text_strings.set("all ascii", all_ascii);
113-
text_strings.set("temp json", JSON.stringify(temp_json));
114-
text_strings.set("temp json float", JSON.stringify(temp_json_float));
115168

116-
options.forEach((option) => {
117-
const text: string[] = [];
169+
text_strings.set("hello_world", hello_world);
170+
text_strings.set("all_ascii", all_ascii);
171+
text_strings.set("temp_json", JSON.stringify(temp_json));
172+
text_strings.set("temp_json_float", JSON.stringify(temp_json_float));
173+
text_strings.set(
174+
"repeated",
175+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
176+
);
177+
text_strings.set(
178+
"pi",
179+
Deno.readTextFileSync(`${data_folder}/pi.bin`),
180+
);
181+
text_strings.set(
182+
"lorem_ipsum",
183+
Deno.readTextFileSync(`${data_folder}/lorem_ipsum.bin`),
184+
);
185+
text_strings.set(
186+
"tattoo",
187+
Deno.readTextFileSync(`${data_folder}/tattoo.bin`),
188+
);
118189

190+
options.forEach((option) => {
119191
text_strings.forEach((test, test_name) =>
120-
text.push(
121-
get_file_info(
122-
test_name,
123-
test,
124-
CompressOption[option as keyof typeof CompressOption],
125-
),
192+
write_result(
193+
test_name,
194+
test,
195+
CompressOption[option as keyof typeof CompressOption],
126196
)
127197
);
128-
129-
Deno.writeTextFileSync(`${option}.txt`, text.join("\n\n"));
130198
});
131199
}

0 commit comments

Comments
 (0)
Please sign in to comment.