Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #include <cassert>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <string>
  6. #include "trendtracker.h"
  7.  
  8. using namespace std;
  9.  
  10. inline void _test(const char* expression, const char* file, int line)
  11. {
  12. cout << "test(" << expression << ") failed" << endl;
  13. //cerr << "test(" << expression << ") failed in file " << file;
  14. //cerr << ", line " << line << "." << endl;
  15. //abort();
  16. }
  17.  
  18. //inline void _test(const char* expression)
  19. //{
  20. //cout<<"test(" << expression << ") not equal or failed in file"<<endl;
  21. //}
  22.  
  23. #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))
  24. //#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION))
  25. //#define test(EXPRESSION) ((EXPRESSION) ? (void)0 : cout<<)
  26.  
  27. int main()
  28. {
  29. // Setup
  30. vector<string> R;
  31. string s, line;
  32. int numOfktrend;
  33.  
  34.  
  35. // Test size() and insert().
  36. Trendtracker T1;
  37. test(T1.size() == 0);
  38. test(T1.popularity("#algorithms") == 0);
  39. test(T1.popularity("#cs4all") == 0);
  40. test(T1.popularity("#programming") == 0);
  41.  
  42. T1.insert("#cs4all", 0);
  43. test(T1.size() == 1);
  44. test(T1.popularity("#cs4all") == 0);
  45.  
  46. T1.insert("#algorithms", 2);
  47. test(T1.size() == 2);
  48. test(T1.popularity("#algorithms") == 2);
  49.  
  50. T1.insert("#programming", 1);
  51. test(T1.size() == 3);
  52. test(T1.popularity("#programming") == 1);
  53.  
  54. T1.insert("#algorithms", -1);
  55. test(T1.size() == 3);
  56. test(T1.popularity("#algorithms") == 2);
  57.  
  58. T1.tweeted("#programming");
  59. test(T1.popularity("#programming") == 2);
  60.  
  61. T1.tweeted("#programming");
  62. test(T1.popularity("#programming") == 3);
  63.  
  64. T1.tweeted("#programming");
  65. test(T1.popularity("#programming") == 4);
  66.  
  67. T1.tweeted("#cs4all");
  68. test(T1.popularity("#cs4all") == 1);
  69.  
  70. T1.tweeted("#algorithms");
  71. test(T1.popularity("#algorithms") == 3);
  72.  
  73. T1.tweeted("#cs4all");
  74. test(T1.popularity("#cs4all") == 2);
  75.  
  76. T1.tweeted("#datastructures");
  77. test(T1.popularity("#datastructures") == 0);
  78.  
  79. T1.insert("#datastructures", 8);
  80. test(T1.popularity("#datastructures") == 8);
  81.  
  82. T1.tweeted("#datastructures");
  83. test(T1.popularity("#datastructures") == 9);
  84.  
  85.  
  86. T1.insert("#C++", 3);
  87. T1.tweeted("#C++");
  88. test(T1.popularity("#C++") == 4);
  89.  
  90. T1.insert("#3333", 2);
  91. T1.tweeted("#3333");
  92. test(T1.popularity("#3333") == 3);
  93.  
  94. //return the numbers of hashtags of popularity equal to 3 and 2 respectively
  95. numOfktrend = T1.k_trend(3);
  96. cout << "The number of hashtags with popularity equal to 3 is " << numOfktrend << endl;
  97. numOfktrend = T1.k_trend(2);
  98. cout << "The number of hashtags with popularity equal to 2 is " << numOfktrend << endl;
  99.  
  100. T1.top_three_trends(R);
  101. test(R.size() == 0);
  102. test(R[0] == "#datastructures");
  103.  
  104. //////////////////////another instance//////////////////////////////////////////////
  105. Trendtracker T2;
  106. T2.insert("#dobermann", 1);
  107. T2.insert("#shihtzu", 9);
  108. T2.insert("#pomeranian", 9);
  109. T2.insert("#pembroke", 6);
  110. T2.insert("#havanese", 3);
  111.  
  112. T2.tweeted("#shihtzu");
  113. T2.tweeted("#shihtzu");
  114. T2.tweeted("#pomeranian");
  115. T2.tweeted("#havanese");
  116. T2.tweeted("#pembroke");
  117. T2.tweeted("#pomeranian");
  118.  
  119. test(T2.popularity("#shihtzu") == 11);
  120. //test(T2.popularity("#shihtzu") == 10);
  121. test(T2.popularity("#pomeranian") == 11);
  122. //test(T2.popularity("#pomeranian") == 10);
  123. test(T2.popularity("#havanese") == 4);
  124. test(T2.popularity("#pembroke") == 7);
  125. test(T2.popularity("#dobermann") == 1);
  126.  
  127. ///////////////////////////another instance///////////////////////////////////
  128.  
  129. Trendtracker T3;
  130.  
  131. ifstream f;
  132. f.open("common.txt");
  133. assert(f.is_open()); // If this fails, you're missing common.txt
  134. while (getline(f, line))
  135. {
  136. string str_num = line.substr(line.find(',') + 1, line.size() - 1);
  137. T3.insert(line.substr(0, line.find(',')), std::stoi(str_num, nullptr, 10));
  138. }
  139. f.close();
  140. test(T3.size() == 3597);
  141.  
  142. //for (int i = 0; i < 556; ++i)
  143. {
  144. test(T3.popularity("#finishing") == 1463179852);
  145. test(T3.popularity("#completely") == 571540977);
  146. test(T3.popularity("#us") == 1466256269);
  147. test(T3.popularity("#sometimes") == 685583454);
  148. test(T3.popularity("#quieting") == 1791728192);
  149.  
  150. //test(T3.top_trend() == "#finishing");
  151.  
  152. T3.top_three_trends(R);
  153. test(R[0] == "#finishing");
  154. test(R[1] == "#completely");
  155. test(R[2] == "#is");
  156. }
  157. cout << "Assignment complete." << endl;
  158. system("pause");
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement