waf

FORK: waf with some random patches
git clone https://git.neptards.moe/neptards/waf.git
Log | Files | Refs | README

AccumulatorTest.cpp (1725B)


      1 #include <cppunit/extensions/HelperMacros.h>
      2 #include "Accumulator.h"
      3 #include <string>
      4 #include <vector>
      5 #include <fstream>
      6 
      7 using namespace std;
      8 
      9 class AccumulatorTest : public CPPUNIT_NS::TestFixture
     10 {
     11   private:
     12     CPPUNIT_TEST_SUITE( AccumulatorTest );
     13     CPPUNIT_TEST( test0 );
     14     CPPUNIT_TEST( test1 );
     15     CPPUNIT_TEST_SUITE_END();
     16 
     17   public:
     18     void test0();
     19     void test1();
     20 
     21     void setUp();
     22     void tearDown();
     23 
     24   private:
     25     Accumulator * m_accumulator;
     26 
     27 };
     28 
     29 // Registers the fixture into the 'registry'
     30 CPPUNIT_TEST_SUITE_REGISTRATION( AccumulatorTest );
     31 
     32 void AccumulatorTest::setUp()
     33 {
     34   m_accumulator = new Accumulator;
     35 }
     36 
     37 void AccumulatorTest::tearDown()
     38 {
     39   delete m_accumulator;
     40 }
     41 
     42 static void readlines(const char * filename, vector<string> & lines)
     43 {
     44   string datafile("input");
     45   datafile += "/";
     46   datafile += filename;
     47   ifstream infile;
     48   infile.open(datafile.c_str());
     49   if (infile.is_open())
     50   {
     51     char buffer[BUFSIZ];
     52     while (!infile.eof())
     53     {
     54       infile.getline(buffer, BUFSIZ);
     55       lines.push_back(buffer);
     56     }
     57   }
     58 }
     59 
     60 void AccumulatorTest::test0()
     61 {
     62   vector<string> lines;
     63   readlines("test0.txt", lines);
     64   size_t expected(2);
     65   CPPUNIT_ASSERT_EQUAL(expected, lines.size());
     66   m_accumulator->accumulate(lines[0].c_str());
     67   CPPUNIT_ASSERT_EQUAL(10, m_accumulator->total());
     68 
     69 }
     70 
     71 void AccumulatorTest::test1()
     72 {
     73   vector<string> lines;
     74   readlines("test1.txt", lines);
     75   size_t expected(6);
     76   CPPUNIT_ASSERT_EQUAL(expected, lines.size());
     77   for (vector<string>::const_iterator it(lines.begin());
     78       it != lines.end(); ++it)
     79   {
     80     const string & line(*it);
     81     m_accumulator->accumulate(line.c_str());
     82   }
     83   CPPUNIT_ASSERT_EQUAL(1+2+3+4+5, m_accumulator->total());
     84 
     85 }