Guest User

Untitled

a guest
Jan 8th, 2013
262
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <vector>
  2. #include <gtest/gtest.h>
  3.  
  4. std::size_t const testSize = 10*1000*1000;
  5. typedef unsigned long long TestElement;
  6.  
  7. typedef std::vector<TestElement> TestVector;
  8. typedef TestVector::iterator Iterator;
  9.  
  10. TEST(VectorIteratorSpeedTest, calibrationTest)
  11. {
  12.     TestVector testVector(testSize);
  13.     TestElement &value = testVector.front();
  14.     TestElement &sum = testVector.back();
  15.  
  16.     value = 0;
  17.     sum = 0;
  18.     for (std::size_t i = 0; i < testSize; ++i) {
  19.         sum += ++value;
  20.     }
  21.  
  22.     EXPECT_EQ(static_cast<TestElement>(testSize + 1) * testSize / 2, sum);
  23. }
  24.  
  25. TEST(VectorIteratorSpeedTest, indexTest)
  26. {
  27.     TestVector testVector(testSize);
  28.  
  29.     TestElement value = 0;
  30.     for (std::size_t i = 0; i < testSize; ++i) {
  31.         testVector[i] = ++value;
  32.     }
  33.  
  34.     TestElement sum = 0;
  35.     for (std::size_t i = 0; i < testSize; ++i) {
  36.         sum += testVector[i];
  37.     }
  38.  
  39.     EXPECT_EQ(static_cast<TestElement>(testSize + 1) * testSize / 2, sum);
  40. }
  41.  
  42. TEST(VectorIteratorSpeedTest, iteratorTest)
  43. {
  44.     TestVector testVector(testSize);
  45.     Iterator end = testVector.end();
  46.  
  47.     TestElement value = 0;
  48.     for (Iterator it = testVector.begin(); it != end; ++it) {
  49.         *it = ++value;
  50.     }
  51.  
  52.     TestElement sum = 0;
  53.     for (Iterator it = testVector.begin(); it != end; ++it) {
  54.         sum += *it;
  55.     }
  56.  
  57.     EXPECT_EQ(static_cast<TestElement>(testSize + 1) * testSize / 2, sum);
  58. }
  59.  
  60. TEST(VectorIteratorSpeedTest, indexAtTest)
  61. {
  62.     TestVector testVector(testSize);
  63.  
  64.     TestElement value = 0;
  65.     for (std::size_t i = 0; i < testSize; ++i) {
  66.         testVector.at(i) = ++value;
  67.     }
  68.  
  69.     TestElement sum = 0;
  70.     for (std::size_t i = 0; i < testSize; ++i) {
  71.         sum += testVector.at(i);
  72.     }
  73.  
  74.     EXPECT_EQ(static_cast<TestElement>(testSize + 1) * testSize / 2, sum);
  75. }
  76.  
  77. int main(int argc, char **argv)
  78. {
  79.     ::testing::InitGoogleTest(&argc, argv);
  80.     return RUN_ALL_TESTS();
  81. }
RAW Paste Data