Skip to content
This repository was archived by the owner on Aug 5, 2023. It is now read-only.

Commit 7297ff1

Browse files
committed
add the examples, added in the UI, to the main.spec
1 parent 6531bc2 commit 7297ff1

File tree

1 file changed

+326
-1
lines changed

1 file changed

+326
-1
lines changed

tests/main.spec.ts

Lines changed: 326 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ display fibNums
6161
expect(main.getOutput()).toStrictEqual('0,1,1,2,3,5,8,13,21,34,55,89\n');
6262
});
6363

64-
test('should check for primality.', () => {
64+
test('should print prime numbers.', () => {
6565
const code = `
6666
6767
@@ -146,4 +146,329 @@ loop while num <= 50:
146146
'50: false\n');
147147
});
148148

149+
test('should find the factorial of numbers.', () => {
150+
const code = `
151+
define function factorial with arguments [ number n ] which returns number:
152+
\tif n == 0 or n == 1, then do:
153+
\t\treturn n
154+
\t
155+
\tlet number factNMinus1 be result of factorial(n - 1)
156+
\treturn n * factNMinus1
157+
158+
\t\t
159+
let number num be 1
160+
loop while num <=10:
161+
\tlet number res be result of factorial(num)
162+
\tdisplay num, ' factorial is ', res
163+
\tset num to num + 1
164+
165+
`;
166+
const main = new Main(code);
167+
main.compile(new Scope());
168+
169+
expect(main.getOutput()).toStrictEqual('1 factorial is 1\n' +
170+
'2 factorial is 2\n' +
171+
'3 factorial is 6\n' +
172+
'4 factorial is 24\n' +
173+
'5 factorial is 120\n' +
174+
'6 factorial is 720\n' +
175+
'7 factorial is 5040\n' +
176+
'8 factorial is 40320\n' +
177+
'9 factorial is 362880\n' +
178+
'10 factorial is 3628800\n');
179+
});
180+
181+
test('should sort the array of numbers.', () => {
182+
const code = `
183+
define function bubbleSort with arguments [ array of number arr ] which returns array of number:
184+
\tlet number i be 0
185+
\tlet number arrLen be length of arr
186+
\tloop while i < arrLen:
187+
\t\tlet number j be 0
188+
\t\tloop while j+1 < arrLen:
189+
\t\t\tif arr[j+1] < arr[j], then do:
190+
\t\t\t\tlet number temp be arr[j]
191+
\t\t\t\tset arr[j] to arr[j+1]
192+
\t\t\t\tset arr[j+1] to temp
193+
\t\t\tset j to j + 1
194+
\t\tset i to i + 1
195+
\treturn arr
196+
197+
let array of number, myNumberArray, be [ 34, 42, 12, 45, 64, 22, 21, 43, 2, 23, 1, 0]
198+
let array of number, sortedArray, be result of bubbleSort(myNumberArray)
199+
display sortedArray
200+
201+
`;
202+
// [ 34, 42, 12, 45, 64, 22, 21, 43, 2, 23, 1, 0]
203+
const main = new Main(code);
204+
main.compile(new Scope());
205+
206+
expect(main.getOutput()).toStrictEqual('0,1,2,12,21,22,23,34,42,43,45,64\n');
207+
});
208+
209+
test('should evaluate nested loop.', () => {
210+
const code = `
211+
loop for 5 times with i as counter:
212+
\tloop for 6 times with j as counter:
213+
\t\tloop for 7 times with k as counter:
214+
\t\t\tdisplay i, ', ', j, ', ', k
215+
`;
216+
217+
const main = new Main(code);
218+
main.compile(new Scope());
219+
220+
expect(main.getOutput()).toStrictEqual('1, 1, 1\n' +
221+
'1, 1, 2\n' +
222+
'1, 1, 3\n' +
223+
'1, 1, 4\n' +
224+
'1, 1, 5\n' +
225+
'1, 1, 6\n' +
226+
'1, 1, 7\n' +
227+
'1, 2, 1\n' +
228+
'1, 2, 2\n' +
229+
'1, 2, 3\n' +
230+
'1, 2, 4\n' +
231+
'1, 2, 5\n' +
232+
'1, 2, 6\n' +
233+
'1, 2, 7\n' +
234+
'1, 3, 1\n' +
235+
'1, 3, 2\n' +
236+
'1, 3, 3\n' +
237+
'1, 3, 4\n' +
238+
'1, 3, 5\n' +
239+
'1, 3, 6\n' +
240+
'1, 3, 7\n' +
241+
'1, 4, 1\n' +
242+
'1, 4, 2\n' +
243+
'1, 4, 3\n' +
244+
'1, 4, 4\n' +
245+
'1, 4, 5\n' +
246+
'1, 4, 6\n' +
247+
'1, 4, 7\n' +
248+
'1, 5, 1\n' +
249+
'1, 5, 2\n' +
250+
'1, 5, 3\n' +
251+
'1, 5, 4\n' +
252+
'1, 5, 5\n' +
253+
'1, 5, 6\n' +
254+
'1, 5, 7\n' +
255+
'1, 6, 1\n' +
256+
'1, 6, 2\n' +
257+
'1, 6, 3\n' +
258+
'1, 6, 4\n' +
259+
'1, 6, 5\n' +
260+
'1, 6, 6\n' +
261+
'1, 6, 7\n' +
262+
'2, 1, 1\n' +
263+
'2, 1, 2\n' +
264+
'2, 1, 3\n' +
265+
'2, 1, 4\n' +
266+
'2, 1, 5\n' +
267+
'2, 1, 6\n' +
268+
'2, 1, 7\n' +
269+
'2, 2, 1\n' +
270+
'2, 2, 2\n' +
271+
'2, 2, 3\n' +
272+
'2, 2, 4\n' +
273+
'2, 2, 5\n' +
274+
'2, 2, 6\n' +
275+
'2, 2, 7\n' +
276+
'2, 3, 1\n' +
277+
'2, 3, 2\n' +
278+
'2, 3, 3\n' +
279+
'2, 3, 4\n' +
280+
'2, 3, 5\n' +
281+
'2, 3, 6\n' +
282+
'2, 3, 7\n' +
283+
'2, 4, 1\n' +
284+
'2, 4, 2\n' +
285+
'2, 4, 3\n' +
286+
'2, 4, 4\n' +
287+
'2, 4, 5\n' +
288+
'2, 4, 6\n' +
289+
'2, 4, 7\n' +
290+
'2, 5, 1\n' +
291+
'2, 5, 2\n' +
292+
'2, 5, 3\n' +
293+
'2, 5, 4\n' +
294+
'2, 5, 5\n' +
295+
'2, 5, 6\n' +
296+
'2, 5, 7\n' +
297+
'2, 6, 1\n' +
298+
'2, 6, 2\n' +
299+
'2, 6, 3\n' +
300+
'2, 6, 4\n' +
301+
'2, 6, 5\n' +
302+
'2, 6, 6\n' +
303+
'2, 6, 7\n' +
304+
'3, 1, 1\n' +
305+
'3, 1, 2\n' +
306+
'3, 1, 3\n' +
307+
'3, 1, 4\n' +
308+
'3, 1, 5\n' +
309+
'3, 1, 6\n' +
310+
'3, 1, 7\n' +
311+
'3, 2, 1\n' +
312+
'3, 2, 2\n' +
313+
'3, 2, 3\n' +
314+
'3, 2, 4\n' +
315+
'3, 2, 5\n' +
316+
'3, 2, 6\n' +
317+
'3, 2, 7\n' +
318+
'3, 3, 1\n' +
319+
'3, 3, 2\n' +
320+
'3, 3, 3\n' +
321+
'3, 3, 4\n' +
322+
'3, 3, 5\n' +
323+
'3, 3, 6\n' +
324+
'3, 3, 7\n' +
325+
'3, 4, 1\n' +
326+
'3, 4, 2\n' +
327+
'3, 4, 3\n' +
328+
'3, 4, 4\n' +
329+
'3, 4, 5\n' +
330+
'3, 4, 6\n' +
331+
'3, 4, 7\n' +
332+
'3, 5, 1\n' +
333+
'3, 5, 2\n' +
334+
'3, 5, 3\n' +
335+
'3, 5, 4\n' +
336+
'3, 5, 5\n' +
337+
'3, 5, 6\n' +
338+
'3, 5, 7\n' +
339+
'3, 6, 1\n' +
340+
'3, 6, 2\n' +
341+
'3, 6, 3\n' +
342+
'3, 6, 4\n' +
343+
'3, 6, 5\n' +
344+
'3, 6, 6\n' +
345+
'3, 6, 7\n' +
346+
'4, 1, 1\n' +
347+
'4, 1, 2\n' +
348+
'4, 1, 3\n' +
349+
'4, 1, 4\n' +
350+
'4, 1, 5\n' +
351+
'4, 1, 6\n' +
352+
'4, 1, 7\n' +
353+
'4, 2, 1\n' +
354+
'4, 2, 2\n' +
355+
'4, 2, 3\n' +
356+
'4, 2, 4\n' +
357+
'4, 2, 5\n' +
358+
'4, 2, 6\n' +
359+
'4, 2, 7\n' +
360+
'4, 3, 1\n' +
361+
'4, 3, 2\n' +
362+
'4, 3, 3\n' +
363+
'4, 3, 4\n' +
364+
'4, 3, 5\n' +
365+
'4, 3, 6\n' +
366+
'4, 3, 7\n' +
367+
'4, 4, 1\n' +
368+
'4, 4, 2\n' +
369+
'4, 4, 3\n' +
370+
'4, 4, 4\n' +
371+
'4, 4, 5\n' +
372+
'4, 4, 6\n' +
373+
'4, 4, 7\n' +
374+
'4, 5, 1\n' +
375+
'4, 5, 2\n' +
376+
'4, 5, 3\n' +
377+
'4, 5, 4\n' +
378+
'4, 5, 5\n' +
379+
'4, 5, 6\n' +
380+
'4, 5, 7\n' +
381+
'4, 6, 1\n' +
382+
'4, 6, 2\n' +
383+
'4, 6, 3\n' +
384+
'4, 6, 4\n' +
385+
'4, 6, 5\n' +
386+
'4, 6, 6\n' +
387+
'4, 6, 7\n' +
388+
'5, 1, 1\n' +
389+
'5, 1, 2\n' +
390+
'5, 1, 3\n' +
391+
'5, 1, 4\n' +
392+
'5, 1, 5\n' +
393+
'5, 1, 6\n' +
394+
'5, 1, 7\n' +
395+
'5, 2, 1\n' +
396+
'5, 2, 2\n' +
397+
'5, 2, 3\n' +
398+
'5, 2, 4\n' +
399+
'5, 2, 5\n' +
400+
'5, 2, 6\n' +
401+
'5, 2, 7\n' +
402+
'5, 3, 1\n' +
403+
'5, 3, 2\n' +
404+
'5, 3, 3\n' +
405+
'5, 3, 4\n' +
406+
'5, 3, 5\n' +
407+
'5, 3, 6\n' +
408+
'5, 3, 7\n' +
409+
'5, 4, 1\n' +
410+
'5, 4, 2\n' +
411+
'5, 4, 3\n' +
412+
'5, 4, 4\n' +
413+
'5, 4, 5\n' +
414+
'5, 4, 6\n' +
415+
'5, 4, 7\n' +
416+
'5, 5, 1\n' +
417+
'5, 5, 2\n' +
418+
'5, 5, 3\n' +
419+
'5, 5, 4\n' +
420+
'5, 5, 5\n' +
421+
'5, 5, 6\n' +
422+
'5, 5, 7\n' +
423+
'5, 6, 1\n' +
424+
'5, 6, 2\n' +
425+
'5, 6, 3\n' +
426+
'5, 6, 4\n' +
427+
'5, 6, 5\n' +
428+
'5, 6, 6\n' +
429+
'5, 6, 7\n');
430+
});
431+
432+
test('should evaluate the fizz-buzz code.', () => {
433+
const code = `
434+
define function fizzBuzz with arguments [ number n ] which returns nothing:
435+
\tloop for n times with i as counter:
436+
\t\tif i%15 == 0, then do:
437+
\t\t\tdisplay 'FizzBuzz'
438+
\t\telse if i%3 == 0, then do:
439+
\t\t\tdisplay 'Fizz'
440+
\t\telse if i%5 == 0, then do:
441+
\t\t\tdisplay 'Buzz'
442+
\t\telse, do:
443+
\t\t\tdisplay i
444+
445+
\t\t\t
446+
execute fizzBuzz(20)
447+
448+
`;
449+
450+
const main = new Main(code);
451+
main.compile(new Scope());
452+
453+
expect(main.getOutput()).toStrictEqual('1\n' +
454+
'2\n' +
455+
'Fizz\n' +
456+
'4\n' +
457+
'Buzz\n' +
458+
'Fizz\n' +
459+
'7\n' +
460+
'8\n' +
461+
'Fizz\n' +
462+
'Buzz\n' +
463+
'11\n' +
464+
'Fizz\n' +
465+
'13\n' +
466+
'14\n' +
467+
'FizzBuzz\n' +
468+
'16\n' +
469+
'17\n' +
470+
'Fizz\n' +
471+
'19\n' +
472+
'Buzz\n');
473+
});
149474
});

0 commit comments

Comments
 (0)