Skip to content

Commit

Permalink
Unit tests: Created channel function unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarkko Paso committed Jan 10, 2018
1 parent 44a85e5 commit 00af7f1
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/nanostack/unittest/service_libs/channel_functions/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
include ../../makefile_defines.txt

COMPONENT_NAME = channel_functions_unit

#This must be changed manually
SRC_FILES = \
../../../../../source/Service_Libs/fhss/channel_functions.c \


TEST_SRC_FILES = \
main.cpp \
channelfunctest.cpp \
test_channel_functions.c \

include ../../MakefileWorker.mk

CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2016, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CppUTest/TestHarness.h"
#include "test_channel_functions.h"


TEST_GROUP(channel_functions)
{
void setup()
{
}

void teardown()
{
}
};

TEST(channel_functions, test_tr51_get_rand)
{
CHECK(test_tr51_get_rand());
}

TEST(channel_functions, test_tr51_calculate_channel_table)
{
CHECK(test_tr51_calculate_channel_table());
}

TEST(channel_functions, test_tr51_compute_cfd)
{
CHECK(test_tr51_compute_cfd());
}

TEST(channel_functions, test_tr51_calculate_hopping_sequence)
{
CHECK(test_tr51_calculate_hopping_sequence());
}
28 changes: 28 additions & 0 deletions test/nanostack/unittest/service_libs/channel_functions/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestPlugin.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTestExt/MockSupportPlugin.h"
int main(int ac, char** av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}

IMPORT_TEST_GROUP(channel_functions);

Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Copyright (c) 2016-2017, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "nsconfig.h"
#include "Service_Libs/fhss/channel_functions.h"
#include "test_channel_functions.h"

const int32_t test_channel_table[131] = { 57,78,25,22,97,90,86,102,
98,16,104,128,28,23,44,26,
1,74,117,6,53,89,105,94,
110,39,119,80,62,46,47,69,
68,21,49,58,5,92,17,54,
65,11,41,14,111,24,83,40,
76,106,19,118,123,51,38,126,
107,12,60,13,87,125,116,100,
88,103,72,96,36,99,114,112,
3,48,32,35,73,7,91,45,
64,20,55,77,27,75,0,59,
109,81,30,122,84,67,121,101,
127,37,85,18,124,66,31,79,
115,9,34,70,10,95,42,33,
93,43,50,108,63,2,15,61,
56,71,4,29,52,82,120,113,
8,-1,-1 };

const int32_t test_HopSequenceTable [129] = { 4,93,31,84,55,3,116,123,
41,68,105,28,25,71,33,66,
122,20,112,125,118,11,69,89,
128,78,56,42,124,30,64,114,
87,19,65,47,53,104,57,61,
95,18,81,45,99,13,106,54,
46,6,16,8,15,10,85,109,
91,36,60,76,17,62,117,98,
113,2,70,37,59,7,96,12,
40,92,80,74,102,120,63,34,
127,0,73,72,107,83,5,119,
1,86,82,108,9,101,75,35,
103,126,24,58,39,26,90,52,
50,115,121,27,32,88,38,111,
49,110,44,97,29,43,79,67,
77,48,100,51,14,21,94,23,
22 };

bool test_tr51_get_rand()
{
tr51_seed_rand(1);
// Test first few random values
if (1103527590 != tr51_get_rand()) {
return false;
}
if (377401575 != tr51_get_rand()) {
return false;
}
if (662824084 != tr51_get_rand()) {
return false;
}
if (1147902781 != tr51_get_rand()) {
return false;
}
if (2035015474 != tr51_get_rand()) {
return false;
}
return true;
}

bool test_tr51_calculate_channel_table()
{
int32_t chantable[131];
// Test generating channel table
tr51_calculate_channel_table(129, 131, chantable);
if (memcmp(chantable, test_channel_table, 131*4)) {
return false;
}

// Test that every channel exists once and only once.
int k=0;
for (int i=0; i<129; i++) {
for (int j=0; j<131; j++) {
if (chantable[j] == i) {
k++;
}
}
if (k != 1) {
return false;
}
k=0;
}
return true;
}

bool test_tr51_compute_cfd()
{
uint8_t first_element;
uint8_t step_size;
uint8_t mac[8] = {0x00, 0x13, 0x50, 0x04, 0x00, 0x00, 0x05, 0xf8};
// Test generating first element and step size
tr51_compute_cfd(mac, &first_element, &step_size , 131);
if (122 != first_element) {
return false;
}
if (119 != step_size) {
return false;
}
return true;
}

bool test_tr51_calculate_hopping_sequence()
{
int32_t chantable[131];
int32_t outtable[129];
tr51_calculate_channel_table(129, 131, chantable);
// Test generating hopping sequence table of specific peer
tr51_calculate_hopping_sequence(chantable, 129, 122, 119, outtable, NULL, 0);
if (memcmp(outtable, test_HopSequenceTable, 129*4)) {
return false;
}
// Test that every channel exists once and only once.
int k=0;
for (int i=0; i<129; i++) {
for (int j=0; j<129; j++) {
if (outtable[j] == i) {
k++;
}
}
if (k != 1) {
return false;
}
k=0;
}

return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2016, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TEST_CHANNEL_FUNC_H
#define TEST_CHANNEL_FUNC_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>

bool test_tr51_get_rand();
bool test_tr51_calculate_channel_table();
bool test_tr51_compute_cfd();
bool test_tr51_calculate_hopping_sequence();

#ifdef __cplusplus
}
#endif

#endif // TEST_CHANNEL_FUNC_H

0 comments on commit 00af7f1

Please sign in to comment.