Add unit tests for the data structure libraries

This commit is contained in:
George Oikonomou 2017-12-12 17:16:05 +00:00
parent c3d2b095b6
commit bf44dbe62e
5 changed files with 1066 additions and 0 deletions

View File

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<simconf>
<project EXPORT="discard">[APPS_DIR]/mrm</project>
<project EXPORT="discard">[APPS_DIR]/mspsim</project>
<project EXPORT="discard">[APPS_DIR]/avrora</project>
<project EXPORT="discard">[APPS_DIR]/serial_socket</project>
<project EXPORT="discard">[APPS_DIR]/collect-view</project>
<project EXPORT="discard">[APPS_DIR]/powertracker</project>
<simulation>
<title>data-structures-sky</title>
<randomseed>123456</randomseed>
<motedelay_us>1000000</motedelay_us>
<radiomedium>
org.contikios.cooja.radiomediums.UDGM
<transmitting_range>50.0</transmitting_range>
<interference_range>100.0</interference_range>
<success_ratio_tx>1.0</success_ratio_tx>
<success_ratio_rx>1.0</success_ratio_rx>
</radiomedium>
<events>
<logoutput>40000</logoutput>
</events>
<motetype>
org.contikios.cooja.mspmote.SkyMoteType
<identifier>sky1</identifier>
<description>Sky Mote Type #sky1</description>
<source EXPORT="discard">[CONTIKI_DIR]/tests/07-simulation-base/code-data-structures/test-data-structures.c</source>
<commands EXPORT="discard">make test-data-structures.sky TARGET=sky</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/tests/07-simulation-base/code-data-structures/test-data-structures.sky</firmware>
<moteinterface>org.contikios.cooja.interfaces.Position</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.RimeAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.IPAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.Mote2MoteRelations</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.MoteAttributes</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspClock</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspMoteID</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyFlash</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
</motetype>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>3.086692968239446</x>
<y>5.726233183606267</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspClock
<deviation>1.0</deviation>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>1</id>
</interface_config>
<motetype_identifier>sky1</motetype_identifier>
</mote>
</simulation>
<plugin>
org.contikios.cooja.plugins.SimControl
<width>280</width>
<z>2</z>
<height>160</height>
<location_x>400</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.LogListener
<plugin_config>
<filter />
<formatted_time />
<coloring />
</plugin_config>
<width>586</width>
<z>1</z>
<height>666</height>
<location_x>400</location_x>
<location_y>160</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.ScriptRunner
<plugin_config>
<scriptfile>[CONFIG_DIR]/js/data-structures.js</scriptfile>
<active>true</active>
</plugin_config>
<width>600</width>
<z>0</z>
<height>700</height>
<location_x>5</location_x>
<location_y>1</location_y>
</plugin>
</simconf>

View File

@ -0,0 +1,9 @@
all: test-data-structures
MODULES += os/services/unit-test
MAKE_MAC = MAKE_MAC_NULLMAC
MAKE_NET = MAKE_NET_NULLNET
CONTIKI = ../../..
include $(CONTIKI)/Makefile.include

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2017, Yasuyuki Tanaka
* 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 HOLDERS 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.
*/
#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_
#define UNIT_TEST_PRINT_FUNCTION print_test_report
#endif /* PROJECT_CONF_H_ */

View File

@ -0,0 +1,897 @@
/*
* Copyright (c) 2017, George Oikonomou - http://www.spd.gr
* 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 HOLDERS 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 "lib/stack.h"
#include "lib/queue.h"
#include "lib/circular-list.h"
#include "lib/dbl-list.h"
#include "lib/dbl-circ-list.h"
#include "lib/random.h"
#include "services/unit-test/unit-test.h"
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
/*---------------------------------------------------------------------------*/
PROCESS(data_structure_test_process, "Data structure process");
AUTOSTART_PROCESSES(&data_structure_test_process);
/*---------------------------------------------------------------------------*/
typedef struct demo_struct_s {
struct demo_struct_s *next;
struct demo_struct_s *previous;
} demo_struct_t;
/*---------------------------------------------------------------------------*/
#define ELEMENT_COUNT 10
static demo_struct_t elements[ELEMENT_COUNT];
/*---------------------------------------------------------------------------*/
void
print_test_report(const unit_test_t *utp)
{
printf("=check-me= ");
if(utp->result == unit_test_failure) {
printf("FAILED - %s: exit at L%u\n", utp->descr, utp->exit_line);
} else {
printf("SUCCEEDED - %s\n", utp->descr);
}
}
/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(test_stack, "Stack Push/Pop");
UNIT_TEST(test_stack)
{
STACK(stack);
UNIT_TEST_BEGIN();
memset(elements, 0, sizeof(elements));
stack_init(stack);
/* Starts from empty */
UNIT_TEST_ASSERT(stack_is_empty(stack) == true);
UNIT_TEST_ASSERT(stack_peek(stack) == NULL);
UNIT_TEST_ASSERT(stack_pop(stack) == NULL);
/*
* Push two elements. Peek and pop should be the last one. Stack should be
* non-empty after the pop
*/
stack_push(stack, &elements[0]);
stack_push(stack, &elements[1]);
UNIT_TEST_ASSERT(stack_peek(stack) == &elements[1]);
UNIT_TEST_ASSERT(stack_pop(stack) == &elements[1]);
UNIT_TEST_ASSERT(stack_peek(stack) == &elements[0]);
UNIT_TEST_ASSERT(stack_is_empty(stack) == false);
UNIT_TEST_ASSERT(stack_pop(stack) == &elements[0]);
/* Ends empty */
UNIT_TEST_ASSERT(stack_is_empty(stack) == true);
UNIT_TEST_ASSERT(stack_peek(stack) == NULL);
UNIT_TEST_ASSERT(stack_pop(stack) == NULL);
UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(test_queue, "Queue Enqueue/Dequeue");
UNIT_TEST(test_queue)
{
QUEUE(queue);
UNIT_TEST_BEGIN();
memset(elements, 0, sizeof(elements));
queue_init(queue);
/* Starts from empty */
UNIT_TEST_ASSERT(queue_is_empty(queue) == true);
UNIT_TEST_ASSERT(queue_peek(queue) == NULL);
UNIT_TEST_ASSERT(queue_dequeue(queue) == NULL);
/* Enqueue three elements. They should come out in the same order */
queue_enqueue(queue, &elements[0]);
queue_enqueue(queue, &elements[1]);
queue_enqueue(queue, &elements[2]);
UNIT_TEST_ASSERT(queue_dequeue(queue) == &elements[0]);
UNIT_TEST_ASSERT(queue_dequeue(queue) == &elements[1]);
UNIT_TEST_ASSERT(queue_dequeue(queue) == &elements[2]);
/* Should be empty */
UNIT_TEST_ASSERT(queue_is_empty(queue) == true);
UNIT_TEST_ASSERT(queue_peek(queue) == NULL);
UNIT_TEST_ASSERT(queue_dequeue(queue) == NULL);
UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(test_csll, "Circular, singly-linked list");
UNIT_TEST(test_csll)
{
demo_struct_t *head, *tail;
CIRCULAR_LIST(csll);
UNIT_TEST_BEGIN();
memset(elements, 0, sizeof(elements));
circular_list_init(csll);
/* Starts from empty */
UNIT_TEST_ASSERT(circular_list_is_empty(csll) == true);
UNIT_TEST_ASSERT(circular_list_length(csll) == 0);
UNIT_TEST_ASSERT(circular_list_head(csll) == NULL);
UNIT_TEST_ASSERT(circular_list_tail(csll) == NULL);
/* Add one element. Should point to itself and act as head and tail */
circular_list_add(csll, &elements[0]);
UNIT_TEST_ASSERT(circular_list_is_empty(csll) == false);
UNIT_TEST_ASSERT(circular_list_length(csll) == 1);
UNIT_TEST_ASSERT(circular_list_head(csll) == &elements[0]);
UNIT_TEST_ASSERT(circular_list_tail(csll) == &elements[0]);
UNIT_TEST_ASSERT(elements[0].next == &elements[0]);
/* Add a second element. The two should point to each-other */
circular_list_add(csll, &elements[1]);
UNIT_TEST_ASSERT(elements[0].next == &elements[1]);
UNIT_TEST_ASSERT(elements[1].next == &elements[0]);
/*
* Add a third element and check that head->next->next points to tail.
* Check that tail->next points to the head
*/
circular_list_add(csll, &elements[2]);
head = circular_list_head(csll);
tail = circular_list_tail(csll);
UNIT_TEST_ASSERT(head->next->next == circular_list_tail(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_head(csll));
/* Re-add an existing element. Check the list's integrity */
circular_list_add(csll, &elements[1]);
head = circular_list_head(csll);
tail = circular_list_tail(csll);
UNIT_TEST_ASSERT(circular_list_is_empty(csll) == false);
UNIT_TEST_ASSERT(circular_list_length(csll) == 3);
UNIT_TEST_ASSERT(head->next->next->next == circular_list_head(csll));
UNIT_TEST_ASSERT(head->next->next == circular_list_tail(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_head(csll));
/* Add another two elements, then start testing removal */
circular_list_add(csll, &elements[3]);
circular_list_add(csll, &elements[4]);
/* Remove an item in the middle and test list integrity */
head = circular_list_head(csll);
circular_list_remove(csll, head->next->next);
head = circular_list_head(csll);
tail = circular_list_tail(csll);
UNIT_TEST_ASSERT(circular_list_length(csll) == 4);
UNIT_TEST_ASSERT(head->next->next->next->next == circular_list_head(csll));
UNIT_TEST_ASSERT(head->next->next->next == circular_list_tail(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_head(csll));
/* Remove the head and test list integrity */
circular_list_remove(csll, circular_list_head(csll));
head = circular_list_head(csll);
tail = circular_list_tail(csll);
UNIT_TEST_ASSERT(circular_list_length(csll) == 3);
UNIT_TEST_ASSERT(head->next->next->next == circular_list_head(csll));
UNIT_TEST_ASSERT(head->next->next == circular_list_tail(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_head(csll));
/* Remove the tail and test list integrity */
circular_list_remove(csll, circular_list_tail(csll));
head = circular_list_head(csll);
tail = circular_list_tail(csll);
UNIT_TEST_ASSERT(circular_list_length(csll) == 2);
UNIT_TEST_ASSERT(head->next->next == circular_list_head(csll));
UNIT_TEST_ASSERT(head->next == circular_list_tail(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_head(csll));
/*
* Remove the tail
* Only one item left: Make sure the head and tail are the same and point to
* each other
*/
circular_list_remove(csll, circular_list_tail(csll));
head = circular_list_head(csll);
tail = circular_list_tail(csll);
UNIT_TEST_ASSERT(circular_list_length(csll) == 1);
UNIT_TEST_ASSERT(head == tail);
UNIT_TEST_ASSERT(head->next->next == circular_list_head(csll));
UNIT_TEST_ASSERT(head->next == circular_list_head(csll));
UNIT_TEST_ASSERT(head->next == circular_list_tail(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_head(csll));
UNIT_TEST_ASSERT(tail->next == circular_list_tail(csll));
/* Remove the last element by removing the head */
circular_list_remove(csll, circular_list_head(csll));
UNIT_TEST_ASSERT(circular_list_is_empty(csll) == true);
UNIT_TEST_ASSERT(circular_list_length(csll) == 0);
UNIT_TEST_ASSERT(circular_list_head(csll) == NULL);
UNIT_TEST_ASSERT(circular_list_tail(csll) == NULL);
/* Remove the last element by removing the tail */
circular_list_add(csll, &elements[0]);
circular_list_remove(csll, circular_list_tail(csll));
UNIT_TEST_ASSERT(circular_list_is_empty(csll) == true);
UNIT_TEST_ASSERT(circular_list_length(csll) == 0);
UNIT_TEST_ASSERT(circular_list_head(csll) == NULL);
UNIT_TEST_ASSERT(circular_list_tail(csll) == NULL);
UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(test_dll, "Doubly-linked list");
UNIT_TEST(test_dll)
{
demo_struct_t *head, *tail;
CIRCULAR_LIST(dll);
UNIT_TEST_BEGIN();
memset(elements, 0, sizeof(elements));
/* Starts from empty */
dbl_list_init(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == true);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 0);
UNIT_TEST_ASSERT(dbl_list_head(dll) == NULL);
UNIT_TEST_ASSERT(dbl_list_tail(dll) == NULL);
/*
* Add an item by adding to the head.
* Head and tail should point to NULL in both directions
*/
dbl_list_add_head(dll, &elements[0]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 1);
UNIT_TEST_ASSERT(head == &elements[0]);
UNIT_TEST_ASSERT(tail == &elements[0]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == NULL);
UNIT_TEST_ASSERT(tail->previous == NULL);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add an item by adding to the tail.
* Head and tail should point to NULL in both directions
*/
dbl_list_remove(dll, dbl_list_head(dll));
dbl_list_add_tail(dll, &elements[1]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 1);
UNIT_TEST_ASSERT(head == &elements[1]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == NULL);
UNIT_TEST_ASSERT(tail->previous == NULL);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add a second item to head. Head points forward to tail.
* Tail points backwards to head.
*/
dbl_list_add_head(dll, &elements[2]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 2);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == tail);
UNIT_TEST_ASSERT(tail->previous == head);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add before head.
* NULL <-- 3 --> 2 --> 1 --> NULL
*/
dbl_list_add_before(dll, dbl_list_head(dll), &elements[3]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 3);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == &elements[2]);
UNIT_TEST_ASSERT(head->next->next == tail);
UNIT_TEST_ASSERT(head->next->next->next == NULL);
UNIT_TEST_ASSERT(tail->previous == &elements[2]);
UNIT_TEST_ASSERT(tail->previous->previous == &elements[3]);
UNIT_TEST_ASSERT(tail->previous->previous->previous == NULL);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add after head.
* NULL <-- 3 --> 4 --> 2 --> 1 --> NULL
*/
dbl_list_add_after(dll, dbl_list_head(dll), &elements[4]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 4);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == &elements[4]);
UNIT_TEST_ASSERT(head->next->next == &elements[2]);
UNIT_TEST_ASSERT(head->next->next->next == tail);
UNIT_TEST_ASSERT(head->next->next->next->next == NULL);
UNIT_TEST_ASSERT(tail->previous == &elements[2]);
UNIT_TEST_ASSERT(tail->previous->previous == &elements[4]);
UNIT_TEST_ASSERT(tail->previous->previous->previous == &elements[3]);
UNIT_TEST_ASSERT(tail->previous->previous->previous->previous == NULL);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add at 3rd position by adding after 2nd
* NULL <-- 3 --> 4 --> 5 --> 2 --> 1 --> NULL
*/
dbl_list_add_after(dll, &elements[4], &elements[5]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 5);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == &elements[4]);
UNIT_TEST_ASSERT(head->next->next == &elements[5]);
UNIT_TEST_ASSERT(tail->previous->previous == &elements[5]);
UNIT_TEST_ASSERT(tail->previous == &elements[2]);
UNIT_TEST_ASSERT(elements[5].next == &elements[2]);
UNIT_TEST_ASSERT(elements[5].previous == &elements[4]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add at 3rd position by adding before 3rd
* NULL <-- 3 --> 4 --> 6 --> 5 --> 2 --> 1 --> NULL
*/
dbl_list_add_before(dll, &elements[5], &elements[6]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 6);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == &elements[4]);
UNIT_TEST_ASSERT(head->next->next == &elements[6]);
UNIT_TEST_ASSERT(tail->previous->previous == &elements[5]);
UNIT_TEST_ASSERT(elements[6].next == &elements[5]);
UNIT_TEST_ASSERT(elements[6].previous == &elements[4]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add before tail
* NULL <-- 3 --> 4 --> 6 --> 5 --> 2 --> 7 --> 1 --> NULL
*/
dbl_list_add_before(dll, dbl_list_tail(dll), &elements[7]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 7);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(tail->previous == &elements[7]);
UNIT_TEST_ASSERT(elements[7].next == &elements[1]);
UNIT_TEST_ASSERT(elements[7].previous == &elements[2]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Add after tail
* NULL <-- 3 --> 4 --> 6 --> 5 --> 2 --> 7 --> 1 --> 8 --> NULL
*/
dbl_list_add_after(dll, dbl_list_tail(dll), &elements[8]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 8);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(tail->previous == &elements[1]);
UNIT_TEST_ASSERT(elements[8].next == NULL);
UNIT_TEST_ASSERT(elements[8].previous == &elements[1]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Find and remove element 5
* NULL <-- 3 --> 4 --> 6 --> 2 --> 7 --> 1 --> 8 --> NULL
*/
dbl_list_remove(dll, &elements[5]);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 7);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(elements[6].next == &elements[2]);
UNIT_TEST_ASSERT(elements[2].previous == &elements[6]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Remove before tail
* NULL <-- 3 --> 4 --> 6 --> 2 --> 7 --> 8 --> NULL
*/
dbl_list_remove(dll, ((demo_struct_t *)dbl_list_tail(dll))->previous);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 6);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(elements[7].next == tail);
UNIT_TEST_ASSERT(tail->previous == &elements[7]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Remove after head
* NULL <-- 3 --> 6 --> 2 --> 7 --> 8 --> NULL
*/
dbl_list_remove(dll, ((demo_struct_t *)dbl_list_head(dll))->next);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 5);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == &elements[6]);
UNIT_TEST_ASSERT(elements[6].previous == head);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Find element 2 and remove whatever is after it
* NULL <-- 3 --> 6 --> 2 --> 8 --> NULL
*/
dbl_list_remove(dll, elements[2].next);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 4);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(elements[2].next == tail);
UNIT_TEST_ASSERT(tail->previous == &elements[2]);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Find element 2 and remove whatever is before it
* NULL <-- 3 --> 2 --> 8 --> NULL
*/
dbl_list_remove(dll, elements[2].previous);
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 3);
UNIT_TEST_ASSERT(head == &elements[3]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == &elements[2]);
UNIT_TEST_ASSERT(elements[2].previous == head);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Remove head
* NULL <-- 2 --> 8 --> NULL
*/
dbl_list_remove(dll, dbl_list_head(dll));
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 2);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == tail);
UNIT_TEST_ASSERT(tail->previous == head);
UNIT_TEST_ASSERT(tail->next == NULL);
/*
* Remove tail
* NULL <-- 8 --> NULL
*/
dbl_list_remove(dll, dbl_list_head(dll));
head = dbl_list_head(dll);
tail = dbl_list_tail(dll);
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == false);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 1);
UNIT_TEST_ASSERT(head == &elements[8]);
UNIT_TEST_ASSERT(tail == &elements[8]);
UNIT_TEST_ASSERT(head->previous == NULL);
UNIT_TEST_ASSERT(head->next == NULL);
UNIT_TEST_ASSERT(tail->previous == NULL);
UNIT_TEST_ASSERT(tail->next == NULL);
/* Remove the last element */
dbl_list_remove(dll, dbl_list_head(dll));
UNIT_TEST_ASSERT(dbl_list_is_empty(dll) == true);
UNIT_TEST_ASSERT(dbl_list_length(dll) == 0);
UNIT_TEST_ASSERT(dbl_list_head(dll) == NULL);
UNIT_TEST_ASSERT(dbl_list_tail(dll) == NULL);
UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
UNIT_TEST_REGISTER(test_cdll, "Circular, doubly-linked list");
UNIT_TEST(test_cdll)
{
demo_struct_t *head, *tail;
CIRCULAR_LIST(cdll);
UNIT_TEST_BEGIN();
memset(elements, 0, sizeof(elements));
/* Starts from empty */
dbl_circ_list_init(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == true);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 0);
UNIT_TEST_ASSERT(dbl_circ_list_head(cdll) == NULL);
UNIT_TEST_ASSERT(dbl_circ_list_tail(cdll) == NULL);
/*
* Add an item by adding to the head.
* Head and tail should be the same element and should point to itself in
* both directions
*/
dbl_circ_list_add_head(cdll, &elements[0]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 1);
UNIT_TEST_ASSERT(head == &elements[0]);
UNIT_TEST_ASSERT(tail == &elements[0]);
UNIT_TEST_ASSERT(head->previous == head);
UNIT_TEST_ASSERT(head->next == head);
UNIT_TEST_ASSERT(tail->previous == tail);
UNIT_TEST_ASSERT(tail->next == tail);
/*
* Add an item by adding to the tail.
* (tail) <--> 0 <--> 1 <--> (head)
* Head should point to tail in both directions
*/
dbl_circ_list_add_tail(cdll, &elements[1]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 2);
UNIT_TEST_ASSERT(head == &elements[0]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->next == tail);
UNIT_TEST_ASSERT(tail->previous == head);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
/*
* Add before head.
* (tail) <--> 2 <--> 0 <--> 1 <--> (head)
*/
dbl_circ_list_add_before(cdll, dbl_circ_list_head(cdll), &elements[2]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 3);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[2].previous == tail);
UNIT_TEST_ASSERT(elements[2].next == &elements[0]);
UNIT_TEST_ASSERT(elements[0].previous == head);
/*
* Add after head.
* (tail) <--> 2 <--> 3 <--> 0 <--> 1 <--> (head)
*/
dbl_circ_list_add_after(cdll, dbl_circ_list_head(cdll), &elements[3]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 4);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[3].previous == head);
UNIT_TEST_ASSERT(elements[3].next == &elements[0]);
UNIT_TEST_ASSERT(elements[0].previous == &elements[3]);
/*
* Add at 3rd position by adding after 2nd
* (tail) <--> 2 <--> 3 <--> 4 <--> 0 <--> 1 <--> (head)
*/
dbl_circ_list_add_after(cdll, &elements[3], &elements[4]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 5);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[3].next == &elements[4]);
UNIT_TEST_ASSERT(elements[4].previous == &elements[3]);
UNIT_TEST_ASSERT(elements[4].next == &elements[0]);
UNIT_TEST_ASSERT(elements[0].previous == &elements[4]);
/*
* Add at 3rd position by adding before 3rd
* (tail) <--> 2 <--> 3 <--> 5 <--> 4 <--> 0 <--> 1 <--> (head)
*/
dbl_circ_list_add_before(cdll, &elements[4], &elements[5]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 6);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[3].next == &elements[5]);
UNIT_TEST_ASSERT(elements[5].previous == &elements[3]);
UNIT_TEST_ASSERT(elements[5].next == &elements[4]);
UNIT_TEST_ASSERT(elements[4].previous == &elements[5]);
/*
* Add before tail
* (tail) <--> 2 <--> 3 <--> 5 <--> 4 <--> 0 <--> 6 <--> 1 <--> (head)
*/
dbl_circ_list_add_before(cdll, dbl_circ_list_tail(cdll), &elements[6]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 7);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[1]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[0].next == &elements[6]);
UNIT_TEST_ASSERT(elements[6].previous == &elements[0]);
UNIT_TEST_ASSERT(elements[6].next == &elements[1]);
UNIT_TEST_ASSERT(elements[1].previous == &elements[6]);
/*
* Add after tail
* (tail) <--> 2 <--> 3 <--> 5 <--> 4 <--> 0 <--> 6 <--> 1 <--> 7 <--> (head)
*/
dbl_circ_list_add_after(cdll, dbl_circ_list_tail(cdll), &elements[7]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 8);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[1].next == &elements[7]);
UNIT_TEST_ASSERT(elements[7].previous == &elements[1]);
UNIT_TEST_ASSERT(elements[7].next == &elements[2]);
UNIT_TEST_ASSERT(elements[2].previous == &elements[7]);
/*
* Find and remove element 5
* (tail) <--> 2 <--> 3 <--> 4 <--> 0 <--> 6 <--> 1 <--> 7 <--> (head)
*/
dbl_circ_list_remove(cdll, &elements[5]);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 7);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[3].next == &elements[4]);
UNIT_TEST_ASSERT(elements[4].previous == &elements[3]);
/*
* Find element 4 and remove what's after it
* (tail) <--> 2 <--> 3 <--> 4 <--> 6 <--> 1 <--> 7 <--> (head)
*/
dbl_circ_list_remove(cdll, elements[4].next);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 6);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[4].next == &elements[6]);
UNIT_TEST_ASSERT(elements[6].previous == &elements[4]);
/*
* Find element 4 and remove what's before it
* (tail) <--> 2 <--> 4 <--> 6 <--> 1 <--> 7 <--> (head)
*/
dbl_circ_list_remove(cdll, elements[4].previous);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 5);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[2].next == &elements[4]);
UNIT_TEST_ASSERT(elements[4].previous == &elements[2]);
/*
* Remove before tail
* (tail) <--> 2 <--> 4 <--> 6 <--> 7 <--> (head)
*/
dbl_circ_list_remove(cdll,
((demo_struct_t *)dbl_circ_list_tail(cdll))->previous);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 4);
UNIT_TEST_ASSERT(head == &elements[2]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[6].next == &elements[7]);
UNIT_TEST_ASSERT(elements[7].previous == &elements[6]);
/*
* Remove after tail
* (tail) <--> 4 <--> 6 <--> 7 <--> (head)
*/
dbl_circ_list_remove(cdll,
((demo_struct_t *)dbl_circ_list_tail(cdll))->next);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 3);
UNIT_TEST_ASSERT(head == &elements[4]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(tail->next == head);
UNIT_TEST_ASSERT(elements[7].next == &elements[4]);
UNIT_TEST_ASSERT(elements[4].previous == &elements[7]);
/*
* Remove after head
* (tail) <--> 4 <--> 7 <--> (head)
*/
dbl_circ_list_remove(cdll,
((demo_struct_t *)dbl_circ_list_head(cdll))->next);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 2);
UNIT_TEST_ASSERT(head == &elements[4]);
UNIT_TEST_ASSERT(tail == &elements[7]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(head->next == tail);
UNIT_TEST_ASSERT(tail->previous == head);
UNIT_TEST_ASSERT(tail->next == head);
/*
* Remove before head
* (tail) <--> 4 <--> (head)
*/
dbl_circ_list_remove(cdll,
((demo_struct_t *)dbl_circ_list_head(cdll))->previous);
head = dbl_circ_list_head(cdll);
tail = dbl_circ_list_tail(cdll);
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == false);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 1);
UNIT_TEST_ASSERT(head == &elements[4]);
UNIT_TEST_ASSERT(tail == &elements[4]);
UNIT_TEST_ASSERT(head->previous == tail);
UNIT_TEST_ASSERT(head->next == tail);
/* Remove head */
dbl_circ_list_remove(cdll, dbl_circ_list_head(cdll));
dbl_circ_list_remove(cdll, dbl_circ_list_head(cdll));
UNIT_TEST_ASSERT(dbl_circ_list_is_empty(cdll) == true);
UNIT_TEST_ASSERT(dbl_circ_list_length(cdll) == 0);
UNIT_TEST_ASSERT(dbl_circ_list_head(cdll) == NULL);
UNIT_TEST_ASSERT(dbl_circ_list_tail(cdll) == NULL);
UNIT_TEST_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(data_structure_test_process, ev, data)
{
PROCESS_BEGIN();
printf("Run unit-test\n");
printf("---\n");
memset(elements, 0, sizeof(elements));
UNIT_TEST_RUN(test_stack);
UNIT_TEST_RUN(test_queue);
UNIT_TEST_RUN(test_csll);
UNIT_TEST_RUN(test_dll);
UNIT_TEST_RUN(test_cdll);
printf("=check-me= DONE\n");
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,25 @@
TIMEOUT(10000, log.testFailed());
var failed = false;
while(true) {
YIELD();
log.log(time + " " + "node-" + id + " "+ msg + "\n");
if(msg.contains("=check-me=") == false) {
continue;
}
if(msg.contains("FAILED")) {
failed = true;
}
if(msg.contains("DONE")) {
break;
}
}
if(failed) {
log.testFailed();
}
log.testOK();