test_lis.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,108 行 · 第 1/3 页

C
1,108
字号
           (ltemp1.length()==1 && ltemp1[0]==44.0 && l2.value()==44.0),
           TRUE);
  TEST_RUN("l2.member(ltemp1, 3.0)", l2.member(ltemp1, 3.0), 
           (ltemp1.length()==0), TRUE);
  TEST_RUN("l2.sublist(ltemp, l3)", l2.sublist(ltemp1, l3),
           (ltemp1.length()==3 && ltemp1[0]==22.0, ltemp1[1]==33.0, 
            ltemp1[2]==44.0 && l2.value()==22.0), 
           TRUE);
  TEST_RUN("l2.sublist(ltemp1, l1)", l2.sublist(ltemp1, l1),
           (ltemp1.length()==0), TRUE);
  TEST_RUN("l2.sublist(ltemp1, l4)", l2.sublist(ltemp1, l4), 
           (ltemp1.length()==0), TRUE);
  TEST_RUN("l4.sublist(ltemp1, l2)", l4.sublist(ltemp1, l2), 
           (ltemp1==l4 && l4.value()==11.0), TRUE);
  TEST_RUN("l1.sublist(ltemp1, l2)", l1.sublist(ltemp1, l2), 
           (ltemp1.length()==0), TRUE);

  ltemp1 = CoolList<double>(4,55.0,33.0,11.0,66.0);
  TEST_RUN("l5.set_intersection(l6)", l5.set_intersection(l6), (l5==ltemp1), TRUE);

  ltemp1 = CoolList<double>(5,111.0,55.0,33.0,11.0,66.0);
  TEST_RUN("l5.set_union(l6)", l5.set_union(l6), (l5==ltemp1), TRUE);

  TEST_RUN("l5.set_difference(l6)", ltemp1.clear(); l5.set_difference(l6), 
           (l5==ltemp1), TRUE);

  ltemp1 = CoolList<double>(5,11.0,66.0,111.0,33.0,55.0);
  TEST_RUN("l5.set_xor(l6)", l5.set_xor(l6), (l5==ltemp1), TRUE);

}

void list_double_set_test2() {

  CoolList<double> ltemp1;
  CoolList<double> l7(2,2.0,3.0);
  CoolList<double> l8(3,1.0,2.0,3.0);
  CoolList<double> l9 = l7 + l8;
  CoolList<double> l10(3,4.0,5.0,6.0);
  CoolList<double> l11(12,1.0,55.0,3.0,88.0,77.0,44.0,33.0,22.0,11.0,66.0,2.0,3.0);
  CoolList<double> l12(5,11.0,66.0,111.0,33.0,55.0);
  CoolList<double> l13;

  ltemp1 = CoolList<double>(5,2.0,3.0,1.0,2.0,3.0);
  TEST("l9 = l7 + l8", (l9==ltemp1), TRUE);
  ltemp1 = CoolList<double>(5,2.0,3.0,4.0,5.0,6.0);
  TEST_RUN("l7 += l10", l7 += l10, (l7==ltemp1), TRUE);

  ltemp1 = CoolList<double>(4,55.0,33.0,11.0,66.0);
  TEST_RUN("l11 &= l12", l11 &= l12, (l11==ltemp1), TRUE);

  ltemp1 = CoolList<double>(5,111.0,55.0,33.0,11.0,66.0);
  TEST_RUN("l11 |= l12", l11 |= l12, (l11==ltemp1), TRUE);

  TEST_RUN("l11 -= l12", ltemp1.clear(); l11 -= l12, (l11==ltemp1), TRUE);

  ltemp1 = CoolList<double>(5,11.0,66.0,111.0,33.0,55.0);
  TEST_RUN("l11 ^= l12", l11 ^= l12, (l11==ltemp1), TRUE);

  l11 = CoolList<double>(12,1.0,55.0,3.0,88.0,77.0,44.0,33.0,22.0,11.0,66.0,2.0,
                     3.0);
  l12 = CoolList<double>(5,11.0,66.0,111.0,33.0,55.0);

  ltemp1 = CoolList<double>(4,55.0,33.0,11.0,66.0);
  TEST_RUN("l13 = l11 & l12", l13 = l11 & l12, (l13==ltemp1), TRUE);

  ltemp1 = CoolList<double>(13,111.0,1.0,55.0,3.0,88.0,77.0,44.0,33.0,22.0,11.0,
                        66.0,2.0,3.0);
  TEST_RUN("l13 = l11 | l12", l13 = l11 | l12, (l13==ltemp1), TRUE);

  ltemp1 = CoolList<double>(8,1.0,3.0,88.0,77.0,44.0,22.0,2.0,3.0);
  TEST_RUN("l13 = l11 - l12", l13 = l11 - l12, (l13==ltemp1), TRUE);

  ltemp1 = CoolList<double>(9,1.0,3.0,88.0,77.0,44.0,22.0,2.0,3.0,111.0);
  TEST_RUN("l13 = l11 ^ l12", l13 = l11 ^ l12, (l13==ltemp1), TRUE);

}

void list_double_sort_test() {

  CoolList<double> l1(4,6.0,66.0,22.0,222.0);
  CoolList<double> l2(4,61.0,12.0,222.0,666.0);
  CoolList<double> l3(4,7.0,51.0,77.0,24.0);
  CoolList<double> ltemp1;

  ltemp1 = CoolList<double>(8,6.0,61.0,12.0,66.0,22.0,222.0,222.0,666.0);
  TEST_RUN("l1.merge(l2,&my_compare_double)", l1.merge(l2,&my_compare_double), 
           (l1==ltemp1), TRUE);

  ltemp1 = CoolList<double>(4,12.0,61.0,222.0,666.0);
  TEST_RUN("l2.sort(&my_compare_double)", l2.sort(&my_compare_double), 
           (l2==ltemp1), TRUE);

  ltemp1 = CoolList<double>(4,7.0,24.0,51.0,77.0);
  TEST_RUN("l3.sort(&my_compare_double)", l3.sort(&my_compare_double),
           (l3==ltemp1), TRUE);

}

void list_double_currentpos_test() {

  CoolList<double> l1(3,11.0,22.0,33.0);
  CoolList<double> l2;
  CoolList<double> ltemp1;

  TEST_RUN("l1.next()",l1.next(),l1.value(), 11.0);
  
  ltemp1 = CoolList<double>(4,11.0,55.0,22.0,33.0);
  TEST_RUN("l1.insert_after(55.0)", l1.insert_after(55.0), 
           (l1.value()==55.0 && l1==ltemp1), TRUE);

  TEST_RUN("l1.prev()",l1.prev(),l1.value(), 11.0);

  ltemp1 = CoolList<double>(5,66.0,11.0,55.0,22.0,33.0);
  TEST_RUN("l1.insert_before(66.0)", l1.insert_before(66.0), 
           (l1.value()==66.0 && l1==ltemp1), TRUE);

  ltemp1 = CoolList<double>(4,11.0,55.0,22.0,33.0);
  TEST("l1.remove()", (l1.remove()==66.0 && l1.value()==11.0 && l1==ltemp1),
        TRUE);

  ltemp1 = CoolList<double>(3,11.0,55.0,33.0);
  TEST_RUN("l1.remove()", l1.next();l1.next(),
           (l1.remove()==22.0 && l1.value()==33.0 && l1==ltemp1),
           TRUE);

  TEST_RUN("l1.position()", l1.prev(), (l1.position()==1 && l1.value()==55.0),
           TRUE);

  // l1 is (11.0,55.0,33.0)
  l2 = CoolList<double>(4,55.0,66.0,33.0,22.0);

  TEST_RUN("l1.next_union(l2)", l1.reset(),
           (l1.next_union(l2) && l1.value()==11.0), TRUE);
  TEST_RUN("l1.next_union(l2)", l1.next_union(l2); l1.next_union(l2),
           (l1.next_union(l2) && l1.value()==66.0),TRUE);
  TEST("l1.next_union(l2)", (l1.next_union(l2) && l1.value()==22.0),TRUE);
  TEST("l1.next_union(l2)", l1.next_union(l2), FALSE);

  TEST_RUN("l1.next_intersection(l2)", l1.reset(),
           (l1.next_intersection(l2) && l1.value()==55.0),
           TRUE);
  TEST("l1.next_intersection(l2)", 
       (l1.next_intersection(l2) && l1.value()==33.0),
       TRUE);
  TEST("l1.next_intersection(l2)", l1.next_intersection(l2), FALSE);
  TEST_RUN("l1.next_difference(l2)", l1.reset(),
           (l1.next_difference(l2) && l1.value()==11.0), 
           TRUE);
  TEST("l1.next_difference(l2)", l1.next_difference(l2), FALSE);
  TEST_RUN("l1.next_xor(l2)", l1.reset(),
           (l1.next_xor(l2) && l1.value()==11.0),
           TRUE);
  TEST_RUN("l1.next_xor(l2)", l1.next_xor(l2),
           (l1.next_xor(l2) && l1.value()==22.0),
           TRUE);
  TEST("l1.next_xor(l2)", l1.next_xor(l2), FALSE);
}

void list_double_io_test() {

  // ** an automated test would get input from file, rather than cin **
  CoolList<double> input_list;
//  cout << "\nTesting operator>> for CoolList<double>.";
//  cout << "\nExample of CoolList<double> is (1 2 3).";
//  cout << "\nPlease enter a CoolList<double> structure: ";
//  cin >> input_list;
//  cout << "\nInput_list is now set to: " << input_list << ".\n";

}

void test_list_double() {
  cout << "\n*** Testing List of Double ***\n\n";
  list_double_op_test1();
  list_double_op_test2();
  list_double_op_test3();
  list_double_op_test4();
  list_double_set_test1();
  list_double_set_test2();
  list_double_sort_test();
  list_double_currentpos_test();
  list_double_io_test();

}

// *******************
// List of char* tests
// *******************

void list_charp_op_test() {

  char* a = "a";
  char* b = "b";
  char* c = "c";
  char* d = "d";
  char* e = "e";

  CoolList<char*> l0;
  TEST("CoolList<char*> l0", 1, 1);

  CoolList<char*> l1(c);
  TEST("CoolList<char*> l1(c)", 1, 1);

  CoolList<char*> l2(b,l1);
  TEST("CoolList<char*> l2(b,l1)", 1, 1);

  CoolList<char*> l3(a,l2);
  TEST("CoolList<char*> l3(a,l2)", 1, 1);

  CoolList<char*> l4(3,a,b,c);
  TEST("CoolList<char*> l4(3,a,b,c)", 1, 1);

  CoolList<char*> l5(l4);
  TEST("CoolList<char*> l5(l4)", 1, 1);

  TEST("l3==l4", l3==l4, TRUE);
  TEST_RUN("l4[2]=d", l4[2]=d, l4[2]==d, TRUE);

}

// *******************
// List of char tests
// *******************

void list_char_op_test() {

  char a = 'a';
  char b = 'b';
  char c = 'c';
  char d = 'd';
  char e = 'e';

  CoolList<char> l0;
  TEST("CoolList<char> l0", 1, 1);

  CoolList<char> l1(c);
  TEST("CoolList<char> l1(c)", 1, 1);

  CoolList<char> l2(b,l1);
  TEST("CoolList<char> l2(b,l1)", 1, 1);

  CoolList<char> l3(a,l2);
  TEST("CoolList<char> l3(a,l2)", 1, 1);

//  CoolList<char> l4(3,a,b,c);                 // char in ... will cause 
//  TEST("CoolList<char> l4(3,a,b,c)", 1, 1);   // alignment error

  CoolList<char> l5(l3);
  TEST("CoolList<char> l5(l3)", 1, 1);

  TEST("l3==l5", l3==l5, TRUE);
  cout << l3 << &l5 << "\n";

  TEST_RUN("l5[2]=d", l5[2]=d, l5[2]==d, TRUE);

}

void test_list_chars() {
  cout << "\n*** Testing List of Char* ***\n\n";
  list_charp_op_test();
  cout << "\n*** Testing List of Char ***\n\n";
  list_char_op_test();

}

void test_list_vector() {

  cout << "\n*** Testing List of Vector of Int ***\n\n";
  CoolVector<int> v1(3,3,1,2,3);
  CoolVector<int> v2(3,3,1,2,3);

  CoolList<CoolVector<int> > l0;
  TEST("CoolList<CoolVector<int> > l0", l0.length()==0, TRUE);

  CoolList<CoolVector<int> > l1(v1);
  TEST("CoolList<CoolVector<int> > l1(v1)", (l1[0]==v1 && l1.length()==1), TRUE);

  CoolList<CoolVector<int>*> l2(2,&v1,&v2);
  TEST("CoolList<CoolVector<int>*> l2(2,&v1,&v2);", (l2[0]==&v1 && l2[1]==&v2), 1);

//  CoolList<CoolVector<int> > l3(2,v1,v2);   // canot pass vector in ...
//  TEST("CoolList<CoolVector<int> > l3(2,v1,v2);", (l3[0]==v1 && l3[1]==v2), 1);

  CoolList<CoolVector<int> > l3;
  l3.push(v1);
  l3.push_end(v2);
  TEST("CoolList<CoolVector<int> > l3(2,v1,v2);", (l3[0]==v1 && l3[1]==v2), 1);
  l3.push_end(v2);
}

void test_list_list() {
  cout << "\n*** Testing List of List ***\n\n";
  CoolList<int> l1(3,1,2,3);

  CoolList<CoolList<int> > l2(l1);
  TEST("CoolList<CoolList<int> > l2(l1)", (l2[0]==l1), 1);

  CoolList<CoolList<int>*> l4(&l1);
  TEST("CoolList<CoolList<int>*> l4(&l1)", (l4[0]==&l1), 1);

  CoolList<int>* l5 = &l1;
  CoolList<CoolList<int>*> l6(l5);
  TEST("CoolList<CoolList<int>*> l6(l5)", (l6[0]==&l1), 1);

//  CoolList<CoolList<int> > l3(2,l1,l1);  // canot pass list in ...
//  TEST("CoolList<CoolList<int> > l2(2,l1,l1)", (l2[0]==l1 && l2[1]==l1), 1);

  CoolList<CoolList<int> > l3;
  l3.push(l1);
  l3.push_end(l1);
  TEST("CoolList<CoolList<int> > l3(2,l1,l1)", (l3[0]==l1 && l3[1]==l1), 1);
}

void test_speed () {
  long n;
  for (n = 10; n <= 100000; n*=10) {    // length = 10, 100, 1000, 10000
    CoolTimer t;
    t.mark();
    {
      CoolList<int> l;
      for (int i = 0; i < n; i++) {
        l.push(i);
      }
    }
    cout << "Create and delete list of " << n << " elements took ";
    cout << t.real() << "milliseconds.\n";
  }
  for (n = 10000; n > 10; n/=10) {      // length = 10, 100, 1000, 10000
    CoolTimer t;
    t.mark();
    {
      CoolList<int> l;
      for (int i = 0; i < n; i++) {
        l.push(i);
      }
    }
    cout << "Create and delete list of " << n << " elements took ";
    cout << t.real() << "milliseconds.\n";
  }
}

void test_leak() { 
  for (;;) { 
    test_list_int(); 
    test_list_double();   
    test_list_chars() ;
    test_list_vector();
    test_list_list(); 
  } 
}

int main (void) {
  START("CoolList");
  test_list_int();
  test_list_double();
  test_list_chars();
  test_list_vector(); 
  test_list_list();
#if LEAK
  test_leak();
#endif
#if SPEED
  test_speed();
#endif
  SUMMARY();
  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?