//===========================================================================// // File: table.tst // // Project: MUNGA Brick: Connection Library // // Contents: test routines for Table class // //---------------------------------------------------------------------------// // Date Who Modification // // -------- --- ---------------------------------------------------------- // // 10/01/94 ECH Initial coding. // // 11/02/94 JMA Made compatible with SGI CC, used Time class instead of // // clock_t // // 11/03/94 ECH Made compatible with BC4.0 // // 11/05/94 JMA Made compatible with GNU C++ // //---------------------------------------------------------------------------// // Copyright (C) 1994, Virtual World Entertainment, Inc. All Rights reserved // // PROPRIETARY AND CONFIDENTIAL // //===========================================================================// #include "time.hpp" class TableTestPlug: public Plug { public: int value; TableTestPlug(int value); ~TableTestPlug(); }; class TableTestNode: public Node { public: TableOf table1; TableOf table2; TableTestNode(); ~TableTestNode(); void RunProfile(); void RunTest(); }; #define RandInt(x) (rand() % x + 1) TableTestPlug::TableTestPlug(int value): Plug() { this->value = value; } TableTestPlug::~TableTestPlug() { } TableTestNode::TableTestNode(): Node(), table1(this, True), table2(this, True) { } TableTestNode::~TableTestNode() { } // //########################################################################### // ProfileClass //########################################################################### // Logical Table::ProfileClass() { TableTestNode testNode; Time startTicks = Now(); Test_Message("Table::ProfileClass\n"); testNode.RunProfile(); Test_Message("Table::ProfileClass elapsed = " << (Now() - startTicks) << endl); return True; } // //########################################################################### // TestClass //########################################################################### // Logical Table::TestClass() { DEBUG_STREAM << "Starting Table test...\n"; TableTestNode testNode; testNode.RunTest(); return True; } void TableTestNode::RunProfile() { TableTestPlug *testPlug1, *testPlug2; int values[TEST_CLASS]; int i, j; Time startTicks; /* * Generate unique values, shuffle them */ for (i = 0; i < TEST_CLASS; i++) { values[i] = i; } for (i = 0; i < TEST_CLASS; i++) { int tmp; j = i + RandInt(TEST_CLASS - i) - 1; tmp = values[j]; values[j] = values[i]; values[i] = tmp; } // //-------------------------------------------------------------------- // Run timing tests //-------------------------------------------------------------------- // /* * Create plugs and add to both sockets */ startTicks = Now(); for (i = 0; i < TEST_CLASS; i++) { testPlug1 = new TableTestPlug(values[i]); Register_Object( testPlug1 ); table1.AddValue(testPlug1, values[i]); table2.AddValue(testPlug1, values[i]); } Test_Message("TableTestNode::RunTest Create = " << (Now() - startTicks) << endl); /* * Iterate over both sockets */ startTicks = Now(); { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); i = 0; while ((testPlug1 = iterator1.ReadAndNext()) != NULL) { Check( testPlug1 ); Verify( testPlug1->value == i ); i++; } Verify( i == TEST_CLASS ); i = 0; while ((testPlug1 = iterator2.ReadAndNext()) != NULL) { Check( testPlug1 ); Verify( testPlug1->value == i ); i++; } Verify( i == TEST_CLASS ); } Test_Message("TableTestNode::RunTest Iterate = " << (Now() - startTicks) << endl); /* * Find */ startTicks = Now(); { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); for (i = 0; i < TEST_CLASS; i++) { testPlug1 = iterator1.Find(i); testPlug2 = iterator2.Find(i); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); Verify( testPlug1 == testPlug2 ); } } Test_Message("TableTestNode::RunTest Find = " << (Now() - startTicks) << endl); /* * Destroy from table1, verify with table2 */ startTicks = Now(); { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); i = 0; while ((testPlug1 = iterator1.ReadAndNext()) != NULL) { Check( testPlug1 ); Verify( testPlug1->value == i ); i++; Unregister_Object( testPlug1 ); delete(testPlug1); } Verify( i == TEST_CLASS ); Verify( iterator1.GetSize() == 0 ); Verify( iterator2.GetSize() == 0 ); } Test_Message("TableTestNode::RunTest Destroy = " << (Now() - startTicks) << endl); } void TableTestNode::RunTest() { TableTestPlug *testPlug1, *testPlug2; int values[TEST_CLASS]; int i, j; // Time startTicks; /* * Generate unique values, shuffle them */ for (i = 0; i < TEST_CLASS; i++) { values[i] = i; } for (i = 0; i < TEST_CLASS; i++) { int tmp; j = i + RandInt(TEST_CLASS - i) - 1; tmp = values[j]; values[j] = values[i]; values[i] = tmp; } // //-------------------------------------------------------------------- // Stress tests //-------------------------------------------------------------------- // /* * Create plugs and add to both sockets */ for (i = 0; i < TEST_CLASS; i++) { testPlug1 = new TableTestPlug(values[i]); Register_Object( testPlug1 ); table1.AddValue(testPlug1, values[i]); table2.AddValue(testPlug1, values[i]); } /* * Find */ { for (i = 0; i < TEST_CLASS; i++) { testPlug1 = table1.Find(i); testPlug2 = table2.Find(i); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); Verify( testPlug1 == testPlug2 ); } } /* * Test first and last */ { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); iterator1.First(); iterator2.First(); testPlug1 = iterator1.GetCurrent(); testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1 == iterator1.GetNth(0) ); Verify( testPlug1 == iterator2.GetNth(0) ); iterator1.Last(); iterator2.Last(); testPlug1 = iterator1.GetCurrent(); testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1 == iterator1.GetNth(TEST_CLASS - 1) ); Verify( testPlug1 == iterator2.GetNth(TEST_CLASS - 1) ); } /* * Test next and prev */ { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); i = 0; while ((testPlug1 = iterator1.GetCurrent()) != NULL) { testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); iterator1.Next(); iterator2.Next(); i++; } Verify( i == TEST_CLASS ); iterator1.Last(); iterator2.Last(); i = TEST_CLASS - 1; while ((testPlug1 = iterator1.GetCurrent()) != NULL) { testPlug2 = iterator2.GetCurrent(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); iterator1.Previous(); iterator2.Previous(); i--; } Verify( i == -1 ); } /* * Test read next and read prev */ { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); i = 0; while ((testPlug1 = iterator1.ReadAndNext()) != NULL) { testPlug2 = iterator2.ReadAndNext(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); i++; } Verify( i == TEST_CLASS ); iterator1.Last(); iterator2.Last(); i = TEST_CLASS - 1; while ((testPlug1 = iterator1.ReadAndPrevious()) != NULL) { testPlug2 = iterator2.ReadAndPrevious(); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); i--; } Verify( i == -1 ); } /* * Test nth */ { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); for (i = 0; i < TEST_CLASS; i++) { testPlug1 = iterator1.GetNth((int)i); testPlug2 = iterator2.GetNth((int)i); Check( testPlug1 ); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Verify( testPlug1->value == i ); Verify( testPlug2->value == i ); } } /* * Test Remove */ { TableIteratorOf iterator1(&table1); TableIteratorOf iterator2(&table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); i = 0; while ((testPlug1 = iterator1.GetCurrent()) != NULL) { Check( testPlug1 ); Verify( testPlug1->value == i ); iterator1.Remove(); testPlug2 = iterator2.GetNth(0); Check( testPlug2 ); Verify( testPlug2->value == i ); Verify( testPlug1 == testPlug2 ); Unregister_Object( testPlug2 ); delete(testPlug2); i++; } Verify( i == TEST_CLASS ); Verify( iterator1.GetSize() == 0 ); Verify( iterator2.GetSize() == 0 ); } /* * Test random deletion */ { /* * Add plugs to both sockets */ TableIteratorOf iterator1(table1); TableIteratorOf iterator2(table2); Verify( iterator1.GetSize() == 0 ); Verify( iterator2.GetSize() == 0 ); for (i = 0; i < TEST_CLASS; i++) { testPlug1 = new TableTestPlug(values[i]); Register_Object( testPlug1 ); table1.AddValue(testPlug1, values[i]); table2.AddValue(testPlug1, values[i]); } } { /* * Perform random deletion */ int size, index; TableIteratorOf iterator1(table1); TableIteratorOf iterator2(table2); Verify( iterator1.GetSize() == TEST_CLASS ); Verify( iterator2.GetSize() == TEST_CLASS ); i = 0; while((size = iterator1.GetSize()) != 0) { index = RandInt(size) - 1; testPlug1 = iterator1.GetNth(index); Check( testPlug1 ); iterator1.Remove(); testPlug2 = iterator2.GetNth(index); Check( testPlug2 ); Verify( testPlug1 == testPlug2 ); Unregister_Object( testPlug2 ); delete(testPlug2); i++; } Verify( i == TEST_CLASS ); Verify( iterator1.GetSize() == 0 ); Verify( iterator2.GetSize() == 0 ); } }