From 8c2b4f8cf9c659143366f8f6238bc8a43dca94f0 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Mon, 4 May 2020 00:29:42 +0200 Subject: [PATCH] Add test cases for function object --- tests/test-mfuncobj.c | 120 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/test-mfuncobj.c diff --git a/tests/test-mfuncobj.c b/tests/test-mfuncobj.c new file mode 100644 index 00000000..da872c8a --- /dev/null +++ b/tests/test-mfuncobj.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2017-2020, Patrick Pelissier + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + 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. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS AND 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 +#include "test-obj.h" +#include "m-string.h" +#include "m-funcobj.h" +#include "coverage.h" + +FUNC_OBJ_ITF_DEF(interface1, int, int, const int *) + +FUNC_OBJ_INS_DEF(instance11, + interface1, + (a, b), { + assert(string_equal_str_p(self->c, "INIT")); + string_set_str(self->c, "CALLED"); + return self->sort_field*(a - *b); + }, + (sort_field, int), + (c, string_t, STRING_OPLIST) ) + +FUNC_OBJ_INS_DEF(instance12, + interface1, + (a, b), { + return a - *b; + }) + +FUNC_OBJ_ITF_DEF(interface2, int) + +FUNC_OBJ_INS_DEF(instance21, + interface2, + (), { + return 1; + }) + +FUNC_OBJ_INS_DEF(instance22, + interface2, + (), { + self->a ++; + return self->a; + }, (a, int) ) + +#define M_OPL_instance22_t() FUNC_OBJ_INS_OPLIST(instance22, int) + + +static void test_instance11(void) +{ + instance11_t cmp; + int n = 2; + instance11_init_with(cmp, -1, STRING_CTE("INIT") ); + int x = interface1_call(instance11_as_interface(cmp), 10, &n); + assert(x == -8); + assert(string_equal_str_p(cmp->c, "CALLED")); + instance11_clear(cmp); + + instance11_init_with(cmp, 1, STRING_CTE("INIT") ); + x = interface1_call(instance11_as_interface(cmp), 100, &n); + assert(x == 98); + assert(string_equal_str_p(cmp->c, "CALLED")); + instance11_clear(cmp); +} + +static void test_instance12(void) +{ + instance12_t cmp; + int n = 0; + instance12_init(cmp); + int x = interface1_call(instance12_as_interface(cmp), 10, &n); + assert(x == 10); + instance12_clear(cmp); +} + +static void test_instance21(void) +{ + instance21_t cmp; + instance21_init(cmp); + int x = interface2_call(instance21_as_interface(cmp)); + assert(x == 1); + instance21_clear(cmp); +} + +static void test_instance22(void) +{ + M_LET( (cmp, 1), instance22_t) { + int x = interface2_call(instance22_as_interface(cmp)); + assert(x == 2); + x = interface2_call(instance22_as_interface(cmp)); + assert(x == 3); + x = interface2_call(instance22_as_interface(cmp)); + assert(x == 4); + } +} + +int main(void) +{ + test_instance11(); + test_instance12(); + test_instance21(); + test_instance22(); + return 0; +}