Example greentea tests with Blinky library

Dependencies:   Blinky

Committer:
sarahmarshy
Date:
Tue Sep 27 22:24:35 2016 +0000
Revision:
1:b0034f8b2c42
Parent:
0:0d5d376157a0
Removed true assertion on success.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 0:0d5d376157a0 1 #include "Blinky.h"
sarahmarshy 0:0d5d376157a0 2 #include "mbed.h"
sarahmarshy 0:0d5d376157a0 3 #include "greentea-client/test_env.h"
sarahmarshy 0:0d5d376157a0 4 #include "unity.h"
sarahmarshy 0:0d5d376157a0 5 #include "utest.h"
sarahmarshy 0:0d5d376157a0 6 #include "rtos.h"
sarahmarshy 0:0d5d376157a0 7
sarahmarshy 0:0d5d376157a0 8 using namespace utest::v1;
sarahmarshy 0:0d5d376157a0 9
sarahmarshy 0:0d5d376157a0 10 //All tests will blink the LED 5 times
sarahmarshy 0:0d5d376157a0 11 const int expected_blinks = 5;
sarahmarshy 0:0d5d376157a0 12 //To be set by interrupt handler
sarahmarshy 0:0d5d376157a0 13 int measured_blinks = 0;
sarahmarshy 0:0d5d376157a0 14
sarahmarshy 0:0d5d376157a0 15 //To be called by interrupt, increases the number of measured rising edges
sarahmarshy 0:0d5d376157a0 16 void count_blinks(){
sarahmarshy 0:0d5d376157a0 17 TEST_ASSERT_MESSAGE(++measured_blinks<=expected_blinks, "******Too many blinks!******");
sarahmarshy 0:0d5d376157a0 18 }
sarahmarshy 0:0d5d376157a0 19
sarahmarshy 0:0d5d376157a0 20 //Generic blink test
sarahmarshy 0:0d5d376157a0 21 //interval => the the amount of time between blinks
sarahmarshy 0:0d5d376157a0 22 //led => PinName of LED to blink
sarahmarshy 0:0d5d376157a0 23 bool test_blinks(int interval, PinName led){
sarahmarshy 0:0d5d376157a0 24 //reset the measured blinks to 0 for each case
sarahmarshy 0:0d5d376157a0 25 measured_blinks = 0;
sarahmarshy 0:0d5d376157a0 26 //Set an appropriate wait time to measure exactly expected_blinks
sarahmarshy 0:0d5d376157a0 27 //with the given interval
sarahmarshy 0:0d5d376157a0 28 float wait_time = 2*interval*expected_blinks;
sarahmarshy 0:0d5d376157a0 29 //Create an interrupt that calls count_blinks on rising edge of LED pin
sarahmarshy 0:0d5d376157a0 30 InterruptIn blink_counter(led);
sarahmarshy 0:0d5d376157a0 31 //Create Blinky
sarahmarshy 0:0d5d376157a0 32 Blinky blinker(led, interval);
sarahmarshy 0:0d5d376157a0 33 //Attach interrupt
sarahmarshy 0:0d5d376157a0 34 blink_counter.rise(&count_blinks);
sarahmarshy 0:0d5d376157a0 35 //start blinky
sarahmarshy 0:0d5d376157a0 36 blinker.start();
sarahmarshy 0:0d5d376157a0 37 //Wait the appropriate amount of time for expected_blinks
sarahmarshy 0:0d5d376157a0 38 Thread::wait(wait_time);
sarahmarshy 0:0d5d376157a0 39 blinker.stop();
sarahmarshy 0:0d5d376157a0 40 }
sarahmarshy 0:0d5d376157a0 41
sarahmarshy 0:0d5d376157a0 42 void blink_500(){
sarahmarshy 0:0d5d376157a0 43 //Blinky with a 500 ms interval between blinks
sarahmarshy 0:0d5d376157a0 44 test_blinks(500, LED1);
sarahmarshy 0:0d5d376157a0 45 }
sarahmarshy 0:0d5d376157a0 46 void blink_1000(){
sarahmarshy 0:0d5d376157a0 47 //Blinky with a 1000 ms interval between blinks
sarahmarshy 0:0d5d376157a0 48 test_blinks(1000, LED1);
sarahmarshy 0:0d5d376157a0 49 }
sarahmarshy 0:0d5d376157a0 50
sarahmarshy 0:0d5d376157a0 51 utest::v1::status_t test_setup(const size_t number_of_cases) {
sarahmarshy 0:0d5d376157a0 52 // Setup Greentea using a reasonable timeout in seconds
sarahmarshy 0:0d5d376157a0 53 GREENTEA_SETUP(40, "default_auto");
sarahmarshy 0:0d5d376157a0 54 return verbose_test_setup_handler(number_of_cases);
sarahmarshy 0:0d5d376157a0 55 }
sarahmarshy 0:0d5d376157a0 56
sarahmarshy 0:0d5d376157a0 57 // Test cases
sarahmarshy 0:0d5d376157a0 58 Case cases[] = {
sarahmarshy 0:0d5d376157a0 59 Case("Testing 500 ms interval", blink_500),
sarahmarshy 0:0d5d376157a0 60 Case("Testing 1000 ms interval", blink_1000),
sarahmarshy 0:0d5d376157a0 61 };
sarahmarshy 0:0d5d376157a0 62
sarahmarshy 0:0d5d376157a0 63 Specification specification(test_setup, cases);
sarahmarshy 0:0d5d376157a0 64
sarahmarshy 0:0d5d376157a0 65 // Entry point into the tests
sarahmarshy 0:0d5d376157a0 66 int main() {
sarahmarshy 0:0d5d376157a0 67 return !Harness::run(specification);
sarahmarshy 0:0d5d376157a0 68 }