X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=blobdiff_plain;f=csrc%2Foceani-tests.mdc;h=12116d4cbb5f40325d9fb1976663bb229183fbfa;hp=d721476903ceb30783a19c9a9389402bc83105f5;hb=b57589e699a72debfbbe6a29983dfe7ed707a148;hpb=9c3c0e628883128ce89c34c177c1b5bb534ea69d diff --git a/csrc/oceani-tests.mdc b/csrc/oceani-tests.mdc index d721476..12116d4 100644 --- a/csrc/oceani-tests.mdc +++ b/csrc/oceani-tests.mdc @@ -714,6 +714,63 @@ Test functions. They don't return anything, so we need to get them to print double 1 is 2 double -2 is -4 +## References + +A simple linked list example + +###### test list + oceani_tests += "linked_list,one,two,three,four" + +###### test: linked_list + + struct node + next: @node + this: string + + func insert(list:@node; new:string):@node + p:=list + prev := @nil + while ?p and then p@.this < new: + prev = p + p = p@.next + if ?prev: + prev@.next = @new() + prev@.next@.next = p + prev@.next@.this = new + else + list = @new() + list@.next = p + list@.this = new + use list + + func printlist(list:@node) + while ?list: + print list@.this + list = list@.next + + func freelist(list:@node) + if list != @nil: + freelist(list@.next) + @free = list + + func main(argv:[ac::]string) + list := insert(@nil, "@start") + list = insert(list, "~end") + for i:=1; then i=i+1; while i < ac: + list = insert(list, argv[i]) + list = insert(list, "Hello!") + printlist(list) + freelist(list) + +###### output: linked_list,one,two,three,four + @start + Hello! + four + one + three + two + ~end + ## Test code with syntax errors Syntax errors aren't handled well yet - the result is almost always a @@ -1114,6 +1171,45 @@ Test for type errors with functions .tmp.code:9:5: error: function 'test1' redeclared .tmp.code:2:5: info: this is where 'test1' was first declared +Test for errors with references + +###### test list + oceani_failing_tests += ref_err1 ref_err2 + +###### test: ref_err1 + func main() + ref:@number + @foo = ref + ref = @old() + if ref == @null: + print "null" + +###### output: ref_err1 + .tmp.code:4:9: error: only "@free" makes sense here: foo + .tmp.code:5:15: error: Only reference function is "@new()": old + .tmp.code:6:19: error: Only reference value is "@nil": null + +###### test: ref_err2 + func main() + ref:@number + ref2:@string + num:number = @new() + print num@ + if num == @nil or ref == ref2 or ref == 2: + @free = num + +###### output: ref_err2 + .tmp.code:5:22: error: @new() can only be used with references, not number + .tmp.code:5:8: info: variable 'num' was set as number here. + .tmp.code:6:14: error: Cannot dereference number + .tmp.code:7:19: error: @nil can only be used with reference, not number + .tmp.code:7:33: error: expected @number but variable 'ref2' is @string + .tmp.code:4:8: info: this is where 'ref2' was set to @string + .tmp.code:7:48: error: expected @number found number + .tmp.code:8:17: error: @free can only be assigned a reference, not number + .tmp.code:8:17: error: @free can only be assigned a reference, not number + oceani: type error in program - not running. + ## Test erroneous command line args To improve coverage, we want to test correct handling of strange command