Skip to content

Commit

Permalink
Merge pull request #1352 from spark/fix/rgb_on_change_test
Browse files Browse the repository at this point in the history
Test for RGB.onChange() handler leak
  • Loading branch information
technobly authored Jul 10, 2017
2 parents 51400d4 + caa0302 commit af9f76e
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion user/tests/wiring/no_fixture/led.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,45 @@ test(LED_11_MirroringWorks) {
RGB.control(false);
}

#endif
namespace {

// Handler class for RGB.onChange() that counts number of its instances
class OnChangeHandler {
public:
OnChangeHandler() {
++s_count;
}

OnChangeHandler(const OnChangeHandler&) {
++s_count;
}

~OnChangeHandler() {
--s_count;
}

static unsigned instanceCount() {
return s_count;
}

void operator()(uint8_t r, uint8_t g, uint8_t b) {
}

private:
static unsigned s_count;
};

unsigned OnChangeHandler::s_count = 0;

} // namespace

test(LED_12_NoLeakWhenOnChangeHandlerIsOverridden) {
RGB.onChange(OnChangeHandler());
assertEqual(OnChangeHandler::instanceCount(), 1);
RGB.onChange(OnChangeHandler());
assertEqual(OnChangeHandler::instanceCount(), 1); // Previous handler has been destroyed
RGB.onChange(nullptr);
assertEqual(OnChangeHandler::instanceCount(), 0); // Current handler has been destroyed
}

#endif // PLATFORM_ID >= 3

0 comments on commit af9f76e

Please sign in to comment.