#include #include void testValueConstructorSizeAndGet() { V10 values(5); assert(values.size() == 10); for (int i = 0; i < values.size(); ++i) { assert(values.get(i) == 5); } } void testDifferentSizes() { V10 small(1); V10 medium(2); V10 large(3); assert(small.size() == 3); assert(medium.size() == 7); assert(large.size() == 20); } void testDifferentTypes() { V10 integers(12); V10 doubles(2.5); V10 characters('A'); assert(integers.get(0) == 12); assert(doubles.get(0) == 2.5); assert(characters.get(0) == 'A'); } void testSetAndGet() { V10 values(0); values.set(0, 10); values.set(2, 20); values.set(4, 30); assert(values.get(0) == 10); assert(values.get(1) == 0); assert(values.get(2) == 20); assert(values.get(3) == 0); assert(values.get(4) == 30); } void testNonConstOperatorBrackets() { V10 values(0); values[0] = 11; values[2] = 22; values[4] = 44; assert(values[0] == 11); assert(values[1] == 0); assert(values[2] == 22); assert(values[4] == 44); // Checks that operator[] returns a modifiable reference. int& value = values[2]; value = 99; assert(values[2] == 99); } void testConstOperatorBrackets() { const V10 values(42); assert(values[0] == 42); assert(values[2] == 42); assert(values[4] == 42); // This line must compile thanks to the const version of operator[]. const int& value = values[3]; assert(value == 42); // However, this must NOT compile: // values[3] = 10; } void testCopyConstructor() { V10 original(4); original[1] = 10; original[3] = 20; V10 copy(original); assert(copy.size() == 5); for (int i = 0; i < original.size(); ++i) { assert(original[i] == copy[i]); } // Checks that these are two independent objects. copy[0] = 999; assert(original[0] == 4); assert(copy[0] == 999); } void testEquals() { V10 first(7); V10 second(7); V10 third(7); assert(first.equals(second)); assert(second.equals(first)); third[3] = 8; assert(!first.equals(third)); assert(!third.equals(first)); } void testEqualsWithConstObjects() { const V10 first(2); const V10 second(2); assert(first.equals(second)); } void testFind() { V10 values(0); values[1] = 10; values[3] = 20; values[5] = 10; assert(values.find(0) == 0); assert(values.find(10) == 1); assert(values.find(20) == 3); assert(values.find(999) == -1); } void testFindWithConstObject() { const V10 values('X'); assert(values.find('X') == 0); assert(values.find('A') == -1); } void testCopyFrom() { V10 source(3); V10 destination(0); source[0] = 10; source[4] = 40; destination.copyFrom(source); assert(destination.equals(source)); // Modifying the destination must not modify the source. destination[0] = 999; assert(source[0] == 10); assert(destination[0] == 999); } void testCopyFromWithConstSource() { const V10 source(8); V10 destination(0); destination.copyFrom(source); assert(destination.equals(source)); } void testAssignmentOperator() { V10 source(1); V10 destination(9); source[2] = 12; source[4] = 42; destination = source; assert(destination.equals(source)); // The objects must remain independent. destination[0] = 100; assert(source[0] == 1); assert(destination[0] == 100); } void testAssignmentOperatorWithConstSource() { const V10 source(17); V10 destination(0); destination = source; assert(destination.equals(source)); } void testSelfAssignment() { V10 values(6); values[1] = 12; values = values; assert(values[0] == 6); assert(values[1] == 12); assert(values[2] == 6); assert(values[3] == 6); } void testChainedAssignments() { V10 first(1); V10 second(2); V10 third(3); first = second = third; assert(first.equals(third)); assert(second.equals(third)); } void testDoubleValues() { V10 values(1.5); values[2] = 3.25; assert(values.get(0) == 1.5); assert(values.get(2) == 3.25); assert(values.find(3.25) == 2); assert(values.find(9.0) == -1); } void testCharValues() { V10 values('a'); values[2] = 'z'; assert(values[0] == 'a'); assert(values[2] == 'z'); assert(values.find('z') == 2); assert(values.find('x') == -1); } void testPrint() { V10 values(7); std::cout << "\nVisual test of print():\n"; std::cout << "Expected: 7 7 7 7 7 (exact format depends on your choice)\n"; std::cout << "Actual : "; values.print(); } void testPrintWithConstObject() { const V10 values(4); std::cout << "\nTest of print() on a const object:\n"; values.print(); } int main() { std::cout << "=== V10 template class tests ===\n"; testValueConstructorSizeAndGet(); testDifferentSizes(); testDifferentTypes(); testSetAndGet(); testNonConstOperatorBrackets(); testConstOperatorBrackets(); testCopyConstructor(); testEquals(); testEqualsWithConstObjects(); testFind(); testFindWithConstObject(); testCopyFrom(); testCopyFromWithConstSource(); testAssignmentOperator(); testAssignmentOperatorWithConstSource(); testSelfAssignment(); testChainedAssignments(); testDoubleValues(); testCharValues(); testPrint(); testPrintWithConstObject(); std::cout << "\nAll automatic tests passed.\n"; return 0; }