From 43116b68c2a6ce33482188e77a16abea89409c3a Mon Sep 17 00:00:00 2001 From: Donovan Baarda Date: Tue, 3 Dec 2019 18:57:51 +1100 Subject: Tidy netint.[ch] and add tests. Add tests/netint_test.c and CMakeLists.txt changes to test rs_int_len(). In netint.[ch] replace runtime checks for invalid arguments with assert() statements and tidy the argument names, variable names, and ordering. Replace `unsigned char` arguments with `rs_byte_t`. Add an assert to rs_int_len() to require the argument is positive. --- tests/netint_test.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/netint_test.c (limited to 'tests') diff --git a/tests/netint_test.c b/tests/netint_test.c new file mode 100644 index 0000000..e25b582 --- /dev/null +++ b/tests/netint_test.c @@ -0,0 +1,49 @@ +/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- + * librsync -- dynamic caching and delta update in HTTP + * + * Copyright (C) 2019 by Donovan Baarda + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This program 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* Force DEBUG on so that tests can use assert(). */ +#undef NDEBUG +#include +#include +#include +#include "librsync.h" +#include "netint.h" + +/* Test driver for netint. */ +int main(int argc, char **argv) +{ + assert(rs_int_len((rs_long_t)0) == 1); + assert(rs_int_len((rs_long_t)1) == 1); + assert(rs_int_len((rs_long_t)INT8_MAX) == 1); + assert(rs_int_len((rs_long_t)1 << 7) == 1); + assert(rs_int_len((rs_long_t)1 << 8) == 2); + assert(rs_int_len((rs_long_t)INT16_MAX) == 2); +#ifdef INT32_MAX + assert(rs_int_len((rs_long_t)1 << 15) == 2); + assert(rs_int_len((rs_long_t)1 << 16) == 4); + assert(rs_int_len((rs_long_t)INT32_MAX) == 4); +#endif +#ifdef INT64_MAX + assert(rs_int_len((rs_long_t)1 << 31) == 4); + assert(rs_int_len((rs_long_t)1 << 32) == 8); + assert(rs_int_len((rs_long_t)INT64_MAX) == 8); +#endif + return 0; +} -- cgit v1.2.1