Skip to content

Commit

Permalink
Add tests for BaseVector.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost authored and OpenModelica-Hudson committed Dec 9, 2015
1 parent 9d7b029 commit 008cd79
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
3 changes: 2 additions & 1 deletion openmodelica/bootstrapping/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ HashTableTest.mos \
PriorityQueue.mos \
SimplifyTest.mos \
System.mos \
UtilTest.mos
UtilTest.mos \
VectorTest.mos

# test that currently fail. Move up when fixed.
# Run make testfailing
Expand Down
80 changes: 80 additions & 0 deletions openmodelica/bootstrapping/VectorTest.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
encapsulated package StringVector
import BaseVector;
extends BaseVector(redeclare type T = String, defaultValue = "");
annotation(__OpenModelica_Interface="util");
end StringVector;

function print_vector
input StringVector.Vector inVector;
protected
Integer sz = StringVector.size(inVector);
Integer capacity = StringVector.capacity(inVector);
algorithm
print("[");
for i in 1:sz loop
print(StringVector.get(inVector, i));
if i <> capacity then print(", "); end if;
end for;

for i in (sz+1):capacity loop
print("_");
if i <> capacity then print(", "); end if;
end for;
print("]\n");
end print_vector;

partial function test
protected
StringVector.Vector vec;
end test;

// Tests basic operations
function test1 extends test;
algorithm
vec := StringVector.new(4);
print("isEmpty = " + boolString(StringVector.isEmpty(vec)) + "\n");
StringVector.add(vec, "test1"); print_vector(vec);
print("isEmpty = " + boolString(StringVector.isEmpty(vec)) + "\n");
StringVector.add(vec, "test2"); print_vector(vec);
StringVector.add(vec, "test3"); print_vector(vec);
StringVector.add(vec, "test4"); print_vector(vec);
StringVector.add(vec, "test5"); print_vector(vec);
StringVector.add(vec, "test6"); print_vector(vec);
StringVector.add(vec, "test7"); print_vector(vec);
StringVector.add(vec, "test8"); print_vector(vec);
StringVector.add(vec, "test9"); print_vector(vec);
StringVector.add(vec, "test10"); print_vector(vec);
StringVector.set(vec, 5, "test5b"); print_vector(vec);
StringVector.set(vec, 1, "test1b"); print_vector(vec);
StringVector.set(vec, 10, "test10b"); print_vector(vec);
end test1;

// Tests resizing operations.
function test2 extends test;
algorithm
vec := StringVector.new(); print_vector(vec);
StringVector.reserve(vec, 5); print_vector(vec);
StringVector.resize(vec, 6, "test"); print_vector(vec);
StringVector.resize(vec, 3, "test2"); print_vector(vec);
StringVector.trim(vec); print_vector(vec);
StringVector.resize(vec, 10, "test2"); print_vector(vec);
StringVector.pop(vec); print_vector(vec);
StringVector.trim(vec); print_vector(vec);
StringVector.pop(vec); print_vector(vec);
end test2;

// Tests fill operations.
function test3 extends test;
algorithm
vec := StringVector.newFill(8, "123"); print_vector(vec);
StringVector.fill(vec, "abc"); print_vector(vec);
StringVector.fill(vec, "xyz", 3); print_vector(vec);
StringVector.fill(vec, "123", 5, 7); print_vector(vec);
end test3;

// Checks that attempting to write to an element out of bounds fails.
function test4 extends test;
algorithm
vec := StringVector.new(4);
StringVector.set(vec, 1, "test");
end test4;
56 changes: 56 additions & 0 deletions openmodelica/bootstrapping/VectorTest.mos
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// name: VectorTest
// cflags: +g=MetaModelica +d=rml
// status: correct
// teardown_command: rm -f VectorTest_*

loadFile("../../../OMCompiler/Compiler/Util/BaseVector.mo");
getErrorString();
loadFile("VectorTest.mo");
getErrorString();
test1(); getErrorString();
test2(); getErrorString();
test3(); getErrorString();
test4(); getErrorString();

// Result:
// true
// ""
// true
// ""
// isEmpty = true
// [test1, _, _, _]
// isEmpty = false
// [test1, test2, _, _]
// [test1, test2, test3, _]
// [test1, test2, test3, test4]
// [test1, test2, test3, test4, test5, _, _, _]
// [test1, test2, test3, test4, test5, test6, _, _]
// [test1, test2, test3, test4, test5, test6, test7, _]
// [test1, test2, test3, test4, test5, test6, test7, test8]
// [test1, test2, test3, test4, test5, test6, test7, test8, test9, _, _, _, _, _, _, _]
// [test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, _, _, _, _, _, _]
// [test1, test2, test3, test4, test5b, test6, test7, test8, test9, test10, _, _, _, _, _, _]
// [test1b, test2, test3, test4, test5b, test6, test7, test8, test9, test10, _, _, _, _, _, _]
// [test1b, test2, test3, test4, test5b, test6, test7, test8, test9, test10b, _, _, _, _, _, _]
//
// ""
// [_]
// [_, _, _, _, _]
// [test, test, test, test, test, test]
// [test, test, test, _, _, _]
// [test, test, test]
// [test, test, test, test2, test2, test2, test2, test2, test2, test2]
// [test, test, test, test2, test2, test2, test2, test2, test2, _]
// [test, test, test, test2, test2, test2, test2, test2, test2, _]
// [test, test, test, test2, test2, test2, test2, test2, _, _]
//
// ""
// [123, 123, 123, 123, 123, 123, 123, 123]
// [abc, abc, abc, abc, abc, abc, abc, abc]
// [abc, abc, xyz, xyz, xyz, xyz, xyz, xyz]
// [abc, abc, xyz, xyz, 123, 123, 123, xyz]
//
// ""
// fail()
// ""
// endResult

0 comments on commit 008cd79

Please sign in to comment.