test_lis.c

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

C
1,108
字号
//
// Copyright (C) 1991 Texas Instruments Incorporated.
//
// Permission is granted to any individual or institution to use, copy, modify,
// and distribute this software, provided that this complete copyright and
// permission notice is maintained, intact, in all copies and supporting
// documentation.
//
// Texas Instruments Incorporated provides this software "as is" without
// express or implied warranty.
//
// Updated: JAM 08/14/92 -- modernized template syntax, remove macro hacks
// Updated: JAM 08/14/92 -- changed counter n from int->long

#include <cool/test.h>
#include <cool/List.h>
#include <cool/Vector.h>
#include <cool/Timer.h>

#include <cool/List.C>
#include <cool/Vector.C>

// *****************
// List of int tests
// *****************

int my_compare_int(const int& t1, const int& t2) {
  return ((t1 < t2) ? -1 : 1);
}

void list_int_op_test1() {
  
  CoolList<int> ltemp1;
  CoolList<int> ltemp2;

  // CoolList l0 is ().
  CoolList<int> l0;
  TEST("CoolList<int> l0", 1, 1);

  // CoolList l1(3) is (3).
  CoolList<int> l1(3);
  TEST("CoolList<int> l1(3)", 1, 1);

  // CoolList l2(2,l1) is (2 3).
  CoolList<int> l2(2,l1);
  TEST("CoolList<int> l2(2,l1)", 1, 1);

  // CoolList l3(1,l2) is (1 2 3).
  CoolList<int> l3(1,l2);
  TEST("CoolList<int> l3(1,l2)", 1, 1);

  // CoolList l4(3,1,2,3) is (1 2 3).
  CoolList<int> l4(3,1,2,3);
  TEST("CoolList<int> l4(3,1,2,3)", 1, 1);

  CoolList<int> l41(1, 0);
  TEST("CoolList<int> l41(1, 0)", 
       (l41[0]==0 && l41.length()==1), TRUE);
  CoolList<int> l42(2, 0, 1);
  TEST("CoolList<int> l42(2, 0, 1)", 
       (l42[0]==0 && l42[1]==1 && l42.length()==2), TRUE);
  CoolList<int> l43(3, 0, 1, 2);
  TEST("CoolList<int> l43(3, 0, 1, 2)", 
       (l43[0]==0 && l43[1]==1 && l43[2]==2 && l43.length()==3), TRUE);


  // CoolList l5(l4) is (1 2 3).
  CoolList<int> l5(l4);
  TEST("CoolList<int> l5(l4)", 1, 1);
  TEST("l3 == l4", l3==l4, TRUE);

  TEST("l5[2]", l5[2]==3 && l5.value()==3, TRUE);
//  TEST("l5[-1]", l5[-1], ERROR);
//  TEST("l5[3]", l5[3], ERROR);
  TEST("l5.get(1)", l5.get(1)==2, TRUE);
  TEST_RUN("l5[1]=43", l5[1]=43, l5[1]==43, TRUE);
  TEST_RUN("l5.put(55,1)", l5.put(55,1), l5.get(1)==55, TRUE);
  // l5 is (1 55 3);
  TEST_RUN("l5.tail(ltemp1)", l5.tail(ltemp1),
           (ltemp1.length()==2 && ltemp1[0]==55 && ltemp1[1]==3), TRUE);
  TEST_RUN("l5.tail(ltemp1,0)", l5.tail(ltemp1,0),
           (ltemp1.length()==3 && ltemp1[0]==1 && ltemp1[1]==55 
            && ltemp1[2]==3),
           TRUE);
  TEST_RUN("l5.tail(ltemp1,3)", l5.tail(ltemp1,3), ltemp1.is_empty(), TRUE);
  TEST_RUN("l5.tail(ltemp1,5)", l5.tail(ltemp1,5), ltemp1.is_empty(), TRUE);

  // l5 is (1 55 3);
  TEST_RUN("l5.last(ltemp1, 0)", l5.last(ltemp1, 0), ltemp1.is_empty(), TRUE);
  TEST_RUN("l5.last(ltemp1)", l5.last(ltemp1),
           (ltemp1.length()==1 && ltemp1[0]==3), TRUE);
  TEST_RUN("l5.last(ltemp1,2)", l5.last(ltemp1,2),
           (ltemp1.length()==2 && ltemp1[0]==55 && ltemp1[1]==3),
           TRUE);
  TEST_RUN("l5.last(ltemp1,3)", l5.last(ltemp1,3),
           (ltemp1.length()==3 && ltemp1[0]==1 && ltemp1[1]==55 
            && ltemp1[2]==3),
           TRUE);
  TEST_RUN("l5.last(ltemp1,5)", l5.last(ltemp1,5), ltemp1.is_empty(), TRUE);
  // l2 is (2 3)
  TEST("l2.is_empty()", l2.is_empty(), FALSE);
  // l3 is (1 2 3)
  TEST("l3.length()", l3.length()== 3, TRUE);
}

void list_int_op_test2() {
  
  CoolList<int> ltemp1;
  CoolList<int> ltemp2;

  ltemp1 = CoolList<int>(4,11,22,33,44);
  CoolList<int> l6;

  TEST_RUN("l6.but_last(ltemp2, 0)", l6.copy(ltemp1);  l6.but_last(ltemp2, 0),
           (ltemp2.length()==4 && ltemp2[0]==11 && ltemp2[1]==22 
            && ltemp2[2]==33 && ltemp2[3]==44),
           TRUE);

  // l6 is now (11 22 33 44)
  TEST_RUN("l6.but_last(ltemp2)", l6.but_last(ltemp2),
           (ltemp2.length()==3 && ltemp2[0]==11 && ltemp2[1]==22 
            && ltemp2[2]==33),
           TRUE);
  TEST_RUN("l6.but_last(ltemp2, 2)", l6.but_last(ltemp2, 2),
           (ltemp2.length()==2 && ltemp2[0]==11 && ltemp2[1]==22),
           TRUE);
  TEST_RUN("l6.but_last(ltemp2, 4)", l6.but_last(ltemp2, 4), 
           (ltemp2.length()==0), TRUE);
  TEST_RUN("l6.but_last(ltemp2, 5)", l6.but_last(ltemp2, 5),
           (ltemp2.length()==0), TRUE);

  TEST_RUN("l6.clear()", l6.clear(), (l6.length()==0), TRUE); 
  // l6 is now ()
  TEST("l6.is_empty()", l6.is_empty(), TRUE);
  TEST("l6.length()", l6.length()==0, TRUE);

  // l6 is (11 22 33 44)
  TEST_RUN("l6.position(33)", l6.copy(ltemp1), 
           (l6.position(33)==2 && l6.value()==33), TRUE);
  TEST("l6.position(2)", l6.position(2)==-1, TRUE);
}

void list_int_op_test3() {
  int temp;
  CoolList<int> ltemp1;
  CoolList<int> ltemp2;
  CoolList<int> l0;
  CoolList<int> l1(3);                          // CoolList l1 is (3).
  CoolList<int> l2(2,l1);                               // CoolList l2 is (2 3).
  CoolList<int> l3(1,l2);                               // CoolList l3 is (1 2 3).
  CoolList<int> l4(3,1,55,3);                   // CoolList l4 is (1 55 3).
  CoolList<int> l5(l4);                         // CoolList l5 is (1 2 3).
  CoolList<int> l6(4, 11, 22, 33, 44);          // CoolList l6 is (11 22 33 44)

  CoolList<int> l9;
  TEST_RUN("l9.copy(l4)", l9.copy(l4), (l4==l9), TRUE);
  TEST_RUN("l9 = l4", l9 = l4, (l4==l9), TRUE);

  // l6 is (11 22 33 44)
  ltemp1 = CoolList<int>(4,44,33,22,11);
  TEST_RUN("l6.reverse()", l6.reverse(), (l6==ltemp1), TRUE);

  // l6 is (44 33 22 11)
  TEST_RUN("l6.push_end(66)", l6.push_end(66), 
           (l6.length()==5 && l6[4]==66 && l6.value()==66),
           TRUE);
  TEST_RUN("l6.push(77)", l6.push(77), 
           (l6.length()==6 && l6[0]==77 && l6.value()==77), TRUE);
  TEST_RUN("l6.push_new(88)", l6.push_new(88), 
           (l6.length()==7 && l6[0]==88 && l6.value()==88),
           TRUE);
  TEST_RUN("l6.push_new(22)", l6.push_new(22), 
           (l6.length()==7 && l6[0]==88), TRUE);
  TEST("l6.pop()", (l6.pop(temp), temp), 88);

  // l6 is (77 44 33 22 11 66)
  // l2 is (2 3)
  // l4 is (1 55 3)

  // l6 is now (77 44 33 22 11 66 2 3)
  TEST_RUN("l6.append(l2)", l6.append(l2),
           (l6.value()==2 && l6.length()==8 && l6[6]==2 && l6[7]==3), TRUE);

  // l6 is now (1 55 3 77 44 33 22 11 66 2 3)
  TEST_RUN("l6.prepend(l4)", l6.prepend(l4),
           (l6.value()==1 && l6.length()==11 && l6[0]==1 && l6[1]==55 
            && l6[2]==3),
           TRUE);

  TEST_RUN("l6.set_tail(l2, 14)", ltemp1.copy(l6), l6.set_tail(l2,14), FALSE);
  TEST("l6.set_tail(l2, 14) ==", l6==ltemp1, TRUE);
  ltemp2 = CoolList<int>(6,1,55,3,77,2,3);
  TEST("l6.set_tail(l2,4)", l6.set_tail(l2,4), TRUE);
  TEST("l6.set_tail(l2,4) ==", (l6==ltemp2), TRUE);
  TEST("l6.set_tail(l2,4) value", l6.value(), 2);
  TEST_RUN("l6.set_tail(l2)", l6.set_tail(l2),
           (l6.value()==2 && l6.length()==3 && l6[0]==1 && l6[1]==2 
            && l6[2]==3),
           TRUE);
  TEST_RUN("l6.set_tail(l2,0)", l6.set_tail(l2,0),
           (l6.value()==2 && l6.length()==2 && l6[0]==2 && l6[1]==3), TRUE);

}

void list_int_op_test4() {
  CoolList<int> ltemp1;
  CoolList<int> ltemp2;

  ltemp1 = CoolList<int>(4,44,33,22,11);

  CoolList<int> l8(5,11,22,33,44,55);
  ltemp1.copy(l8);
  ltemp2 = CoolList<int>(4,11,22,33,55);
  TEST_RUN("l8.remove(77)", l8.remove(77), (l8==ltemp1), TRUE);
  TEST_RUN("l8.remove(44)", l8.remove(44), (l8==ltemp2 && l8.value()==55),
           TRUE);

  CoolList<int> l11(7,11,22,33,11,22,33,44,11,11);
  ltemp1 = CoolList<int>(4,11,22,33,44);
  l11.remove_duplicates();
  TEST_RUN("l11.remove_duplicates()", l11.remove_duplicates(), (l11==ltemp1),
           TRUE);

  // l8 is now (11 22 33 55)
  ltemp1.copy(l8);
  ltemp2 = CoolList<int>(4,11,55,33,55);
  TEST_RUN("l8.replace(66,22)", l8.replace(66,22), (l8==ltemp1), TRUE);
  TEST_RUN("l8.replace(22,55)", l8.replace(22,55), 
           (l8==ltemp2 && l8.value()==55), TRUE);

  ltemp1.copy(l8);
  ltemp2 = CoolList<int>(4,11,22,33,22);
  TEST_RUN("l8.replace_all(66,22)", l8.replace_all(66,22), (l8==ltemp1), TRUE);
  TEST_RUN("l8.replace_all(55,22)", l8.replace_all(55,22), (l8==ltemp2), TRUE);


  // l8 is now (11 22 33 22)
  ltemp1.copy(l8);
  ltemp2 = CoolList<int>(5,11,22,111,33,22);
  TEST_RUN("l8.insert_after(111,44)", l8.insert_after(111,44), (l8==ltemp1),
           TRUE);
  TEST_RUN("l8.insert_after(111,22)", l8.insert_after(111,22), 
           (l8==ltemp2 && l8.value()==111), TRUE);

  TEST_RUN("l8.insert_before(222,44)", 
           ltemp1.copy(l8); l8.insert_before(222,44), 
           (l8==ltemp1), TRUE);
  ltemp2 = CoolList<int>(6,11,222,22,111,33,22);
  TEST_RUN("l8.insert_before(222,22)", l8.insert_before(222,22),
           (l8==ltemp2 && l8.value()==222), TRUE);
  ltemp2 = CoolList<int>(7,333,11,222,22,111,33,22);
  TEST_RUN("l8.insert_before(333,11)", l8.insert_before(333,11),
           (l8==ltemp2 && l8.value()==333), TRUE);
}

void list_int_sort_test() {

  CoolList<int> l1(4,6,66,22,222);
  CoolList<int> l2(4,61,12,222,666);
  CoolList<int> l3(4,7,51,77,24);
  CoolList<int> l4(2,22,33);
  CoolList<int> ltemp1;

  ltemp1 = CoolList<int>(8,6,61,12,66,22,222,222,666);
  TEST_RUN("l1.merge(l2,&my_compare_int)", l1.merge(l2,&my_compare_int), 
           (l1==ltemp1), TRUE);

  ltemp1 = CoolList<int>(6,22,33,61,12,222,666);
  TEST_RUN("l4.merge(l2,&my_compare_int)", l4.merge(l2,&my_compare_int), 
           (l4==ltemp1), TRUE);

  ltemp1 = CoolList<int>(4,12,61,222,666);
  TEST_RUN("l2.sort(&my_compare_int)", l2.sort(&my_compare_int), (l2==ltemp1),
           TRUE);

  ltemp1 = CoolList<int>(4,7,24,51,77);
  TEST_RUN("l3.sort(&my_compare_int)", l3.sort(&my_compare_int), (l3==ltemp1),
           TRUE);
}

void list_int_io_test() {

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

}


void list_int_set_test1() {
  CoolList<int> ltemp1;
  CoolList<int> l1;
  CoolList<int> l2(4,11,22,33,44);
  CoolList<int> l3(2,22,33);
  CoolList<int> l4(5,11,22,33,44,55);

  TEST("l2.find(33)", (l2.find(33) && l2.value()==33), TRUE);
  TEST("l2.find(2)", l2.find(2), FALSE);

  TEST("l2.search(l3)", (l2.search(l3) && l2.value()==22), TRUE);
  TEST("l2.search(l1)", l2.search(l1), FALSE);
  TEST("l2.search(l4)", l2.search(l4), FALSE);
  TEST("l4.search(l2)", (l4.search(l2) && l4.value()==11), TRUE);
  TEST("l1.search(l2)", l1.search(l2), FALSE);

  TEST_RUN("l2.member(ltemp1, 22)", l2.member(ltemp1, 22),
           (ltemp1.length()==3, ltemp1[0]==22 && ltemp1[1]==33, ltemp1[2]==44
            && l2.value()==22),
           TRUE);
  TEST_RUN("l2.member(ltemp1, 44)", l2.member(ltemp1, 44),
           (ltemp1.length()==1 && ltemp1[0]==44 && l2.value()==44),
           TRUE);
  TEST_RUN("l2.member(ltemp1, 3)", l2.member(ltemp1, 3), (ltemp1.length()==0),
           TRUE);

  TEST_RUN("l2.sublist(ltemp1, l3)", l2.sublist(ltemp1, l3),
           (ltemp1.length()==3 && ltemp1[0]==22, ltemp1[1]==33, ltemp1[2]==44
            && l2.value()==22),
           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), TRUE);
  TEST_RUN("l1.sublist(ltemp1, l2)", l1.sublist(ltemp1, l2), 
           (ltemp1.length()==0), TRUE);
}

void list_int_set_test2() {
  CoolList<int> ltemp1;
  CoolList<int> l5(12,1,55,3,88,77,44,33,22,11,66,2,3);
  CoolList<int> l6(5,11,66,111,33,55);

  ltemp1 = CoolList<int>(4,55,33,11,66);
  TEST_RUN("l5.set_intersection(l6)", l5.set_intersection(l6), (l5==ltemp1), TRUE);

  ltemp1 = CoolList<int>(5,111,55,33,11,66);
  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<int>(5,11,66,111,33,55);
  TEST_RUN("l5.set_xor(l6)", l5.set_xor(l6), (l5==ltemp1), TRUE);

}

void list_int_set_test3() {
  CoolList<int> ltemp1;
  CoolList<int> l7(2,2,3);
  CoolList<int> l8(3,1,2,3);
  CoolList<int> l9 = l7 + l8;
  CoolList<int> l10(3,4,5,6);
  CoolList<int> l11(12,1,55,3,88,77,44,33,22,11,66,2,3);
  CoolList<int> l12(5,11,66,111,33,55);
  CoolList<int> l13;

  ltemp1 = CoolList<int>(5,2,3,1,2,3);
  TEST("l9 = l7 + l8", (l9==ltemp1), TRUE);

  ltemp1 = CoolList<int>(5,2,3,4,5,6);
  TEST_RUN("l7 += l10", l7 += l10, (l7==ltemp1), TRUE);

  ltemp1 = CoolList<int>(4,55,33,11,66);
  TEST_RUN("l11 &= l12", l11 &= l12, (l11==ltemp1), TRUE);

⌨️ 快捷键说明

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