From 555eeeee15abf095b81a0604e398bc496c89d8b7 Mon Sep 17 00:00:00 2001 From: rafagafe Date: Sat, 1 Sep 2018 01:25:10 +0200 Subject: [PATCH] Fix license date and refactor test. --- example-01.c | 2 +- example-02.c | 2 +- tests.c | 73 +++++++++++++++++++++++++++++++--------------------- tiny-json.c | 2 +- tiny-json.h | 2 +- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/example-01.c b/example-01.c index 2be3de9..50babe3 100644 --- a/example-01.c +++ b/example-01.c @@ -5,7 +5,7 @@ Licensed under the MIT License . SPDX-License-Identifier: MIT - Copyright (c) 2013-2018 Rafa Garcia . + Copyright (c) 2016-2018 Rafa Garcia . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/example-02.c b/example-02.c index 144c9e7..229c840 100644 --- a/example-02.c +++ b/example-02.c @@ -5,7 +5,7 @@ Licensed under the MIT License . SPDX-License-Identifier: MIT - Copyright (c) 2013-2018 Rafa Garcia . + Copyright (c) 2016-2018 Rafa Garcia . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/tests.c b/tests.c index 2f439f8..f826cf3 100644 --- a/tests.c +++ b/tests.c @@ -5,7 +5,7 @@ Licensed under the MIT License . SPDX-License-Identifier: MIT - Copyright (c) 2013-2018 Rafa Garcia . + Copyright (c) 2016-2018 Rafa Garcia . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -33,11 +33,42 @@ #include #include "tiny-json.h" + + +// ----------------------------------------------------- Test "framework": --- + #define done() return 0 #define fail() return __LINE__ -static unsigned int checkqty = 0; +static int checkqty = 0; #define check( x ) do { ++checkqty; if (!(x)) fail(); } while ( 0 ) +struct test { + int(*func)(void); + char const* name; +}; + +static int test_suit( struct test const* tests, int numtests ) { + printf( "%s", "\n\nTests:\n" ); + int failed = 0; + for( int i = 0; i < numtests; ++i ) { + printf( " %02d%s%-25s ", i, ": ", tests[i].name ); + int linerr = tests[i].func(); + if ( 0 == linerr ) + printf( "%s", "OK\n" ); + else { + printf( "%s%d\n", "Failed, line: ", linerr ); + ++failed; + } + } + printf( "\n%s%d\n", "Total checks: ", checkqty ); + printf( "%s[ %d / %d ]\r\n\n\n", "Tests PASS: ", numtests - failed, numtests ); + return failed; +} + + + +// ----------------------------------------------------------- Unit tests: --- + static int empty( void ) { json_t pool[6]; unsigned const qty = sizeof pool / sizeof *pool; @@ -332,35 +363,17 @@ int badformat( void ) { done(); } -struct test { - int(*func)(void); - char const* name; -}; -static int test_exec( struct test const* test ) { - int const err = test->func(); - if ( err ) { - fprintf( stderr, "%s%s%s%d%s", "Failed test: '", test->name, "' Line: ", err, ".\n" ); - return 1; - } - return 0; -} - -static struct test const tests[] = { - { empty, "Empty object and array" }, - { primitive, "Primitive properties" }, - { text, "Text" }, - { array, "Array" }, - { badformat, "Bad format" }, - { goodformats, "Formats" }, -}; +// --------------------------------------------------------- Execute tests: --- int main( void ) { - int failed = 0; - unsigned int const qty = sizeof tests / sizeof *tests; - for( unsigned int i = 0; i < qty; ++i ) - failed += test_exec( tests + i ); - unsigned int const percent = 100.0 * ( qty - failed ) / qty; - printf( "%d%s%d%s", percent, "%. ", checkqty, " checks.\n" ); - return failed; + static struct test const tests[] = { + { empty, "Empty object and array" }, + { primitive, "Primitive properties" }, + { text, "Text" }, + { array, "Array" }, + { badformat, "Bad format" }, + { goodformats, "Formats" }, + }; + return test_suit( tests, sizeof tests / sizeof *tests ); } diff --git a/tiny-json.c b/tiny-json.c index ebad757..5f6be67 100644 --- a/tiny-json.c +++ b/tiny-json.c @@ -5,7 +5,7 @@ Licensed under the MIT License . SPDX-License-Identifier: MIT - Copyright (c) 2013-2018 Rafa Garcia . + Copyright (c) 2016-2018 Rafa Garcia . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/tiny-json.h b/tiny-json.h index 844b266..fd8b238 100644 --- a/tiny-json.h +++ b/tiny-json.h @@ -5,7 +5,7 @@ Licensed under the MIT License . SPDX-License-Identifier: MIT - Copyright (c) 2013-2018 Rafa Garcia . + Copyright (c) 2016-2018 Rafa Garcia . Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal