AccumulatorTest.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "gtest/gtest.h"
  2. #include "Accumulator.h"
  3. #include <string>
  4. #include <vector>
  5. #include <fstream>
  6. using namespace std;
  7. class AccumulatorTest : public testing::Test
  8. {
  9. protected:
  10. virtual void SetUp();
  11. virtual void TearDown();
  12. Accumulator * m_accumulator;
  13. };
  14. void AccumulatorTest::SetUp()
  15. {
  16. m_accumulator = new Accumulator;
  17. }
  18. void AccumulatorTest::TearDown()
  19. {
  20. delete m_accumulator;
  21. }
  22. static void readlines(const char * filename, vector<string> & lines)
  23. {
  24. string datafile("input");
  25. datafile += "/";
  26. datafile += filename;
  27. ifstream infile;
  28. infile.open(datafile.c_str());
  29. if (infile.is_open())
  30. {
  31. char buffer[BUFSIZ];
  32. while (!infile.eof())
  33. {
  34. infile.getline(buffer, BUFSIZ);
  35. lines.push_back(buffer);
  36. }
  37. }
  38. }
  39. TEST_F(AccumulatorTest, test0)
  40. {
  41. vector<string> lines;
  42. readlines("test0.txt", lines);
  43. size_t expected(2);
  44. ASSERT_EQ(expected, lines.size());
  45. m_accumulator->accumulate(lines[0].c_str());
  46. ASSERT_EQ(10, m_accumulator->total());
  47. }
  48. TEST_F(AccumulatorTest, test1)
  49. {
  50. vector<string> lines;
  51. readlines("test1.txt", lines);
  52. size_t expected(6);
  53. ASSERT_EQ(expected, lines.size());
  54. for (vector<string>::const_iterator it(lines.begin());
  55. it != lines.end(); ++it)
  56. {
  57. const string & line(*it);
  58. m_accumulator->accumulate(line.c_str());
  59. }
  60. ASSERT_EQ(1+2+3+4+5, m_accumulator->total());
  61. }