Skip to content

Commit

Permalink
etimer: Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrentz committed Dec 7, 2023
1 parent 1ffc650 commit e0b9643
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 1 deletion.
14 changes: 14 additions & 0 deletions os/sys/etimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,18 @@ etimer_stop(struct etimer *et)
et->p = PROCESS_NONE;
}
/*---------------------------------------------------------------------------*/
bool
etimer_check_ordering(void)
{
struct etimer *current = next_etimer;
while(current && current->next) {
if(CLOCK_LT(etimer_expiration_time(current->next),
etimer_expiration_time(current))) {
return false;
}
current = current->next;
}
return true;
}
/*---------------------------------------------------------------------------*/
/** @} */
6 changes: 6 additions & 0 deletions os/sys/etimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#define ETIMER_H_

#include "contiki.h"
#include <stdbool.h>

#include <stdbool.h>
#include <stddef.h>
Expand Down Expand Up @@ -253,6 +254,11 @@ int etimer_pending(void);
*/
clock_time_t etimer_next_expiration_time(void);

/**
* \brief Check if all etimers are correctly ordered by expiration time.
* \return True if the ordering is correct and false otherwise.
*/
bool etimer_check_ordering(void);

/** @} */

Expand Down
3 changes: 3 additions & 0 deletions tests/08-native-runs/16-etimer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh -e

./run-one.sh 16-etimer
9 changes: 9 additions & 0 deletions tests/08-native-runs/16-etimer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONTIKI_PROJECT = test-etimer
all: $(CONTIKI_PROJECT)

TARGET = native

MODULES += os/services/unit-test

CONTIKI = ../../..
include $(CONTIKI)/Makefile.include
99 changes: 99 additions & 0 deletions tests/08-native-runs/16-etimer/test-etimer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2021, Uppsala universitet.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "contiki.h"
#include "unit-test.h"
#include <stdio.h>

PROCESS(test_process, "test");
AUTOSTART_PROCESSES(&test_process);

/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(expiration, "expiration");
UNIT_TEST(expiration)
{
struct etimer et;

UNIT_TEST_BEGIN();

etimer_set(&et, CLOCK_SECOND);
UNIT_TEST_ASSERT(etimer_pending());
UNIT_TEST_ASSERT(
etimer_start_time(&et) + CLOCK_SECOND == etimer_expiration_time(&et));
etimer_stop(&et);
UNIT_TEST_ASSERT(etimer_expired(&et));

UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(ordering, "ordering");
UNIT_TEST(ordering)
{
struct etimer et[3];

UNIT_TEST_BEGIN();

UNIT_TEST_ASSERT(etimer_check_ordering());
etimer_set(&et[0], CLOCK_SECOND);
etimer_set(&et[1], 3 * CLOCK_SECOND);
etimer_set(&et[2], 2 * CLOCK_SECOND);
UNIT_TEST_ASSERT(etimer_check_ordering());
etimer_stop(&et[0]);
etimer_stop(&et[2]);
UNIT_TEST_ASSERT(etimer_check_ordering());
etimer_reset(&et[0]);
etimer_restart(&et[2]);
UNIT_TEST_ASSERT(etimer_check_ordering());

UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_process, ev, data)
{
PROCESS_BEGIN();

printf("Run unit-test\n");
printf("---\n");

UNIT_TEST_RUN(expiration);
UNIT_TEST_RUN(ordering);

if(!UNIT_TEST_PASSED(expiration)
|| !UNIT_TEST_PASSED(ordering)) {
printf("=check-me= FAILED\n");
printf("---\n");
}

printf("=check-me= DONE\n");
printf("---\n");

PROCESS_END();
}
/*---------------------------------------------------------------------------*/
3 changes: 2 additions & 1 deletion tests/08-native-runs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ tests/08-native-runs/11-aes-ccm/native:./11-aes-ccm.sh \
tests/08-native-runs/12-heapmem/native:./12-heapmem.sh \
tests/08-native-runs/13-coffee/native:./13-coffee.sh \
tests/08-native-runs/14-sha-256/native:./14-sha-256.sh \
tests/08-native-runs/15-ieee802154-security/native:./15-ieee802154-security.sh
tests/08-native-runs/15-ieee802154-security/native:./15-ieee802154-security.sh \
tests/08-native-runs/16-etimer/native:./16-etimer.sh


include ../Makefile.compile-test

0 comments on commit e0b9643

Please sign in to comment.