| 1 | #include "CppUTest/TestHarness.h" |
| 2 | #include "CppUTest/TestRegistry.h" |
| 3 | #include "CppUTest/TestOutput.h" |
| 4 | #include "CppUTest/TestPlugin.h" |
| 5 | |
| 6 | static void orig_func1() |
| 7 | { |
| 8 | } |
| 9 | |
| 10 | static void stub_func1() |
| 11 | { |
| 12 | } |
| 13 | |
| 14 | static void orig_func2() |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | static void stub_func2() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | static void (*fp1)(); |
| 23 | static void (*fp2)(); |
| 24 | |
| 25 | TEST_GROUP(SetPointerPluginTest) |
| 26 | { |
| 27 | SetPointerPlugin* plugin_; |
| 28 | TestRegistry* myRegistry_; |
| 29 | StringBufferTestOutput* output_; |
| 30 | TestResult* result_; |
| 31 | |
| 32 | void setup() |
| 33 | { |
| 34 | myRegistry_ = new TestRegistry(); |
| 35 | plugin_ = new SetPointerPlugin("TestSetPlugin"); |
| 36 | myRegistry_->setCurrentRegistry(myRegistry_); |
| 37 | myRegistry_->installPlugin(plugin_); |
| 38 | output_ = new StringBufferTestOutput(); |
| 39 | result_ = new TestResult(*output_); |
| 40 | } |
| 41 | |
| 42 | void teardown() |
| 43 | { |
| 44 | myRegistry_->setCurrentRegistry(NULLPTR); |
| 45 | delete myRegistry_; |
| 46 | delete plugin_; |
| 47 | delete output_; |
| 48 | delete result_; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | class FunctionPointerUtest : public Utest |
| 53 | { |
| 54 | public: |
| 55 | void setup() _override |
| 56 | { |
| 57 | UT_PTR_SET(fp1, stub_func1); |
| 58 | UT_PTR_SET(fp2, stub_func2); |
| 59 | UT_PTR_SET(fp2, stub_func2); |
| 60 | } |
| 61 | void testBody() _override |
| 62 | { |
| 63 | CHECK(fp1 == stub_func1); |
| 64 | CHECK(fp2 == stub_func2); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | class FunctionPointerUtestShell: public UtestShell |
| 69 | { |
| 70 | public: |
| 71 | virtual Utest* createTest() |
| 72 | { |
| 73 | return new FunctionPointerUtest(); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | TEST(SetPointerPluginTest, installTwoFunctionPointer) |
| 78 | { |
| 79 | FunctionPointerUtestShell *tst = new FunctionPointerUtestShell(); |
| 80 | |
| 81 | fp1 = orig_func1; |
| 82 | fp2 = orig_func2; |
| 83 | myRegistry_->addTest(tst); |
| 84 | myRegistry_->runAllTests(*result_); |
| 85 | CHECK(fp1 == orig_func1); |
| 86 | CHECK(fp2 == orig_func2); |
| 87 | LONGS_EQUAL(0, result_->getFailureCount()); |
| 88 | LONGS_EQUAL(2, result_->getCheckCount()); |
| 89 | delete tst; |
| 90 | } |
| 91 | |
| 92 | class MaxFunctionPointerUtest : public Utest |
| 93 | { |
| 94 | public: |
| 95 | int numOfFpSets; |
| 96 | MaxFunctionPointerUtest(int num) : |
| 97 | numOfFpSets(num) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | void setup() |
| 102 | { |
| 103 | for (int i = 0; i < numOfFpSets; ++i) |
| 104 | { |
| 105 | UT_PTR_SET(fp1, stub_func1); |
| 106 | } |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | class MaxFunctionPointerUtestShell: public UtestShell |
| 111 | { |
| 112 | public: |
| 113 | int numOfFpSets; |
| 114 | MaxFunctionPointerUtestShell(int num) : |
| 115 | numOfFpSets(num) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | virtual Utest* createTest() |
| 120 | { |
| 121 | return new MaxFunctionPointerUtest(numOfFpSets); |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | TEST(SetPointerPluginTest, installTooMuchFunctionPointer) |
| 126 | { |
| 127 | MaxFunctionPointerUtestShell *tst = new MaxFunctionPointerUtestShell(SetPointerPlugin::MAX_SET + 1); |
| 128 | myRegistry_->addTest(tst); |
| 129 | |
| 130 | myRegistry_->runAllTests(*result_); |
| 131 | |
| 132 | LONGS_EQUAL(1, result_->getFailureCount()); |
| 133 | delete tst; |
| 134 | } |
| 135 | |
| 136 | static double orig_double = 3.0; |
| 137 | static double* orig_double_ptr = &orig_double; |
| 138 | static double stub_double = 4.0; |
| 139 | |
| 140 | class SetDoublePointerUtest : public Utest |
| 141 | { |
| 142 | public: |
| 143 | void setup() |
| 144 | { |
| 145 | UT_PTR_SET(orig_double_ptr, &stub_double); |
| 146 | } |
| 147 | void testBody() |
| 148 | { |
| 149 | CHECK(orig_double_ptr == &stub_double); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | class SetDoublePointerUtestShell: public UtestShell |
| 154 | { |
| 155 | public: |
| 156 | Utest * createTest() |
| 157 | { |
| 158 | return new SetDoublePointerUtest(); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | TEST(SetPointerPluginTest, doublePointer) |
| 163 | { |
| 164 | SetDoublePointerUtestShell *doubletst = new SetDoublePointerUtestShell(); |
| 165 | myRegistry_->addTest(doubletst); |
| 166 | myRegistry_->runAllTests(*result_); |
| 167 | |
| 168 | CHECK(orig_double_ptr == &orig_double); |
| 169 | LONGS_EQUAL(1, result_->getCheckCount()); |
| 170 | delete doubletst; |
| 171 | } |