Advertisement
GeeckoDev

HybridTrie test template2

Dec 10th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include "hybridtrie.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. #define ASSERT(p) \
  7.     if (!(p)) { return true; }
  8. #define SUCCESS() \
  9.     return false;
  10.  
  11. static bool test_exists()
  12. {
  13.     cout << " exists";
  14.     HybridTrie t;
  15.  
  16.     ASSERT(!t.exists(""))
  17.  
  18.     t.insert("algav");
  19.     ASSERT(t.exists("algav") && !t.exists("alga") && !t.exists("a"))
  20.  
  21.     t.insert("alga");
  22.     ASSERT(t.exists("alga"))
  23.  
  24.     SUCCESS()
  25. }
  26.  
  27. static bool test_countWords()
  28. {
  29.     cout << " countWords";
  30.     HybridTrie t;
  31.  
  32.     t.insert("");
  33.     ASSERT(t.countWords() == 0)
  34.  
  35.     t.insert("algav");
  36.     ASSERT(t.countWords() == 1)
  37.  
  38.     t.insert("algav");
  39.     ASSERT(t.countWords() == 1)
  40.  
  41.     t.insert("alga");
  42.     ASSERT(t.countWords() == 2)
  43.  
  44.     t.insert("test");
  45.     ASSERT(t.countWords() == 3)
  46.  
  47.     t.insert("akga");
  48.     ASSERT(t.countWords() == 4)
  49.  
  50.     SUCCESS()
  51. }
  52.  
  53. static bool test_listWords()
  54. {
  55.     cout << " listWords";
  56.     HybridTrie t;
  57.  
  58.     ASSERT(t.listWords().empty())
  59.  
  60.     t.insert("algav");
  61.     t.insert("abc");
  62.     t.insert("helloworld");
  63.     t.insert("z");
  64.     t.insert("because");
  65.     t.insert("caused");
  66.     t.insert("cause");
  67.     t.insert("cauded");
  68.     t.insert("cauzed");
  69.     t.insert("hellozorld");
  70.     t.insert("algav");
  71.     ASSERT(t.listWords().at(0) == "abc")
  72.     ASSERT(t.listWords().at(1) == "algav")
  73.     ASSERT(t.listWords().at(2) == "because")
  74.     ASSERT(t.listWords().at(3) == "cauded")
  75.     ASSERT(t.listWords().at(4) == "cause")
  76.     ASSERT(t.listWords().at(5) == "caused")
  77.     ASSERT(t.listWords().at(6) == "cauzed")
  78.     ASSERT(t.listWords().at(7) == "helloworld")
  79.     ASSERT(t.listWords().at(8) == "hellozorld")
  80.     ASSERT(t.listWords().at(9) == "z")
  81.     ASSERT(t.listWords().size() == 10)
  82.  
  83.     SUCCESS()
  84. }
  85.  
  86. static bool test_countNil()
  87. {
  88.     cout << " countNil";
  89.     HybridTrie t;  
  90.  
  91.     ASSERT(t.countNil() == 3)
  92.  
  93.     t.insert("m");
  94.     ASSERT(t.countNil() == 3)
  95.  
  96.     t.insert("ma");
  97.     ASSERT(t.countNil() == 5)
  98.    
  99.     t.insert("a");
  100.     ASSERT(t.countNil() == 7)
  101.  
  102.     t.insert("z");
  103.     ASSERT(t.countNil() == 9)
  104.  
  105.     SUCCESS()  
  106. }
  107.  
  108. static bool test_height()
  109. {
  110.     cout << " height";
  111.     HybridTrie t;  
  112.  
  113.     ASSERT(t.height() == 1)
  114.  
  115.     t.insert("m");
  116.     ASSERT(t.height() == 1)
  117.  
  118.     t.insert("ma");
  119.     ASSERT(t.height() == 2)
  120.    
  121.     t.insert("a");
  122.     ASSERT(t.height() == 2)
  123.  
  124.     t.insert("z");
  125.     ASSERT(t.height() == 2)
  126.  
  127.     t.insert("abcdefghijklmnopqrstuvwxyz");
  128.     ASSERT(t.height() == 27)
  129.  
  130.     SUCCESS()  
  131. }
  132.  
  133. static bool test_meanDepth()
  134. {
  135.     cout << " meanDepth";
  136.     HybridTrie t;
  137.  
  138.     ASSERT(t.meanDepth() == 1)
  139.  
  140.     t.insert("bbb");
  141.     ASSERT(t.meanDepth() == 3)
  142.  
  143.     t.insert("cb");
  144.     ASSERT(t.meanDepth() == 3)
  145.  
  146.     t.insert("abcde");
  147.     ASSERT(t.meanDepth() == 4)
  148.  
  149.     SUCCESS()
  150. }
  151.  
  152. static bool test_prefix()
  153. {
  154.     cout << " prefix";
  155.     HybridTrie t;
  156.  
  157.     ASSERT(t.prefix("algav") == 0)
  158.  
  159.     SUCCESS()
  160. }
  161.  
  162. static bool (*test[])() =
  163. {
  164.     test_exists,
  165.     test_countWords,
  166.     test_listWords,
  167.     test_countNil,
  168.     test_height,
  169.     test_meanDepth,
  170.     test_prefix
  171. };
  172.  
  173. int main(int argc, char *argv[])
  174. {
  175.     int n = (sizeof(test) / sizeof(bool(*)()));
  176.     int count = 0;
  177.  
  178.     for (int i = 0; i < n; i++) {
  179.         cout << "test " << i;
  180.         if (test[i]()) {
  181.             cout << " failed" << endl;
  182.         }
  183.         else {
  184.             cout << " passed" << endl;
  185.             count++;
  186.         }
  187.     }
  188.  
  189.     cout << count << '/' << n << " tests passed." << endl;
  190.  
  191.     return (count != n);
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement