Guest User

Tests to VectorFuncRange

a guest
Feb 9th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.90 KB | None | 0 0
  1. /**
  2.     Author: Djeefther Souza Albuquerque ([email protected])
  3.     Created at 20/09/2015
  4. */
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <utility>
  12. #include <memory>
  13.  
  14. //rename the  main(), main1(), main2(), main3(), main4() and main5() at the end to select wich of the test do you want to try
  15.  
  16. #include "VectorFuncRange.h"
  17.  
  18. struct Min {
  19. public:
  20.     template<class T>
  21.     T operator()(const T& a, const T& b) const {
  22.         return std::min(a, b);
  23.     }
  24. };
  25.  
  26. struct Max{
  27. public:
  28.     template<class T>
  29.     T operator()(const T& a, const T& b) const {
  30.         return std::max(a,b);
  31.     }
  32. };
  33.  
  34. struct Product {
  35. public:
  36.     template<class T>
  37.     T operator()(const T& a, const T& b) const {
  38.         return a*b;
  39.     }
  40. };
  41.  
  42.  
  43. struct MaxRef {
  44. public:
  45.     template<class T>
  46.     T operator()(const T& a, const T& b) const {
  47.         return (*a > *b) ? a : b;
  48.     }
  49. };
  50.  
  51. struct MinRef {
  52. public:
  53.     template<class T>
  54.     T operator()(const T& a, const T& b) const {
  55.         return (*a < *b) ? a : b;
  56.     }
  57. };
  58.  
  59. struct Sum {
  60. public:
  61.     template<class T>
  62.     T operator()(const T& a, const T& b) const {
  63.         return a + b;
  64.     }
  65. };
  66.  
  67. template<class Func>
  68. void tokenizer(const std::string& text, Func func_token, std::vector<std::string>& words) {
  69.     //Tokenizer the original string from a text every time func_token return true values
  70.     words.clear();
  71.  
  72.     bool has_word = false;
  73.     size_t begin_word = 0;
  74.  
  75.     for (size_t i = 0; i < text.size(); i++) {
  76.         bool is_token = func_token(text[i]);
  77.  
  78.         if (is_token && has_word) {
  79.             words.push_back(text.substr(begin_word,i-begin_word));
  80.             has_word = false;
  81.         }
  82.         else if (!is_token && !has_word) {
  83.             has_word = true;
  84.             begin_word = i;
  85.         }
  86.     }
  87.  
  88.     if (has_word) {
  89.         words.push_back(text.substr(begin_word));
  90.     }
  91. }
  92.  
  93.  
  94. #define CATCH_INVALID_COMMAND() catch (...) { std::cout << "Invalid command\n"; }
  95. #define ELSE_INVALID_COMMAND()  else std::cout << "Invalid command\n"
  96. #define IF_COMMAND(level, command) if (words[level] == command)
  97. #define ELIF_COMMAND(level, command) else if (words[level] == command)
  98.  
  99.  
  100.  
  101. std::string rand_string(size_t size) {
  102.     std::string str;
  103.     for (size_t i = 0; i < size; i++) {
  104.         char c = rand() % 26 + 'a' + (rand() & 1 ? 'A' - 'a' : 0);
  105.         str += c;
  106.     }
  107.     return str;
  108. }
  109.  
  110. template<class T,size_t r, size_t c>
  111. class Matrix {
  112.     T data_[r][c];
  113. public:
  114.     Matrix(){
  115.        
  116.     }
  117.     static Matrix Identity() {
  118.         Matrix ret;
  119.         for (size_t i = 0; i < r; i++) {
  120.             for (size_t j = 0; j < c; j++) {
  121.                 if (i == j) {
  122.                     ret[i][j] = 1;
  123.                 }
  124.                 else {
  125.                     ret[i][j] = 0;
  126.                 }
  127.             }
  128.         }
  129.         return ret;
  130.     }
  131.     ~Matrix() {
  132.     }
  133.     const T* operator[](size_t i) const {
  134.         return data_[i];
  135.     }
  136.     T* operator[](size_t i) {
  137.         return data_[i];
  138.     }
  139.     template<size_t n>
  140.     Matrix<T,r,n> operator*(const Matrix<T,c,n>& other) const {
  141.         Matrix<T, r, n> ret;
  142.  
  143.         for (size_t i = 0; i < r; i++) {
  144.             for (size_t j = 0; j < n; j++) {
  145.                 ret[i][j] = 0;
  146.                 for (size_t k = 0; k < c; k++) {
  147.                     ret[i][j] += (*this)[i][k] * other[k][j];
  148.                 }
  149.             }
  150.         }
  151.  
  152.         return ret;
  153.     }
  154.     bool operator ==(const Matrix& other) const {
  155.         auto it = this->begin();
  156.         auto it_end = this->end();
  157.         auto other_it = other.begin();
  158.  
  159.         for (; it != it_end; it++, other_it++) {
  160.             if (*it != *other_it)
  161.                 return false;
  162.         }
  163.  
  164.         return true;
  165.     }
  166.     bool operator != (const Matrix& other) const {
  167.         auto it = this->begin();
  168.         auto it_end = this->end();
  169.         auto other_it = other.begin();
  170.  
  171.         for (; it != it_end; it++, other_it++) {
  172.             if (*it != *other_it)
  173.                 return true;
  174.         }
  175.  
  176.         return false;
  177.     }
  178.     T* begin() {
  179.         return ((T*)data_);
  180.     }
  181.     T* end() {
  182.         return begin() + r*c;
  183.     }
  184.     inline const T* begin() const {
  185.         return cbegin();
  186.     }
  187.     inline const T* end() const {
  188.         return cend();
  189.     }
  190.     const T* cbegin() const {
  191.         return ((T*)data_);
  192.     }
  193.     const T* cend() const  {
  194.         return begin() + r*c;
  195.     }
  196.     constexpr size_t rows() const {
  197.         return r;
  198.     }
  199.     constexpr size_t cols() const {
  200.         return c;
  201.     }
  202. };
  203.  
  204. template<class T, size_t r, size_t c>
  205. std::ostream& operator<<(std::ostream& stream, const Matrix<T, r, c>& obj) {
  206.     for (size_t i = 0; i < obj.rows(); i++) {
  207.         for (size_t j = 0; j < obj.cols(); j++) {
  208.             stream << obj[i][j] << " ";
  209.         }
  210.         stream << "\n";
  211.     }
  212.     return stream;
  213. }
  214.  
  215. int main1() {
  216.     VectorFuncRange<int> vector;
  217.     vector.func(Sum());
  218.  
  219.     std::string input_buffer;
  220.     std::vector<std::string> words;
  221.  
  222.     bool show_print_debug = true;
  223.  
  224.     while (true) {
  225.         std::getline(std::cin, input_buffer);
  226.         tokenizer(input_buffer, isspace, words);
  227.  
  228.         //for (auto & i : words) {
  229.         //  std::cout << "-" << i << "-\n";
  230.         //}
  231.         //std::cout << "\n\n";
  232.  
  233.         if (words.size() > 0) {
  234.             if (words[0] == "push_back") {
  235.                 try {
  236.                     int value = std::stoi(words.at(1));
  237.                     vector.push_back(value);
  238.                 }
  239.                 CATCH_INVALID_COMMAND();
  240.             }
  241.             else if (words[0] == "resize") {
  242.                 try {
  243.                     size_t new_size = std::stoul(words.at(1));
  244.  
  245.                     if (words.size() > 2) {
  246.                         int value = std::stoi(words.at(2));
  247.                         vector.resize(new_size, value);
  248.                     }
  249.                     else {
  250.                         vector.resize(new_size);
  251.                     }
  252.                 }
  253.                 CATCH_INVALID_COMMAND();
  254.             }
  255.             else if (words[0] == "pop_back") {
  256.                 vector.pop_back();
  257.             }
  258.             else if (words[0] == "func") {
  259.                 if (words.size() > 1) {
  260.                     if (words[1] == "sum") {
  261.                         vector.func(Sum());
  262.                     }
  263.                     else if (words[1] == "product") {
  264.                         vector.func(Product());
  265.                     }
  266.                     else if (words[1] == "max") {
  267.                         vector.func(Max());
  268.                     }
  269.                     else if (words[1] == "min") {
  270.                         vector.func(Min());
  271.                     }
  272.                     ELSE_INVALID_COMMAND();
  273.                 }
  274.                 ELSE_INVALID_COMMAND();
  275.             }
  276.             else if (words[0] == "shrink_to_fit") {
  277.                 vector.shrink_to_fit();
  278.             }
  279.             else if (words[0] == "clear") {
  280.                 vector.clear();
  281.             }
  282.             else if (words[0] == "show") {
  283.                 show_print_debug = true;
  284.             }
  285.             else if (words[0] == "no_show") {
  286.                 show_print_debug = false;
  287.             }
  288.             else if (words[0] == "set") {
  289.                 try {
  290.                     size_t index = stoul(words.at(1));
  291.                     int value = stoi(words.at(2));
  292.  
  293.                     vector.set(index, value);
  294.                 }
  295.                 CATCH_INVALID_COMMAND();
  296.             }
  297.             else if (words[0] == "get") {
  298.                 try {
  299.                     size_t index = stoul(words.at(1));
  300.                     std::cout << vector.get(index);
  301.                     std::cout << "\n";
  302.                 }
  303.                 CATCH_INVALID_COMMAND();
  304.             }
  305.             else if (words[0] == "print") {
  306.                 for (size_t i = 0; i < vector.size(); i++) {
  307.                     std::cout << vector[i] << " ";
  308.                 }
  309.                 std::cout << "\n";
  310.             }
  311.             else if (words[0] == "range") {
  312.                 try {
  313.                     size_t begin = std::stoul(words.at(1));
  314.                     size_t end = std::stoul(words.at(2));
  315.  
  316.                     std::cout << vector.range_func(begin, end) << " \n";
  317.                 }
  318.                 CATCH_INVALID_COMMAND();
  319.             }
  320.             else if (words[0] == "all") {
  321.                 std::cout << vector.all_range_func() << "\n";
  322.             }
  323.             ELSE_INVALID_COMMAND();
  324.         }
  325.  
  326.  
  327.         if (show_print_debug) {
  328.             std::cout << "\n";
  329.             vector.print_debug();
  330.             std::cout << "\n";
  331.         }
  332.         else {
  333.             std::cout << "\ndone\n\n";
  334.         }
  335.     }
  336. }
  337.  
  338. int main2() {
  339.     VectorFuncRange<int> v;
  340.     v.func(Sum());
  341.  
  342.     v.print_debug();
  343.  
  344.     for (int i = 0; i < 7; i++) {
  345.         v.push_back(rand() % 10);
  346.         v.print_debug();
  347.     }
  348.  
  349.     std::cout << "\n############\n\n";
  350.  
  351.     for (int i = 0; i < 7; i++) {
  352.         v[i] = (rand() % 10) + 10;
  353.         v.print_debug();
  354.     }
  355.  
  356.     std::cout << "\n############\n\n";
  357.  
  358.     v.resize(3);
  359.     v.print_debug();
  360.  
  361.     v.resize(7, 1);
  362.     v.print_debug();
  363.  
  364.     v.resize(10, -1);
  365.     v.print_debug();
  366.  
  367.     v.resize(3, -1);
  368.     v.print_debug();
  369.  
  370.     v.push_back(1000);
  371.     v.print_debug();
  372.  
  373.     v.func(Product());
  374.     v.print_debug();
  375.  
  376.     v.resize(8);
  377.     v.print_debug();
  378.  
  379.     v.resize(4);
  380.     v.resize(8, 2);
  381.     v.print_debug();
  382.  
  383.     v.func(Sum());
  384.     v.print_debug();
  385.  
  386.     v.push_back(-1);
  387.     v.print_debug();
  388.  
  389.     v.resize(0);
  390.     v.print_debug();
  391.  
  392.     v.resize(8, 2);
  393.     v.print_debug();
  394.  
  395.     v.func(Product());
  396.     v.print_debug();
  397.  
  398.     v[3] = 0;
  399.     v.print_debug();
  400.  
  401.     v[3] = -1;
  402.     v.print_debug();
  403.  
  404.     v[5] = 22;
  405.     v.print_debug();
  406.  
  407.     v.func(Sum());
  408.     v.print_debug();
  409.  
  410.     v.shrink_to_fit();
  411.     v.print_debug();
  412.  
  413.     for (size_t i = 0; i < v.size(); i++) {
  414.         std::cout << v[i] << " ";
  415.     }
  416.  
  417.     getchar();
  418.     return 0;
  419. }
  420.  
  421. int main3() {
  422.     VectorFuncRange<double, Product> v;
  423.  
  424.     for (int i = 0; i < 1000; i++) {
  425.         v.push_back(rand() % 10 + 10);
  426.     }
  427.  
  428.  
  429.  
  430.  
  431.     v.print_debug();
  432.  
  433.     getchar();
  434.     return 0;
  435. }
  436.  
  437. int main4() {
  438.     VectorFuncRange<std::string,Sum> v;
  439.    
  440.     for (int i = 0; i < 100; i++) {
  441.         //v.push_back(rand() % 10);
  442.         v.push_back(rand_string(rand()%10));
  443.     }
  444.  
  445.  
  446.     for (int i = 0; i < 10000000; i++) {
  447.         size_t first = rand() % v.size();
  448.         size_t lenght = rand() % (v.size() - first);
  449.         size_t last = first + lenght;
  450.  
  451.         auto r1 = v.range_func_dumb(first, last);
  452.         auto r2 = v.range_func(first, last);
  453.         //auto r1 = r2;
  454.  
  455.         if (r1 != r2) {
  456.             std::cout << "Test " << i << "\n";
  457.             std::cout << "[" << first << "," << last << ")\n";
  458.             std::cout << r1 << "\n";
  459.             std::cout << r2 << "\n";
  460.             std::cout << "\n";
  461.             getchar();
  462.         }
  463.        
  464.         if (i % 10000 == 0) {
  465.             std::cout << "Test " << i << "...\n";
  466.         }
  467.     }
  468.     return 0;
  469. }
  470.  
  471. int main() {
  472.     //A vector of Matrix with rand range products that match in linear and O(log n) evaluation
  473.     //If they do not match the test is stoped and shows the difference
  474.     VectorFuncRange<Matrix<int,3,3>, Product> v;
  475.     v.neutral_element(Matrix<int,3,3>::Identity());
  476.  
  477.     for (int i = 0; i < 10000; i++) {
  478.         //v.push_back(rand() % 10);
  479.         Matrix<int, 3, 3> a;
  480.         for (auto & i : a) {
  481.             i = 1 + rand() % 10;
  482.         }
  483.         v.push_back(a);
  484.     }
  485.  
  486.  
  487.     for (int i = 0; i < 1000000; i++) {
  488.         size_t first = rand() % v.size();
  489.         size_t lenght = rand() % (v.size() - first);
  490.         size_t last = first + lenght;
  491.  
  492.         auto r1 = v.range_func_dumb(first, last);
  493.         auto r2 = v.range_func(first, last);
  494.         // auto r1 = r2;
  495.  
  496.         if (r1 != r2) {
  497.             std::cout << "Test " << i << "\n";
  498.             std::cout << "[" << first << "," << last << ")\n";
  499.             std::cout << r1 << "\n";
  500.             std::cout << r2 << "\n";
  501.             std::cout << "\n";
  502.  
  503.             getchar();
  504.         }
  505.  
  506.         if (i % 10000 == 0) {
  507.             std::cout << "Test " << i << "...\n";
  508.         }
  509.     }
  510.  
  511.     return 0;
  512. }
Add Comment
Please, Sign In to add comment