home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / splash / spltest.cpp < prev    next >
Text File  |  1993-01-15  |  13KB  |  372 lines

  1. #ifdef    TEST
  2.  
  3. #include <iostream.h>
  4. /*
  5.  * V1.8
  6.  */
  7.  
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <stdio.h>
  11.  
  12. #ifdef    __TURBOC__
  13. #pragma hdrstop
  14. #endif
  15.  
  16. #include "splash.h"
  17.  
  18. int main()
  19. {
  20. SPList<int> il, il1, il2, il3;
  21. SPStringList x, y, z;
  22. int n;
  23.  
  24.     if(x) cout << "x is not empty" << endl;
  25.     else cout << "x is empty" << endl;
  26.  
  27.     if(x.isempty()) cout << "x.isempty() is true" << endl;
  28.     else cout << "x.isempty() is false" << endl;
  29.  
  30.     n= x.split("a b c d e f");
  31.  
  32.     if(x) cout << "x is not empty" << endl;
  33.     else cout << "x is empty" << endl;
  34.  
  35.     if(x.isempty()) cout << "x.isempty() is true" << endl;
  36.     else cout << "x.isempty() is false" << endl;
  37.  
  38.     cout << "x.split(a b c d e f)= " << n << ": " << x << endl;
  39.     cout << "x[0] = " << x[0] << endl;
  40.     z= x; z[0]= "x";
  41.     cout << "z= x; z[0]=\"x\" " << "z: " << z << endl;
  42.  
  43.     SPString ss("1.2.3.4.5.6.7.8.9.0");
  44.     y= ss.split("\\.");
  45.     cout << "ss= " << ss << ", y= ss.split(\"\\.\"), y=" << endl << y << endl;
  46.     cout << "y.join(\" \")" << y.join(" ") << endl;       
  47.     {
  48.     SPString xx= "a b c\nd e\tf   g";
  49.         cout << xx << endl << "xx.split()= " << xx.split() << endl;
  50.     xx= "a b c d e f g";
  51.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  52.     xx= "  a b c d e f g hi  ";
  53.     cout << xx << endl << "xx.split(\"\")= " << xx.split("") << endl;
  54.     xx= "a,b,c,d,,e,f,g,,,,";
  55.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  56.     xx= "a,b,c,d,,e,f,g,,";
  57.     cout << xx << endl << "xx.split(\",\", 5)= " << xx.split(",", 5) << endl;
  58.     xx= " a b c d e f g  ";
  59.     cout << xx << endl << "xx.split(\" \")= " << xx.split(" ") << endl;
  60.     xx= "a b c d,e,f g";
  61.     cout << xx << endl << "xx.split(\"([ ,])+\")= " << xx.split("([ ,])+") << endl;
  62.     xx= ",,,,";
  63.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  64.     xx= "";
  65.     cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
  66.      xx= "   a b c\td    e\nf  g   ";
  67.     cout << xx << endl << "xx.split(\"' '\")= " << xx.split("' '") << endl;
  68.    }
  69.  
  70.     cout << "x = " << x << endl;
  71.  
  72.     cout << "x.pop() : " << x.pop() << ", " ;
  73.     cout << x.pop() << endl << "x= " << x << endl;;
  74.     cout << "x.shift() : " << x.shift() << ", ";
  75.     cout << x.shift() << endl<< "x= " << x << endl;
  76.     
  77.     x.unshift(y);
  78.     cout << "x.unshift(y): " << x << endl;
  79.     
  80.     if(il) cout << "il is not empty" << endl;
  81.     else cout << "il is empty" << endl;
  82.     
  83.     il.push(1); il.push(2); il.push(3); il.push(4);
  84.  
  85.     if(il) cout << "il is not empty" << endl;
  86.     else cout << "il is empty" << endl;
  87.     
  88.     cout << "il(1, 2, 3, 4) : " << il << endl;
  89.     il3= il; il3[0]= 9999;
  90.     cout << "il3= il; il3[0]= 9999; il3 = " << il3 << endl << "il= " << il << endl;
  91.  
  92.     il1= il.reverse();
  93.     cout << "il.reverse: " << il1 << endl;
  94.  
  95.     cout << "il1.sort(): " << il1.sort() << endl;
  96.  
  97.     y.reset();
  98.     y.push("one"); y.push("two"); y.push("three"); y.push("four");
  99.     cout << "y = " << endl << y;
  100.     cout << "y.reverse() " << y.reverse() << endl;
  101.     cout << "y.sort() " << y.sort() << endl;
  102.     cout << "y.sort().reverse() " << y.sort().reverse() << endl;
  103.         
  104.     il2.push(3); il2.push(4);
  105.     cout << "il2.push(3, 4) : " << il2 << endl;
  106.  
  107.     il.push(il2);    
  108.     cout << "il.push(il2) : " << il << endl;
  109.     
  110.     cout << "il.pop() : " << il.pop() << ", ";
  111.     cout << il.pop() << endl;
  112.     
  113.     il.unshift(il2);    
  114.     cout << "il.unshift(il2) : " << il << endl;
  115.     
  116.     cout << "il.shift() : " << il.shift() << ", ";
  117.     cout << il.shift() << endl;
  118.  
  119.     il.reset();
  120.     il.push(1); il.push(2);
  121.     if(il.shift() != 1) cout << "FIFO1 error" << endl;
  122.     if(il.shift() != 2) cout << "FIFO2 error" << endl;
  123.  
  124.     for(int i=0;i<100;i++) il.push(i);
  125.     i= 0;
  126.     while(il){
  127.     if(il.shift() != i)  cout << "FIFO3 error" << endl;
  128.     i++;
  129.     }
  130.     if(i != 100) cout << "FIFO over/under run" << endl;
  131.     
  132.     cout << "testing splice:" << endl;
  133.     x.reset();
  134.     x.split("a b c d e f g h i");
  135.     cout << "x = " << x << endl;
  136.     z= x.splice(2, 3);
  137.     cout << "z= x.splice(2, 3): z= " << z << endl << "x = " << x << endl;
  138.     cout << "x.splice(2, 0, z): " << x.splice(2, 0, z);
  139.     cout << "x= " << x << endl;
  140.     cout << "z.splice(1, 1, x): " << z.splice(1, 1, x);
  141.     cout << "z= " << z << endl;
  142.     cout << "x= " << x << endl;
  143.     cout << "z.splice(20, 1, x): " << z.splice(20, 1, x);
  144.     cout << "z= " << z << endl;
  145.     
  146.  // test auto expand
  147.     SPList<int> ile;
  148.     ile[3]= 3;
  149.     cout << ile.scalar() << ", " << ile[3] << endl;
  150.     ile[100]= 1234;
  151.     ile[0]= 5678;
  152.     cout << ile.scalar() << ", " << ile[0] << ", " << ile[100] << endl;
  153.     SPList<int> ile2;
  154.     for(i=0;i<=100;i++) ile2[i]= i;
  155.     for(i=200;i>100;i--) ile2[i]= i;
  156.     for(i=0;i<=200;i++) if(ile2[i] != i) cout << "error at index " << i << endl;
  157.     cout << "Index check done" << endl;
  158.     cout << ile2.scalar() << ", " << ile2[0] << ", " << ile2[200] << endl;
  159.  
  160. // test Regexp stuff
  161.     cout << endl << "testing regexp stuff:" << endl;
  162.     x.reset();
  163.     cout << "x.m(\".*X((...)...(...))\", \"12345Xabcxyzdef\") returns " <<
  164.      x.m(".*X((...)...(...))", "12345Xabcxyzdef") << endl;
  165.     cout << "subs matched = " << x << endl;
  166.  
  167.     Regexp rexp("abc");
  168.     SPString rst("12345Xabcxyzdef");
  169.     cout << "rst.m(rexp) returns " << rst.m(rexp) << endl;
  170.     
  171.     cout << endl << "testing grep:" << endl;
  172.     x.reset();
  173.     x.push("abcd"); x.push("a2345"); x.push("X2345"); x.push("Xaaaaa"); x.push("aaaaa");
  174.     
  175.     y= x.grep("^a.*");
  176.     cout << "x: " << endl << x << endl << "grep(^a.*)" << endl;
  177.     cout << "Expect 3 matches:" << endl << y << endl;
  178.     {
  179.     SPString s1("abcdef");
  180.         cout << "s1= " << s1 << ", s1.m(\"^cde\") : " << s1.m("^cde") << endl;
  181.     cout << "s1= " << s1 << ", s1.m(\"^..cde\") : " << s1.m("^..cde") << endl;
  182.     }
  183.     {
  184.     SPStringList sl;
  185.     SPString str= "ab cd ef";
  186.     sl = m("(..) (..)", str);
  187.     cout << "sl = m(\"(..) (..)\", \"ab cd ef\"); sl = " << endl <<
  188.          sl << endl;
  189.     }
  190.  
  191.     {
  192.     Regexp ncr("abc", Regexp::nocase);
  193.     Regexp cr("abc");
  194.     SPString s= "ABC";
  195.     cout << "s= " << s << ": s.m(ncr)= " << s.m(ncr) << endl;
  196.     cout << "s= " << s << ": s.m(cr)= " << s.m(cr) << endl;
  197.     cout << "s.m(\"abc\", \"i\")= " << s.m("abc", "i") << endl;
  198.     cout << "s.m(\"abc\")= " << s.m("abc") << endl;
  199.     }
  200.     
  201. // Test strings
  202.     cout << "test string stuff:" << endl;
  203.  
  204.     SPString s1("string1"), s2, s3;
  205.     const char *s= s1;
  206.  
  207.     cout << "Empty string: " << s2 << " length= " << s2.length()
  208.      << ",  strlen(s2) = " << strlen(s2) << endl;
  209.     
  210.     cout << "s1:" << s1 << endl;
  211.     cout << "s[0]= " << s[0] << ", s[5]= " << s[5] << endl;
  212. //    s[2]= 'X';
  213. //    cout << "s[2]= 'X', s= " << s << endl;
  214. //    s[2]= 'r';
  215.     
  216.     cout << "const char *s= s1: s= " << s << endl;
  217.     s2= s1;
  218.     cout << "s2=s1,  s2:" << s2 << endl;
  219.     s1.chop();
  220.     cout << "s1.chop()" << s1 << endl;
  221.     s3= s;
  222.     cout << "s3= s: s3 = " << s3 << endl;
  223.     cout << "index(\"ri\") in " << s1 << ": " << s1.index("ri") << endl;
  224.     s3= "1";
  225.     cout << "index(" << s3 << ") in " << s1 << ": " << s1.index(s3) << endl;
  226.     s3= "abcabcabc";
  227.     cout << "rindex(abc) in" << s3 << ": " << s3.rindex("abc") << endl;
  228.     cout << "rindex(abc, 5) in" << s3 << ": " << s3.rindex("abc", 5) << endl;
  229.  
  230. // test substrings
  231.     cout << "substr(5, 3) in " << s3 << ": " << s3.substr(5, 3) << endl;
  232.     s3.substr(5, 3)= "XXX";
  233.     cout << "s3.substr(5, 3) = \"XXX\"" << s3 << endl;
  234.     s3.substr(5, 3)= s1;
  235.     cout << "s3.substr(5, 3) = s1" << s3 << endl;
  236.     s3.substr(5, 3)= s1.substr(1, 3);
  237.     cout << "s3.substr(5, 3) = s1.substr(1, 3)" << s3 << endl;
  238.     s3.substr(0, 6)= s1.substr(0, 3);
  239.     cout << "s3.substr(0, 6) = s1.substr(0, 3)" << s3 << endl;
  240.     s3.substr(-3, 2)= s1.substr(0, 2);
  241.     cout << "s3.substr(-3, 2) = s1.substr(0, 2)" << s3 << endl;
  242.  
  243. // test overlapping substrings
  244.     s1= "1234567890";
  245.     cout << "s1 = " << s1 << endl;
  246.     s1.substr(0, 10)= s1.substr(1, 9);
  247.     cout << "s1.substr(0, 10)= s1.substr(1, 9) " << s1 << endl;
  248.     s1= "1234567890";
  249.     cout << "s1 = " << s1 << endl;
  250.     s1.substr(1, 9)= s1.substr(0, 10);
  251.     cout << "s1.substr(1, 9)= s1.substr(0, 10) " << s1 << endl;
  252.  
  253.     // test over-large substrings
  254.     s1= "1234567890"; s1.substr(7, 10)= "abcdefghij";
  255.     cout << "s1.substr(7, 10)= \"abcdefghij\" " << s1 << endl;
  256.     s1= "1234567890"; s1.substr(10, 5)= "abcdefghij";
  257.     cout << "s1.substr(10, 5)= \"abcdefghij\" " << s1 << endl;
  258.     s1= "1234567890"; s1.substr(20, 1)= "abcdefghij";
  259.     cout << "s1.substr(20, 1)= \"abcdefghij\" " << s1 << endl;
  260.  
  261.     s1= "abcdef"; s2= "123456";
  262.      
  263.     cout << s1 << " + " << s2 << ": " << s1 + s2 << endl;
  264.     cout << s1 << " + " << "\"hello\"= " << s1 + "hello" << endl;
  265.     cout << "\"hello\"" << " + " << s1 << "= " << "hello" + s1 << endl;
  266.     cout << s1 << " + \'x\' = " << s1 + 'x' << endl;
  267.     
  268.     s1= "abc"; s2= "def"; s3= "abc";
  269.     cout << s1 << " == " << s2 << ": " << (s1 == s2) << endl; 
  270.     cout << s1 << " != " << s2 << ": " << (s1 != s2) << endl;
  271.     cout << s1 << " == " << s3 << ": " << (s1 == s3) << endl; 
  272.     cout << s1 << " != " << s3 << ": " << (s1 != s3) << endl;
  273.     cout << s1 << " < " << s2 << ": " << (s1 < s2) << endl; 
  274.     cout << s1 << " > " << s2 << ": " << (s1 > s2) << endl; 
  275.     cout << s1 << " <= " << s2 << ": " << (s1 <= s2) << endl; 
  276.     cout << s1 << " >= " << s3 << ": " << (s1 >= s3) << endl; 
  277.  
  278.     cout << s1 << " == abc:" << (s1 == "abc") << endl; 
  279.     cout << "abc == " << s1 << ("abc" == s1) << endl; 
  280.  
  281.     cout << s1 << " != abc:" << (s1 != "abc") << endl; 
  282.     cout << "abc != " << s1 << ("abc" != s1) << endl; 
  283.  
  284. // Test the tr() functions
  285.     s1= "abcdefghi";
  286.     cout << "s1 = " << s1;
  287.     cout << ", s1.tr(\"ceg\", \"123\") = " << s1.tr("ceg", "123");
  288.     cout << ", s1 = " << s1 << endl;
  289.     s1= "abcdefghi";
  290.     cout << "s1.tr(\"a-z\", \"A-Z\") = " << s1.tr("a-z", "A-Z");
  291.     cout << ", s1 = " << s1 << endl;
  292.     s1= "abcdefghi";
  293.     cout << "s1.tr(\"efg\", \"\") = " << s1.tr("efg", "");
  294.     cout << ", s1 = " << s1 << endl;
  295.     s1= "abcdefghi";
  296.     cout << "s1.tr(\"ac-e\", \"X\") = " << s1.tr("ac-e", "X");
  297.     cout << ", s1 = " << s1 << endl;
  298.     s1= "abcdefghiiii";
  299.     cout << "s1 = " << s1;
  300.     cout << ", s1.tr(\"ac-e\", \"X\", \"s\") = " << s1.tr("ac-e", "X", "s");
  301.     cout << ", s1 = " << s1 << endl;
  302.     s1= "abcdefghi";
  303.     cout << "s1.tr(\"ac-e\", \"\", \"d\") = " << s1.tr("ac-e", "", "d");
  304.     cout << ", s1 = " << s1 << endl;
  305.     s1= "abcdefghi";
  306.     cout << "s1.tr(\"ac-e\", \"d\", \"d\") = " << s1.tr("ac-e", "d", "d");
  307.     cout << ", s1 = " << s1 << endl;
  308.     s1= "abcdefghi";
  309.     cout << "s1.tr(\"ac-e\", \"\", \"cd\") = " << s1.tr("ac-e", "", "cd");
  310.     cout << ", s1 = " << s1 << endl;
  311.     s1= "bookkeeper";
  312.     cout << s1;
  313.     cout << ": s1.tr(\"a-zA-Z\", \"\", \"s\") = " << s1.tr("a-zA-Z", "", "s");
  314.     cout << ", s1 = " << s1 << endl;
  315.     s1= "abc123def456ghi";
  316.     cout << s1;
  317.     cout << ": s1.tr(\"a-zA-Z\", \" \", \"c\") = " << s1.tr("a-zA-Z", " ", "c");
  318.     cout << ", s1 = " << s1 << endl;
  319.     s1= "abc123def456ghi789aaa";
  320.     cout << s1;
  321.     cout << ": s1.tr(\"a-zA-Z\", \" \", \"cs\") = " << s1.tr("a-zA-Z", " ", "cs");
  322.     cout << ", s1 = " << s1 << endl;
  323.     s1= "abcdddaaaxxx";
  324.     cout << s1;
  325.     cout << ": s1.tr(\"a\", \"d\", \"s\") = " << s1.tr("a", "d", "s");
  326.     cout << ", s1 = " << s1 << endl;
  327.     
  328. // Test substitute command
  329.     s1= "abcdefghi";
  330.     cout << s1;
  331.     cout <<" s1.s(\"def\", \"FED\") = " << s1.s("def", "FED");
  332.     cout << ", s1= " << s1 << endl;
  333.     s1= "abcDEFghi";
  334.     cout << s1;
  335.     cout <<" s1.s(\"def\", \"FED\") = " << s1.s("def", "FED");
  336.     cout << ", s1= " << s1 << endl;
  337.     s1= "abcDEFghi";
  338.     cout << s1;
  339.     cout <<" s1.s(\"def\", \"FED\", \"i\") = " << s1.s("def", "FED", "i");
  340.     cout << ", s1= " << s1 << endl;
  341.     s1= "abcdefghi";
  342.     cout << s1;
  343.     cout <<" s1.s(\"(...)(...)\", \"\\$,$&,$2 $1\") = " <<
  344.          s1.s("(...)(...)", "\\$,$&,$2 $1");
  345.     cout << ", s1= " << s1 << endl;
  346.     s1= "abcdefabcghiabc";
  347.     cout << s1;
  348.     cout <<" s1.s(\"abc\", \"XabcX\", \"g\") = " << s1.s("abc", "XabcX", "g");
  349.     cout << ", s1= " << s1 << endl;
  350.     s1= "abcdefabcghiabc";
  351.     cout << s1;
  352.     cout <<" s1.s(\"abc\", \"X\", \"g\") = " << s1.s("abc", "X", "g");
  353.     cout << ", s1= " << s1 << endl;
  354.     s1= "abcdefabcghiabc";
  355.     cout << s1;
  356.     cout <<" s1.s(\"abc(.)\", \"X$1abcX$1\", \"g\") = " <<
  357.          s1.s("abc(.)", "X$1abcX$1", "g");
  358.     cout << ", s1= " << s1 << endl;
  359.     s1= "abcdefabcghiabc";
  360.     cout << s1;
  361.     cout <<" s1.s(\"(.)abc\", \"$1X$1abcX\", \"g\") = " <<
  362.          s1.s("(.)abc", "$1X$1abcX", "g");
  363.     cout << ", s1= " << s1 << endl;
  364.     s1= "1234567890";
  365.     cout << s1;
  366.     cout <<" s1.s(\"(.)(.)\", \"$2$1\", \"g\") = " <<
  367.          s1.s("(.)(.)", "$2$1", "g");
  368.     cout << ", s1= " << s1 << endl;
  369.  
  370. }
  371. #endif
  372.