Created
April 4, 2022 07:59
-
-
Save 0x2f0713/339a853392aeb4ae49e5f694c6fb27d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#include <cppunit/TestRunner.h> | |
#include <cppunit/TestResult.h> | |
#include <cppunit/TestResultCollector.h> | |
#include <cppunit/extensions/HelperMacros.h> | |
#include <cppunit/BriefTestProgressListener.h> | |
#include <cppunit/extensions/TestFactoryRegistry.h> | |
std::vector<int> listSquare(int a, int b) { | |
if(a < 0) a = 0; | |
if(b<0) b = 0; | |
std::vector<int> x; | |
for(int i = round(sqrt(a));i <= sqrt(b);i++) { | |
if (i*i >= a && i*i <= b) { | |
x.push_back(i*i); | |
} | |
} | |
return x; | |
} | |
struct TestStruct | |
{ | |
std::string testName; | |
std::vector<int> result; | |
std::vector<int> expected; | |
std::string errorMsg; | |
}; | |
void runTestLoop(TestStruct testCases[], int testSize){ | |
int i; | |
for (i = 0; i< testSize; i++){ | |
std::cout << testCases[i].testName + ": "; | |
if (testCases[i].result == testCases[i].expected) | |
{ | |
std::cout << "PASSED \n"; | |
} | |
else | |
{ | |
std::cout << testCases[i].errorMsg; | |
exit(1); | |
} | |
} | |
} | |
class Test: public CPPUNIT_NS::TestCase | |
{ | |
CPPUNIT_TEST_SUITE(Test); | |
CPPUNIT_TEST(testQuestion2); | |
CPPUNIT_TEST(successTestExit); | |
CPPUNIT_TEST_SUITE_END(); | |
public: | |
void setUp(void) {} | |
void tearDown(void) {} | |
protected: | |
void testQuestion2(void) { | |
int testSize = 3; | |
std::string sharedName = "[listSquare test] "; | |
TestStruct listSquareTestCases[testSize] = | |
{ | |
{ | |
sharedName + "test normal 1", | |
listSquare(2, 30), | |
std::vector<int> {4, 9, 16, 25}, | |
"Result should be 4 9 16 25 \n" | |
}, | |
{ | |
sharedName + "test normal 2", | |
listSquare(12, 15), | |
std::vector<int> {}, | |
"Result should be empty \n" | |
}, | |
{ | |
sharedName + "test normal 3", | |
listSquare(-1, 120), | |
std::vector<int> {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, | |
"Result should be 100 81 64 49 36 25 16 9 4 1 0 \n" | |
}, | |
}; | |
runTestLoop(listSquareTestCases, testSize); | |
// exit(0); | |
} | |
void successTestExit(void){ | |
std::cout << "all tests passed! \n"; | |
exit(0); | |
} | |
}; | |
CPPUNIT_TEST_SUITE_REGISTRATION(Test); | |
int main() | |
{ | |
CPPUNIT_NS::TestResult controller; | |
CPPUNIT_NS::TestResultCollector result; | |
controller.addListener(&result); | |
CPPUNIT_NS::BriefTestProgressListener progress; | |
controller.addListener(&progress); | |
CPPUNIT_NS::TestRunner runner; | |
runner.addTest(CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest()); | |
runner.run(controller); | |
return result.wasSuccessful() ? 0 : 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment