-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathstd.ts
448 lines (392 loc) · 11.7 KB
/
std.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import * as assert from 'assert';
import { describe, test } from 'vitest';
import { utils } from '../src';
import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value';
import { exe, eq } from './testutils';
describe('Core', () => {
test.concurrent('range', async () => {
eq(await exe('<: Core:range(1, 10)'), ARR([NUM(1), NUM(2), NUM(3), NUM(4), NUM(5), NUM(6), NUM(7), NUM(8), NUM(9), NUM(10)]));
eq(await exe('<: Core:range(1, 1)'), ARR([NUM(1)]));
eq(await exe('<: Core:range(9, 7)'), ARR([NUM(9), NUM(8), NUM(7)]));
});
test.concurrent('to_str', async () => {
eq(await exe('<: Core:to_str("abc")'), STR('abc'));
eq(await exe('<: Core:to_str(123)'), STR('123'));
eq(await exe('<: Core:to_str(true)'), STR('true'));
eq(await exe('<: Core:to_str(false)'), STR('false'));
eq(await exe('<: Core:to_str(null)'), STR('null'));
eq(await exe('<: Core:to_str({ a: "abc", b: 1234 })'), STR('{ a: "abc", b: 1234 }'));
eq(await exe('<: Core:to_str([ true, 123, null ])'), STR('[ true, 123, null ]'));
eq(await exe('<: Core:to_str(@( a, b, c ) {})'), STR('@( a, b, c ) { ... }'));
eq(await exe(`
let arr = []
arr.push(arr)
<: Core:to_str(arr)
`), STR('[ ... ]'));
eq(await exe(`
let arr = []
arr.push({ value: arr })
<: Core:to_str(arr)
`), STR('[ { value: ... } ]'));
});
test.concurrent('abort', async () => {
assert.rejects(
exe('Core:abort("hoge")'),
e => e.message.includes('hoge'),
);
});
});
describe('Arr', () => {
test.concurrent('create', async () => {
eq(await exe("<: Arr:create(0)"), ARR([]));
eq(await exe("<: Arr:create(3)"), ARR([NULL, NULL, NULL]));
eq(await exe("<: Arr:create(3, 1)"), ARR([NUM(1), NUM(1), NUM(1)]));
});
});
describe('Math', () => {
test.concurrent('trig', async () => {
eq(await exe("<: Math:sin(Math:PI / 2)"), NUM(1));
eq(await exe("<: Math:sin(0 - (Math:PI / 2))"), NUM(-1));
eq(await exe("<: Math:sin(Math:PI / 4) * Math:cos(Math:PI / 4)"), NUM(0.5));
});
test.concurrent('abs', async () => {
eq(await exe("<: Math:abs(1 - 6)"), NUM(5));
});
test.concurrent('pow and sqrt', async () => {
eq(await exe("<: Math:sqrt(3^2 + 4^2)"), NUM(5));
});
test.concurrent('round', async () => {
eq(await exe("<: Math:round(3.14)"), NUM(3));
eq(await exe("<: Math:round(-1.414213)"), NUM(-1));
});
test.concurrent('ceil', async () => {
eq(await exe("<: Math:ceil(2.71828)"), NUM(3));
eq(await exe("<: Math:ceil(0 - Math:PI)"), NUM(-3));
eq(await exe("<: Math:ceil(1 / Math:Infinity)"), NUM(0));
});
test.concurrent('floor', async () => {
eq(await exe("<: Math:floor(23.14069)"), NUM(23));
eq(await exe("<: Math:floor(Math:Infinity / 0)"), NUM(Infinity));
});
test.concurrent('min', async () => {
eq(await exe("<: Math:min(2, 3)"), NUM(2));
});
test.concurrent('max', async () => {
eq(await exe("<: Math:max(-2, -3)"), NUM(-2));
});
/* flaky
test.concurrent('rnd', async () => {
const steps = 512;
const res = await exe(`
let counts = [] // 0 ~ 10 の出現回数を格納する配列
for (11) {
counts.push(0) // 初期化
}
for (${steps}) {
let rnd = Math:rnd(0 10) // 0 以上 10 以下の整数乱数
counts[rnd] = counts[rnd] + 1
}
<: counts`);
function chiSquareTest(observed: number[], expected: number[]) {
let chiSquare = 0; // カイ二乗値
for (let i = 0; i < observed.length; i++) {
chiSquare += Math.pow(observed[i] - expected[i], 2) / expected[i];
}
return chiSquare;
}
let observed: Array<number> = [];
for (let i = 0; i < res.value.length; i++) {
observed.push(res.value[i].value);
}
let expected = new Array(11).fill(steps / 10);
let chiSquare = chiSquareTest(observed, expected);
// 自由度が (11 - 1) の母分散の カイ二乗分布 95% 信頼区間は [3.94, 18.31]
assert.deepEqual(3.94 <= chiSquare && chiSquare <= 18.31, true, `カイ二乗値(${chiSquare})が母分散の95%信頼区間にありません`);
});
*/
test.concurrent('rnd with arg', async () => {
eq(await exe("<: Math:rnd(1, 1.5)"), NUM(1));
});
test.concurrent('gen_rng', async () => {
// 2つのシード値から1~maxの乱数をn回生成して一致率を見る
const res = await exe(`
@test(seed1, seed2) {
let n = 100
let max = 100000
let threshold = 0.05
let random1 = Math:gen_rng(seed1)
let random2 = Math:gen_rng(seed2)
var same = 0
for n {
if random1(1, max) == random2(1, max) {
same += 1
}
}
let rate = same / n
if seed1 == seed2 { rate == 1 }
else { rate < threshold }
}
let seed1 = \`{Util:uuid()}\`
let seed2 = \`{Date:year()}\`
<: [
test(seed1, seed1)
test(seed1, seed2)
]
`)
eq(res, ARR([BOOL(true), BOOL(true)]));
});
});
describe('Obj', () => {
test.concurrent('keys', async () => {
const res = await exe(`
let o = { a: 1, b: 2, c: 3, }
<: Obj:keys(o)
`);
eq(res, ARR([STR('a'), STR('b'), STR('c')]));
});
test.concurrent('vals', async () => {
const res = await exe(`
let o = { _nul: null, _num: 24, _str: 'hoge', _arr: [], _obj: {}, }
<: Obj:vals(o)
`);
eq(res, ARR([NULL, NUM(24), STR('hoge'), ARR([]), OBJ(new Map([]))]));
});
test.concurrent('kvs', async () => {
const res = await exe(`
let o = { a: 1, b: 2, c: 3, }
<: Obj:kvs(o)
`);
eq(res, ARR([
ARR([STR('a'), NUM(1)]),
ARR([STR('b'), NUM(2)]),
ARR([STR('c'), NUM(3)])
]));
});
test.concurrent('merge', async () => {
const res = await exe(`
let o1 = { a: 1, b: 2 }
let o2 = { b: 3, c: 4 }
<: Obj:merge(o1, o2)
`);
eq(res, utils.jsToVal({ a: 1, b: 3, c: 4}));
});
test.concurrent('pick', async () => {
const res = await exe(`
let o = { a: 1, b: 2, c: 3 }
<: Obj:pick(o, ['b', 'd'])
`);
eq(res, utils.jsToVal({ b: 2, d: null }));
});
});
describe('Str', () => {
test.concurrent('lf', async () => {
const res = await exe(`
<: Str:lf
`);
eq(res, STR('\n'));
});
test.concurrent('from_codepoint', async () => {
const res = await exe(`
<: Str:from_codepoint(65)
`);
eq(res, STR('A'));
});
test.concurrent('from_unicode_codepoints', async () => {
const res = await exe(`
<: Str:from_unicode_codepoints([171581, 128073, 127999, 128104, 8205, 128102])
`);
eq(res, STR('𩸽👉🏿👨👦'));
});
test.concurrent('from_utf8_bytes', async () => {
const res = await exe(`
<: Str:from_utf8_bytes([240, 169, 184, 189, 240, 159, 145, 137, 240, 159, 143, 191, 240, 159, 145, 168, 226, 128, 141, 240, 159, 145, 166])
`);
eq(res, STR('𩸽👉🏿👨👦'));
});
test.concurrent('charcode_at', async () => {
let res = await exe(`
<: "aiscript".split().map(@(x, _) { x.charcode_at(0) })
`);
eq(res, ARR([97, 105, 115, 99, 114, 105, 112, 116].map(x => NUM(x))));
res = await exe(`
<: "".charcode_at(0)
`);
eq(res, NULL);
});
});
describe('Uri', () => {
test.concurrent('encode_full', async () => {
const res = await exe(`
<: Uri:encode_full("https://example.com/?q=あいちゃん")
`);
eq(res, STR('https://example.com/?q=%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93'));
});
test.concurrent('encode_component', async () => {
const res = await exe(`
<: Uri:encode_component("https://example.com/?q=あいちゃん")
`);
eq(res, STR('https%3A%2F%2Fexample.com%2F%3Fq%3D%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93'));
});
test.concurrent('decode_full', async () => {
const res = await exe(`
<: Uri:decode_full("https%3A%2F%2Fexample.com%2F%3Fq%3D%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93")
`);
eq(res, STR('https%3A%2F%2Fexample.com%2F%3Fq%3Dあいちゃん'));
});
test.concurrent('decode_component', async () => {
const res = await exe(`
<: Uri:decode_component("https%3A%2F%2Fexample.com%2F%3Fq%3D%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93")
`);
eq(res, STR('https://example.com/?q=あいちゃん'));
});
});
describe('Error', () => {
test.concurrent('create', async () => {
eq(
await exe(`
<: Error:create('ai', {chan: 'kawaii'})
`),
ERROR('ai', OBJ(new Map([['chan', STR('kawaii')]])))
);
});
});
describe('Json', () => {
test.concurrent('stringify: fn', async () => {
const res = await exe(`
<: Json:stringify(@(){})
`);
eq(res, STR('"<function>"'));
});
test.concurrent('parsable', async () => {
[
'null',
'"hoge"',
'[]',
'{}',
].forEach(async (str) => {
const res = await exe(`
<: [
Json:parsable('${str}')
Json:stringify(Json:parse('${str}'))
]
`);
eq(res, ARR([TRUE, STR(str)]));
});
});
test.concurrent('unparsable', async () => {
[
'',
'hoge',
'[',
].forEach(async (str) => {
const res = await exe(`
<: [
Json:parsable('${str}')
Json:parse('${str}')
]
`);
eq(res, ARR([FALSE, ERROR('not_json')]));
});
});
});
describe('Date', () => {
const example_time = new Date(2024, 1 - 1, 2, 3, 4, 5, 6).getTime();
const zero_date = new Date(0);
test.concurrent('year', async () => {
const res = await exe(`
<: [Date:year(0), Date:year(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getFullYear()), NUM(2024)]));
});
test.concurrent('month', async () => {
const res = await exe(`
<: [Date:month(0), Date:month(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getMonth() + 1), NUM(1)]));
});
test.concurrent('day', async () => {
const res = await exe(`
<: [Date:day(0), Date:day(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getDate()), NUM(2)]));
});
test.concurrent('hour', async () => {
const res = await exe(`
<: [Date:hour(0), Date:hour(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getHours()), NUM(3)]));
});
test.concurrent('minute', async () => {
const res = await exe(`
<: [Date:minute(0), Date:minute(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getMinutes()), NUM(4)]));
});
test.concurrent('second', async () => {
const res = await exe(`
<: [Date:second(0), Date:second(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getSeconds()), NUM(5)]));
});
test.concurrent('millisecond', async () => {
const res = await exe(`
<: [Date:millisecond(0), Date:millisecond(${example_time})]
`);
eq(res, ARR([NUM(zero_date.getMilliseconds()), NUM(6)]));
});
test.concurrent('to_iso_str', async () => {
const res = await exe(`
let d1 = Date:parse("2024-04-12T01:47:46.021+09:00")
let s1 = Date:to_iso_str(d1)
let d2 = Date:parse(s1)
<: [d1, d2, s1]
`);
eq(res.value[0], res.value[1]);
assert.match(res.value[2].value, /^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}T[0-9]{2,2}:[0-9]{2,2}:[0-9]{2,2}\.[0-9]{3,3}(Z|[-+][0-9]{2,2}:[0-9]{2,2})$/);
});
test.concurrent('to_iso_str (UTC)', async () => {
const res = await exe(`
let d1 = Date:parse("2024-04-12T01:47:46.021+09:00")
let s1 = Date:to_iso_str(d1, 0)
let d2 = Date:parse(s1)
<: [d1, d2, s1]
`);
eq(res.value[0], res.value[1]);
eq(res.value[2], STR("2024-04-11T16:47:46.021Z"));
});
test.concurrent('to_iso_str (+09:00)', async () => {
const res = await exe(`
let d1 = Date:parse("2024-04-12T01:47:46.021+09:00")
let s1 = Date:to_iso_str(d1, 9*60)
let d2 = Date:parse(s1)
<: [d1, d2, s1]
`);
eq(res.value[0], res.value[1]);
eq(res.value[2], STR("2024-04-12T01:47:46.021+09:00"));
});
test.concurrent('to_iso_str (-05:18)', async () => {
const res = await exe(`
let d1 = Date:parse("2024-04-12T01:47:46.021+09:00")
let s1 = Date:to_iso_str(d1, -5*60-18)
let d2 = Date:parse(s1)
<: [d1, d2, s1]
`);
eq(res.value[0], res.value[1]);
eq(res.value[2], STR("2024-04-11T11:29:46.021-05:18"));
});
test.concurrent('parse', async () => {
eq(await exe(`<: [
'01 Jan 1970 00:00:00 GMT'
'1970-01-01'
'1970-01-01T00:00:00.000Z'
'1970-01-01T00:00:00.000+00:00'
'hoge'
].map(Date:parse)`), ARR([
NUM(0),
NUM(0),
NUM(0),
NUM(0),
ERROR('not_date')
]));
});
});