Skip to content

Commit

Permalink
Merge pull request #188 from xmidt-org/improve-tests
Browse files Browse the repository at this point in the history
Improve the tests by using actual times instead of the mock times.
  • Loading branch information
schmidtw authored Jul 17, 2023
2 parents ebbf31b + 0dc61c0 commit d77b02e
Showing 1 changed file with 41 additions and 26 deletions.
67 changes: 41 additions & 26 deletions tests/test_schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,20 @@ void test_decoder( void )

time_t convert_unix_time_to_weekly( time_t unixtime )
{
//printf( "unix: %ld -> rel: %ld\n", unixtime, (unixtime - 1234000 + 11) );
return unixtime - 1234000 + 11;
time_t seconds_since_sunday_midnght;
time_t t = unixtime;
struct tm ts;

ts = *localtime(&t);

seconds_since_sunday_midnght = (ts.tm_wday * 24 * 3600) +
(ts.tm_hour * 3600) +
(ts.tm_min * 60) +
ts.tm_sec;


//printf( "unix: %ld -> rel: %ld\n", unixtime, seconds_since_sunday_midnght);
return seconds_since_sunday_midnght;
}


Expand Down Expand Up @@ -163,6 +175,9 @@ void run_schedule_test( schedule_test_t *t )
uint8_t i, j;
time_t next_unixtime;

/* Ensure the TZ is consistent for the tests. */
set_unix_time_zone("UTC");

CU_ASSERT( NULL != t );

s = create_schedule();
Expand Down Expand Up @@ -230,8 +245,8 @@ void test_simple_case( void )
char *mac_id[] = { "11:22:33:44:55:66", "22:33:44:55:66:aa", "33:44:55:66:aa:BB", };

input_t absolute[] = {
{ .time = 1234000, .block_count = 2, .block = { 2, 1 }, },
{ .time = 1234010, .block_count = 1, .block = { 2 }, },
{ .time = 864010, .block_count = 2, .block = { 2, 1 }, },
{ .time = 864015, .block_count = 1, .block = { 2 }, },
};

input_t weekly[] = {
Expand All @@ -240,12 +255,16 @@ void test_simple_case( void )
};

block_test_t b_test[] = {
{ .unixtime = 1233999, .macs = "22:33:44:55:66:aa", .next_unixtime = 1234000, },
{ .unixtime = 1234000, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 1234010, },
{ .unixtime = 1234001, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 1234010, },
{ .unixtime = 1234010, .macs = "33:44:55:66:aa:BB", .next_unixtime = 1234012, },
{ .unixtime = 1234011, .macs = "33:44:55:66:aa:BB", .next_unixtime = 1234012, },
{ .unixtime = 1234012, .macs = "11:22:33:44:55:66", .next_unixtime = 1234013, },
{ .unixtime = 864000, .macs = "22:33:44:55:66:aa", .next_unixtime = 864010, },
{ .unixtime = 864009, .macs = "22:33:44:55:66:aa", .next_unixtime = 864010, },
{ .unixtime = 864010, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 864015, },
{ .unixtime = 864011, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 864015, },
{ .unixtime = 864014, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 864015, },
{ .unixtime = 864015, .macs = "33:44:55:66:aa:BB", .next_unixtime = 864023, },
{ .unixtime = 864016, .macs = "33:44:55:66:aa:BB", .next_unixtime = 864023, },
{ .unixtime = 864023, .macs = "11:22:33:44:55:66", .next_unixtime = 864024, },
{ .unixtime = 864024, .macs = "22:33:44:55:66:aa", .next_unixtime = 1468823, },
{ .unixtime = 864025, .macs = "22:33:44:55:66:aa", .next_unixtime = 1468823, },
};

schedule_test_t test = { .macs = mac_id, .macs_size = sizeof(mac_id),
Expand All @@ -263,7 +282,7 @@ void test_another_usecase( void )

input_t absolute[] = {
{ .time = 0, .block_count = 2, .block = { 2, 1 }, },
{ .time = 1234010, .block_count = 1, .block = { 2 }, },
{ .time = 864015, .block_count = 1, .block = { 2 }, },
};

input_t weekly[] = {
Expand All @@ -272,12 +291,12 @@ void test_another_usecase( void )
};

block_test_t b_test[] = {
{ .unixtime = 1233999, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 1234010, },
{ .unixtime = 1234000, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 1234010, },
{ .unixtime = 1234001, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 1234010, },
{ .unixtime = 1234010, .macs = "33:44:55:66:aa:BB", .next_unixtime = 1234012, },
{ .unixtime = 1234011, .macs = "33:44:55:66:aa:BB", .next_unixtime = 1234012, },
{ .unixtime = 1234012, .macs = "11:22:33:44:55:66", .next_unixtime = 1234013, },
{ .unixtime = 864, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 864015, },
{ .unixtime = 864014, .macs = "33:44:55:66:aa:BB 22:33:44:55:66:aa", .next_unixtime = 864015, },
{ .unixtime = 864015, .macs = "33:44:55:66:aa:BB", .next_unixtime = 864023, },
{ .unixtime = 864022, .macs = "33:44:55:66:aa:BB", .next_unixtime = 864023, },
{ .unixtime = 864023, .macs = "11:22:33:44:55:66", .next_unixtime = 864024, },
{ .unixtime = 864024, .macs = "22:33:44:55:66:aa", .next_unixtime = 1468823, },
};

schedule_test_t test = { .macs = mac_id, .macs_size = sizeof(mac_id),
Expand Down Expand Up @@ -359,22 +378,18 @@ void test_only_one_absolute( void )

void test_only_one_weekly( void )
{
#define WEEKLY_23_NEXT_WEEK 1234012 + SECONDS_IN_A_WEEK

char *mac_id[] = { "11:22:33:44:55:66", "22:33:44:55:66:aa", "33:44:55:66:aa:BB", "44:55:66:aa:BB:cc", "55:66:aa:BB:cc:DD", };

input_t weekly[] = {
{ .time = 23, .block_count = 1, .block = { 0 }, },
};

block_test_t b_test[] = {
{ .unixtime = 1233999, .macs = "11:22:33:44:55:66", .next_unixtime = 1234012, },
{ .unixtime = 1234000, .macs = "11:22:33:44:55:66", .next_unixtime = 1234012, },
{ .unixtime = 1234001, .macs = "11:22:33:44:55:66", .next_unixtime = 1234012, },
{ .unixtime = 1234010, .macs = "11:22:33:44:55:66", .next_unixtime = 1234012, },
{ .unixtime = 1234011, .macs = "11:22:33:44:55:66", .next_unixtime = 1234012, },
{ .unixtime = 1234012, .macs = "11:22:33:44:55:66", .next_unixtime = WEEKLY_23_NEXT_WEEK, },
{ .unixtime = 1234013, .macs = "11:22:33:44:55:66", .next_unixtime = WEEKLY_23_NEXT_WEEK, },
{ .unixtime = 864000, .macs = "11:22:33:44:55:66", .next_unixtime = 864023, },
{ .unixtime = 864001, .macs = "11:22:33:44:55:66", .next_unixtime = 864023, },
{ .unixtime = 864022, .macs = "11:22:33:44:55:66", .next_unixtime = 864023, },
{ .unixtime = 864023, .macs = "11:22:33:44:55:66", .next_unixtime = 1468823, },
{ .unixtime = 864024, .macs = "11:22:33:44:55:66", .next_unixtime = 1468823, },
};

schedule_test_t test = { .macs = mac_id, .macs_size = sizeof(mac_id),
Expand Down

0 comments on commit d77b02e

Please sign in to comment.