Skip to content

Commit d2ced83

Browse files
TEST std-list.mx - testing of index errors + %foreach()
1 parent b00a83b commit d2ced83

File tree

3 files changed

+115
-8
lines changed

3 files changed

+115
-8
lines changed

std/ds/list.mx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@
202202
; detach single node after THIS, set THIS to detached
203203
; assert detachable node exists
204204
%proc{ __list_detach_after_this__asserted,
205-
%call(__list_assert_this_has_next)
206205
%seg:push_seg(%this) ; save head
207-
%advance_this() ; THIS = THIS.next
206+
%call(__list_assert_this_has_next)
207+
%advance_this() ; THIS = THIS.next
208208
%seg:load(%this, %list_node:next) ; detached.next = null
209209
str 0
210-
%stack:drop() ; head.next = detached.next
210+
%stack:drop() ; head.next = detached.next
211211
movm
212212
strr
213213
}

tests/std-list.mx

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
%include "debug"
88
%include "array"
99

10+
%vm_runtime_delim()
11+
1012
%proc{ print_lists, ; uses main's local segment
1113
outc 'A'
1214
%seg:push_addr(%local, 0)
@@ -19,15 +21,15 @@
1921
%print_endl( %call(list_print) )
2022
}
2123

22-
%macro callListMethodWithArgs(methodName, listIdx, pushArguemts) {
24+
%macro callListMethodWithArgs(methodName, listIdx, pushArguments) {
2325
%if_else { ; lives list in main:locals?
2426
ld %listIdx
2527
lle 1
2628
,
2729
%seg:push_addr(%local, %listIdx),
2830
%seg:push(%local, %listIdx)
2931
}
30-
%pushArguemts
32+
%pushArguments
3133
%call(%methodName)
3234
}
3335
%macro callListMethod(methodName, listIdx) { ; no arguments
@@ -40,7 +42,7 @@
4042
outum
4143
outc %delim
4244
}
43-
%macro populateFirstArray() { ; initialize array at local:0
45+
%macro populateFirstList() { ; initialize array at local:0
4446
%callListMethodWithArgs(list_insert_after, 0, %stack:push(5))
4547
%callListMethodWithArgs(list_insert_after, 0, %stack:push(6))
4648
%callListMethodWithArgs(list_insert_back, 0, %stack:push(7))
@@ -52,7 +54,7 @@
5254
%seg:local(2, %list:construct_here()) ; third lives in heap (here is ptr to that struct)
5355
%seg:local(3, %array:init())
5456

55-
%populateFirstArray()
57+
%populateFirstList()
5658

5759
; A.insert_after(list[1], 2)
5860
%callListMethodWithArgs(list_index, 0, %stack:push(1))
@@ -77,6 +79,89 @@
7779
%printResult('B', ' ')
7880
%callListMethod(list_size, 2)
7981
%printResult('C', '\n')
82+
83+
; TODO splice
84+
85+
; set elems to their idx
86+
%seg:point(%this, %local, 0)
87+
%list:foreach {
88+
%stack:top()
89+
%seg:store(%this, %list_node:value)
90+
}
91+
%call(print_lists)
8092
}
8193

8294
%call(main)
95+
96+
; testing for errors in ctime ----------------------------------------------------
97+
%macro setLocalsToTemp() {
98+
%nop { ; point locals to temp (because we're not in any function)
99+
!store(%local, %temp_addr)
100+
}
101+
}
102+
%macro initCtimeTesting() {
103+
%setLocalsToTemp()
104+
%nop {
105+
!seg:temp(0, %list:init()) ; clear all lists
106+
!seg:temp(1, %list:init())
107+
; DONT USE third - passed via ptr
108+
!populateFirstList()
109+
!stack:push(%temp_addr)
110+
!print_endl(
111+
%call(list_print)
112+
)
113+
}
114+
}
115+
%macro _implErrorneousTest(methodName, listIdx, pushArguments, preDesc, desc) {
116+
%print_spaced( outc %preDesc )
117+
%callListMethodWithArgs(%methodName, %listIdx, %pushArguments)
118+
%printResult(%desc, '\n')
119+
}
120+
%macro testPossiblyErrorneous(methodName, listIdx, pushArguments, preDesc, desc) {
121+
%nop {
122+
!_implErrorneousTest(%methodName, %listIdx, %pushArguments, %preDesc, %desc)
123+
}
124+
%setLocalsToTemp() ; need to be repeated after error in function causes local segment to not return to original state
125+
}
126+
; test indexing bounds
127+
%initCtimeTesting()
128+
%nop(!print_endl(
129+
outc 'I'
130+
outc 'E'
131+
outc 's'
132+
))
133+
%testPossiblyErrorneous(list_index, 0, %stack:push(0), '0', 'I')
134+
%testPossiblyErrorneous(list_index, 0, %stack:push(2), '2', 'I')
135+
%testPossiblyErrorneous(list_index, 0, %stack:push(3), '3', 'I') ; out of bounds
136+
%nop(!print_spaced( outc 'B' ))
137+
%testPossiblyErrorneous(list_index, 1, %stack:push(0), '0', 'I') ; empty[0]
138+
139+
; popping
140+
%nop(!print_endl(
141+
outc 'P'
142+
outc 'O'
143+
outc 'P'
144+
outc ' '
145+
outc '*'
146+
%load(%temp_addr) ; first node in first list
147+
outum
148+
))
149+
; A = [6, 5, 7]
150+
%testPossiblyErrorneous(list_pop_after, 0, ld 0, '0', 'F') ; pop front
151+
%nop { ; pop after 5 -> 7
152+
!load(%temp_addr)
153+
!storer(%local)
154+
}
155+
%testPossiblyErrorneous(list_pop_after, 0, ld 0, '0', 'A') ; ^^
156+
%testPossiblyErrorneous(list_pop_back, 0, ld 0, '0', 'B') ; pop back
157+
%testPossiblyErrorneous(list_size, 0, ld 0, '0', 'S') ; size = 0
158+
159+
; popping empty
160+
%testPossiblyErrorneous(list_pop_after, 0, ld 0, 'A', '@') ; pop after
161+
%testPossiblyErrorneous(list_pop_back, 1, ld 0, 'B', '@') ; pop back
162+
%nop { ; !this.has_next
163+
!print_spaced( outc 'N' )
164+
!store(12, 0)
165+
!stack:push(12)
166+
!call(list_pop_after)
167+
}

tests/std-list.txt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
:returncode 0
22

3-
:stdout 50
3+
:stdout 209
4+
- VM -
5+
[6, 5, 7]
6+
IEs
7+
0 I:6
8+
2 I:7
9+
3
10+
ERROR: 2
11+
B 0
12+
ERROR: 2
13+
POP *2054
14+
0 F:6
15+
0 A:7
16+
0 B:5
17+
0 S:0
18+
A
19+
ERROR: 0
20+
B
21+
ERROR: 0
22+
N
23+
ERROR: 0
24+
- RUN -
425
I:5
526
A[6, 5, 2, 7] B[] C[]
627
A F:6 L:7
728
S A:4 B:0 C:0
29+
A[0, 1, 2, 3] B[] C[]
830

931

0 commit comments

Comments
 (0)