123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include "tests/ListTests.h"
- #include "tests/Test.h"
- #include "utils/List.h"
- typedef List<int, 20> IntList;
- static void testAdd(Test& test) {
- IntList list;
- list.add(5);
- test.checkEqual(5, list[0], "list contains added value");
- test.checkEqual(1, list.getLength(), "list sizes is increased by add");
- }
- static void testMultipleAdd(Test& test) {
- IntList list;
- list.add(4);
- list.add(3);
- list.add(2);
- test.checkEqual(4, list[0], "list contains added value");
- test.checkEqual(3, list[1], "list contains added value");
- test.checkEqual(2, list[2], "list contains added value");
- test.checkEqual(3, list.getLength(), "list sizes is increased by add");
- }
- static void testAddReplace(Test& test) {
- IntList list;
- list.add(5);
- list[0] = 3;
- test.checkEqual(3, list[0], "list value is overwritten");
- }
- static void testClear(Test& test) {
- IntList list;
- list.add(5);
- list.add(4);
- list.clear();
- test.checkEqual(0, list.getLength(), "list length is 0 after clear");
- }
- static void testOverflow(Test& test) {
- IntList list;
- for(int i = 0; i < 1000000; i++) {
- list.add(i);
- }
- for(int i = 0; i < list.getLength(); i++) {
- test.checkEqual(i, list[i], "list still contains values after overflow");
- }
- test.checkEqual(true, true, "list survives overflow");
- }
- static void testCopy(Test& test) {
- IntList list;
- list.add(1);
- list.add(2);
- list.add(3);
- IntList copy(list);
- test.checkEqual(list.getLength(), copy.getLength(), "list copy has same length");
- for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
- test.checkEqual(list[i], copy[i], "list copy has same values");
- }
- }
- static void testCopyAssignment(Test& test) {
- IntList list;
- list.add(1);
- list.add(2);
- list.add(3);
- IntList copy;
- copy = list;
- test.checkEqual(list.getLength(), copy.getLength(), "list copy assignment has same length");
- for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
- test.checkEqual(list[i], copy[i], "list copy assignment has same values");
- }
- }
- static void testMove(Test& test) {
- IntList list;
- list.add(1);
- list.add(2);
- list.add(3);
- IntList move(std::move(list));
- test.checkEqual(0, list.getLength(), "moved list has length 0");
- test.checkEqual(3, move.getLength(), "moved list passes length");
- test.checkEqual(1, move[0], "moved list passes values");
- test.checkEqual(2, move[1], "moved list passes values");
- test.checkEqual(3, move[2], "moved list passes values");
- }
- static void testMoveAssignment(Test& test) {
- IntList list;
- list.add(1);
- list.add(2);
- list.add(3);
- IntList move(std::move(list));
- test.checkEqual(0, list.getLength(), "assignment moved list has length 0");
- test.checkEqual(3, move.getLength(), "assignment moved list passes length");
- test.checkEqual(1, move[0], "assignment moved list passes values");
- test.checkEqual(2, move[1], "assignment moved list passes values");
- test.checkEqual(3, move[2], "assignment moved list passes values");
- }
- void ListTests::test() {
- Test test("List");
- testAdd(test);
- testMultipleAdd(test);
- testAddReplace(test);
- testClear(test);
- testOverflow(test);
- testCopy(test);
- testCopyAssignment(test);
- testMove(test);
- testMoveAssignment(test);
- test.finalize();
- }
|