diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 679576a..9dcc0f1 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -20,7 +20,8 @@ set(HEADER_FILES include/chiaki/audioreceiver.h include/chiaki/video.h include/chiaki/videoreceiver.h - include/chiaki/frameprocessor.h) + include/chiaki/frameprocessor.h + include/chiaki/seqnum.h) set(SOURCE_FILES src/common.c diff --git a/lib/include/chiaki/seqnum.h b/lib/include/chiaki/seqnum.h new file mode 100644 index 0000000..16f2688 --- /dev/null +++ b/lib/include/chiaki/seqnum.h @@ -0,0 +1,56 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_SEQNUM_H +#define CHIAKI_SEQNUM_H + +#include "common.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef uint16_t ChiakiSeqNum16; + +#define CHIAKI_SEQ_NUM_16_MAX 0xffff + +static inline bool chiaki_seq_num_16_lt(ChiakiSeqNum16 a, ChiakiSeqNum16 b) +{ + if(a == b) + return false; + int32_t d = (int32_t)b - (int32_t)a; + return (a < b && d < (1 << 15)) + || ((a > b) && -d > (1 << 15)); +} + +static inline bool chiaki_seq_num_16_gt(ChiakiSeqNum16 a, ChiakiSeqNum16 b) +{ + if(a == b) + return false; + int32_t d = (int32_t)b - (int32_t)a; + return (a < b && d > (1 << 15)) + || ((a > b) && -d < (1 << 15)); +} + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_SEQNUM_H diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4cd8ca9..c843621 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,7 +7,8 @@ add_executable(chiaki-unit http.c rpcrypt.c gkcrypt.c - takion.c) + takion.c + seqnum.c) target_link_libraries(chiaki-unit chiaki-lib munit) diff --git a/test/main.c b/test/main.c index b453138..94e23ec 100644 --- a/test/main.c +++ b/test/main.c @@ -17,12 +17,20 @@ #include +extern MunitTest tests_seq_num[]; extern MunitTest tests_http[]; extern MunitTest tests_rpcrypt[]; extern MunitTest tests_gkcrypt[]; extern MunitTest tests_takion[]; static MunitSuite suites[] = { + { + "/seq_num", + tests_seq_num, + NULL, + 1, + MUNIT_SUITE_OPTION_NONE + }, { "/http", tests_http, diff --git a/test/seqnum.c b/test/seqnum.c new file mode 100644 index 0000000..d4a77d1 --- /dev/null +++ b/test/seqnum.c @@ -0,0 +1,64 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +#include + + +static MunitResult test_seq_num_16(const MunitParameter params[], void *user) +{ + ChiakiSeqNum16 a = 0; + do + { + ChiakiSeqNum16 b = a + 1; + munit_assert(chiaki_seq_num_16_gt(b, a)); + munit_assert(!chiaki_seq_num_16_gt(a, b)); + munit_assert(chiaki_seq_num_16_lt(a, b)); + munit_assert(!chiaki_seq_num_16_lt(b, a)); + a = b; + } while(a); + + a = 0; + do + { + ChiakiSeqNum16 b = a + 0xfff; + munit_assert(chiaki_seq_num_16_gt(b, a)); + munit_assert(!chiaki_seq_num_16_gt(a, b)); + munit_assert(chiaki_seq_num_16_lt(a, b)); + munit_assert(!chiaki_seq_num_16_lt(b, a)); + a++; + } while(a); + + munit_assert(chiaki_seq_num_16_gt(1, 0xfff5)); + munit_assert(!chiaki_seq_num_16_gt(0xfff5, 1)); + + return MUNIT_OK; +} + + +MunitTest tests_seq_num[] = { + { + "/seq_num_16", + test_seq_num_16, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, + { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } +}; \ No newline at end of file