Advertisement
Guest User

labtests

a guest
Apr 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <numeric>
  4. #include <vector>
  5. #include <sstream>
  6. #include <string.h>
  7.  
  8.  
  9. // FreeStoreProxy.h
  10. class FreeStoreProxy {
  11. public:
  12.     static FreeStoreProxy& instance();
  13.    
  14.     void* allocate(size_t size);
  15.    
  16.     void release(void* ptr) ;
  17.    
  18.     int getDanglingPointersCount() const;
  19.    
  20.     void freeAll();
  21.  
  22. private:
  23.     static const int CAPACITY = 1024;
  24.     static FreeStoreProxy _instance;
  25.     int dbgLevel;
  26.     void* entries[CAPACITY];
  27.    
  28.     FreeStoreProxy(int debugLevel=0);
  29.     void* const* position() const;
  30.     void debug(const char* msg) const;
  31. } ;
  32.  
  33. // FreeStoreProxy.cpp
  34. FreeStoreProxy FreeStoreProxy::_instance;
  35.  
  36. inline void* const* FreeStoreProxy::position() const {
  37.     return std::find(entries, entries+CAPACITY, nullptr);
  38. }
  39.    
  40. FreeStoreProxy::FreeStoreProxy(int debugLevel) : dbgLevel{debugLevel} {
  41.     std::fill_n(entries, CAPACITY, nullptr);
  42.     debug("FreeStoreProxy::FreeStoreProxy");
  43. }
  44.  
  45. inline FreeStoreProxy& FreeStoreProxy::instance() {
  46.     return _instance;
  47. }  
  48.  
  49. void* FreeStoreProxy::allocate(size_t size) {
  50.     void** pos = (void **)position();
  51.     if (pos!=entries+CAPACITY) {
  52.         void* ptr = malloc(size);
  53.         *pos = ptr;
  54.         debug("allocate memory");
  55.         return ptr;
  56.     }
  57.     return nullptr;
  58. }
  59.  
  60. void FreeStoreProxy::release(void* ptr) {
  61.     for(int i=0; i<CAPACITY; i++) {
  62.         if (entries[i]==ptr) {
  63.             debug("free memory");
  64.             free(ptr);
  65.             entries[i] = nullptr;
  66.             break;
  67.         }
  68.     }
  69. }
  70.  
  71. int FreeStoreProxy::getDanglingPointersCount() const {
  72.     // int counter = 0;
  73.     // std::for_each(entries, entries+CAPACITY, [&](void* p) { counter += (p!=nullptr); });
  74.     // return counter;
  75.     //return std::accumulate(entries, entries+CAPACITY, 0, [](int a, void* p) { return a + (p!=nullptr); });
  76.     return CAPACITY - std::count(entries, entries+CAPACITY, nullptr);
  77. }
  78.  
  79. void FreeStoreProxy::freeAll() {
  80.     for(int i=0; i<CAPACITY; i++) {
  81.         if (entries[i]!=nullptr) {
  82.             debug("free memory");
  83.             free(entries[i]);
  84.             entries[i] = nullptr;
  85.         }
  86.     }
  87. }
  88.  
  89. void FreeStoreProxy::debug(const char* msg) const {
  90.     if (dbgLevel>0) {
  91.         std::cout << "FreeStoreProxy - " << msg << std::endl;
  92.     }
  93. }
  94.        
  95. void * operator new(size_t size) {
  96.     return FreeStoreProxy::instance().allocate(size);
  97. }
  98.  
  99. void operator delete(void *ptr) noexcept {
  100.     FreeStoreProxy::instance().release(ptr);
  101. }  
  102.  
  103. // Date.h
  104. class Date {
  105. public: // public interface:
  106.     Date(int day, int month, int year);
  107.  
  108.      // non-modifying functions for examining the Date:
  109.      int day() const;
  110.      int month() const;
  111.      int year() const;
  112.      const char* toString() const; // C-style string representation
  113.      bool compare(const Date& other) const;
  114.  
  115. private:
  116.      int _day, _month, _year; // representation
  117.      char _strRepresentation[11]; // C-style string representation
  118. };
  119.  
  120. // Date.cpp
  121. Date::Date(int day, int month, int year)
  122.     : _day{day}, _month{month}, _year{year} {
  123.     // build C-style string representation
  124.     std::stringstream stream;
  125.     stream.rdbuf()->pubsetbuf(_strRepresentation, sizeof(_strRepresentation));
  126.     stream << _day << "/" << _month << "/" << year << (char)0;
  127. }
  128.  
  129. inline int Date::day() const {
  130.      return _day;
  131. }
  132.  
  133. inline int Date::month() const {
  134.      return _month;
  135. }
  136.  
  137. inline int Date::year() const {
  138.      return _year;
  139. }
  140.  
  141. inline const char* Date::toString() const {
  142.     return _strRepresentation;
  143. }
  144.  
  145. inline bool Date::compare(const Date& other) const {
  146.     return _day==other._day && _month==other._month && _year==other._year;
  147. }
  148.  
  149. // Lab5TestSuite.h
  150. class Lab5TestSuite {
  151.    
  152. public:
  153.     Lab5TestSuite();
  154.  
  155.     int runTests();
  156.    
  157. private:
  158.     typedef void (Lab5TestSuite::*Test)() const;
  159.    
  160.     std::vector<std::pair<const char*, Test>> tests;
  161.    
  162.     void registerTest(const char* description, Test fnTest);
  163.    
  164.     void assert(bool expression, const char* context, const char* message) const;
  165.    
  166.     // Actual tests declarations
  167.     void testCtorWithName() const;
  168.  
  169.     void testCtorWithNameAndDate() const;
  170.    
  171.     void testCopyCtor() const;
  172.    
  173.     void testStudentRepresentation() const;  
  174.    
  175.     void testNumberOfInstances() const;
  176.    
  177.     void fnWithReferenceArgument(const Student&) const {};
  178.    
  179.     void fnWithTypeArgument(Student) const {};
  180. } ;
  181.  
  182.  
  183. // Lab5TestSuite.cpp
  184. Lab5TestSuite::Lab5TestSuite() {
  185.     registerTest("ctor with name", &Lab5TestSuite::testCtorWithName);
  186.     registerTest("ctor with name and date", &Lab5TestSuite::testCtorWithNameAndDate);
  187.     registerTest("copy ctor", &Lab5TestSuite::testCopyCtor);
  188.     registerTest("Student representation", &Lab5TestSuite::testStudentRepresentation);
  189.     registerTest("number of instances", &Lab5TestSuite::testNumberOfInstances);
  190. }
  191.  
  192. int Lab5TestSuite::runTests() {
  193.     int failedTests = std::accumulate(tests.begin(), tests.end(), 0, [this](int counter, std::pair<const char*, Test> entry) {
  194.         std::cout << "Running test " << entry.first << "...\n";
  195.         const int before = FreeStoreProxy::instance().getDanglingPointersCount();
  196.         (this->*entry.second)();
  197.         int after = FreeStoreProxy::instance().getDanglingPointersCount() - before;
  198.         if (after!=0) {
  199.             std::cout << "Assertion failed: Memory leak in test [" << entry.first << "]\n";
  200.         }
  201.         return counter + (after!=0);
  202.     });
  203.    
  204.     std::cout << "Tests run: " << tests.size() << ", Passed: " << (tests.size()-failedTests) << ", Failed: " << failedTests << std::endl;
  205.     return 0;
  206. }
  207.    
  208. inline void Lab5TestSuite::assert(bool expression, const char* context, const char* message) const {
  209.     if (!expression) {
  210.         std::cout << "Assertion failed in [" << context << "]: " << message << std::endl;
  211.     }
  212. }
  213.  
  214. inline void Lab5TestSuite::registerTest(const char* description, Test fnTest) {
  215.     tests.push_back(std::pair<const char*, Test>{description, fnTest});
  216. }
  217.  
  218. void Lab5TestSuite::testCtorWithName() const {
  219.     Student john{"John Doe"};
  220.     assert(100==john.id(), "testCtorWithName", "internal representation error");
  221.     assert(0==strcmp("John Doe", john.name()), "testCtorWithName", "internal representation error");
  222.     assert(Date{1, 1, 1970}.compare(john.dob()), "testCtorWithName", "internal representation error");
  223.    
  224.     assert(1==Student::numberOfInstance(), "testCtorWithName", "wrong number of total instances");
  225. }
  226.  
  227. void Lab5TestSuite::testCtorWithNameAndDate() const {
  228.     const Date releaseDate{27, 8, 1974};
  229.     Student mary{"Mary Poppins", releaseDate};
  230.     assert(101==mary.id(), "testCtorWithNameAndDate", "internal representation error");
  231.     assert(0==strcmp("Mary Poppins", mary.name()), "testCtorWithNameAndDate", "internal representation error");
  232.     assert(releaseDate.compare(mary.dob()), "testCtorWithNameAndDate", "internal representation error");
  233.    
  234.     assert(2==Student::numberOfInstance(), "testCtorWithName", "wrong number of total instances");
  235. }
  236.  
  237. void Lab5TestSuite::testCopyCtor() const {
  238.     const Date birthDate{23, 6, 1912};
  239.     Student alan{"Alan Turing", birthDate};
  240.     assert(102==alan.id(), "testCopyCtor", "internal representation error");
  241.     assert(0==strcmp("Alan Turing", alan.name()), "testCopyCtor", "internal representation error");
  242.     assert(birthDate.compare(alan.dob()), "testCopyCtor", "internal representation error");
  243.    
  244.     Student copyOfAlan{alan};
  245.     assert(102==copyOfAlan.id(), "testCopyCtor", "internal representation error");
  246.     assert(0==strcmp("Alan Turing", copyOfAlan.name()), "testCopyCtor", "internal representation error");
  247.     assert(birthDate.compare(copyOfAlan.dob()), "testCopyCtor", "internal representation error");
  248.    
  249.     copyOfAlan.setName("Alan Turing (2)");
  250.     copyOfAlan.setDob(Date{7, 6, 1954});
  251.     assert(102==alan.id(), "testCopyCtor", "internal representation error");
  252.     assert(0==strcmp("Alan Turing", alan.name()), "testCopyCtor", "internal representation error");
  253.     assert(birthDate.compare(alan.dob()), "testCopyCtor", "internal representation error");
  254.    
  255.     assert(102==copyOfAlan.id(), "testCopyCtor", "internal representation error");
  256.     assert(0==strcmp("Alan Turing (2)", copyOfAlan.name()), "testCopyCtor", "internal representation error");
  257.     assert(Date{7, 6, 1954}.compare(copyOfAlan.dob()), "testCopyCtor", "internal representation error");
  258.    
  259.     assert(4==Student::numberOfInstance(), "testCtorWithName", "wrong number of total instances");
  260. }
  261.  
  262. void Lab5TestSuite::testStudentRepresentation() const {
  263.     const Date birthDate{24, 6, 1987};
  264.     Student lionel{"Lionel Messi", birthDate};
  265.     assert(103==lionel.id(), "testStringRepresentation", "internal representation error");
  266.     assert(0==strcmp("Lionel Messi", lionel.name()), "testStringRepresentation", "internal representation error");
  267.     assert(birthDate.compare(lionel.dob()), "testStringRepresentation", "internal representation error");
  268.     assert(0==strcmp("{id: 103, name: Lionel Messi, dob: 24/6/1987}", lionel.toString()), "testStringRepresentation", "string representation error");
  269.    
  270.     assert(5==Student::numberOfInstance(), "testStringRepresentation", "wrong number of total instances");
  271. }
  272.  
  273. void Lab5TestSuite::testNumberOfInstances() const {
  274.     const Date birthDate{7, 2, 1982};
  275.     Student delia{"Delia Matache", birthDate};
  276.     assert(104==delia.id(), "testNumberOfInstances", "internal representation error");
  277.     assert(0==strcmp("Delia Matache", delia.name()), "testNumberOfInstances", "internal representation error");
  278.     assert(birthDate.compare(delia.dob()), "testNumberOfInstances", "internal representation error");
  279.    
  280.     assert(6==Student::numberOfInstance(), "testNumberOfInstances", "wrong number of total instances");
  281.    
  282.     fnWithReferenceArgument(delia);
  283.     assert(6==Student::numberOfInstance(), "testNumberOfInstances", "wrong number of total instances");
  284.  
  285.     fnWithTypeArgument(delia);
  286.     assert(7==Student::numberOfInstance(), "testNumberOfInstances", "wrong number of total instances");
  287.  
  288.     Student flickrCoFounder{"Daniel Stewart Butterfield"};
  289.     assert(105==flickrCoFounder.id(), "testNumberOfInstances", "internal representation error");
  290.     assert(8==Student::numberOfInstance(), "testNumberOfInstances", "wrong number of total instances");
  291. }
  292.  
  293. // main.cpp
  294. int main(void) {
  295.     return Lab5TestSuite{}.runTests();
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement