|
| 1 | +#define CATCH_CONFIG_MAIN |
| 2 | + |
| 3 | +#include <stddef.h> |
| 4 | + |
| 5 | +#include "catch_amalgamated.hpp" |
| 6 | +#include "assert.h" |
| 7 | +#include "mpy_aliases.h" |
| 8 | +#include "mpy_obj.h" |
| 9 | +#include "builtins-setup.h" |
| 10 | +#include "function-args.h" |
| 11 | +#include "literals/tuple.h" |
| 12 | +#include "literals/int.h" |
| 13 | +#include "literals/boolean.h" |
| 14 | +#include "literals/str.h" |
| 15 | +#include "type-hierarchy/object.h" |
| 16 | +#include "type-hierarchy/type.h" |
| 17 | +#include "test/test_helpers.h" |
| 18 | + |
| 19 | +int add_op(int i, int j){ |
| 20 | + __MPyObj *a; |
| 21 | + |
| 22 | + __mpy_builtins_setup(); |
| 23 | + a = __mpy_obj_init_object(); |
| 24 | + __mpy_obj_ref_inc(a); |
| 25 | + |
| 26 | + __mpy_obj_ref_dec(a); |
| 27 | + a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__add__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL); |
| 28 | + __mpy_obj_ref_inc(a); |
| 29 | + |
| 30 | + __mpy_obj_ref_dec(a); |
| 31 | + |
| 32 | + printf("[ADD] i : %d --- j : %d\n",i,j); |
| 33 | + print_mpyobj_int(a); |
| 34 | + |
| 35 | + __mpy_builtins_cleanup(); |
| 36 | + |
| 37 | + return (*(int*)(a->content)) == (i+j) ? 1 : 0; |
| 38 | +} |
| 39 | + |
| 40 | +int sub_op(int i, int j){ |
| 41 | + __MPyObj *a; |
| 42 | + |
| 43 | + __mpy_builtins_setup(); |
| 44 | + a = __mpy_obj_init_object(); |
| 45 | + __mpy_obj_ref_inc(a); |
| 46 | + |
| 47 | + __mpy_obj_ref_dec(a); |
| 48 | + a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__sub__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL); |
| 49 | + __mpy_obj_ref_inc(a); |
| 50 | + |
| 51 | + __mpy_obj_ref_dec(a); |
| 52 | + |
| 53 | + printf("[SUB] i : %d --- j : %d\n",i,j); |
| 54 | + print_mpyobj_int(a); |
| 55 | + |
| 56 | + __mpy_builtins_cleanup(); |
| 57 | + |
| 58 | + return (*(int*)(a->content)) == (i-j) ? 1 : 0; |
| 59 | +} |
| 60 | + |
| 61 | +int mul_op(int i, int j){ |
| 62 | + __MPyObj *a; |
| 63 | + |
| 64 | + __mpy_builtins_setup(); |
| 65 | + a = __mpy_obj_init_object(); |
| 66 | + __mpy_obj_ref_inc(a); |
| 67 | + |
| 68 | + __mpy_obj_ref_dec(a); |
| 69 | + a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__mul__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL); |
| 70 | + __mpy_obj_ref_inc(a); |
| 71 | + |
| 72 | + __mpy_obj_ref_dec(a); |
| 73 | + |
| 74 | + printf("[MUL] i : %d --- j : %d\n",i,j); |
| 75 | + print_mpyobj_int(a); |
| 76 | + |
| 77 | + __mpy_builtins_cleanup(); |
| 78 | + |
| 79 | + return (*(int*)(a->content)) == (i*j) ? 1 : 0; |
| 80 | +} |
| 81 | + |
| 82 | +int div_op(int i, int j){ |
| 83 | + __MPyObj *a; |
| 84 | + |
| 85 | + __mpy_builtins_setup(); |
| 86 | + a = __mpy_obj_init_object(); |
| 87 | + __mpy_obj_ref_inc(a); |
| 88 | + |
| 89 | + __mpy_obj_ref_dec(a); |
| 90 | + a = __mpy_call(__mpy_obj_get_attr(__mpy_obj_init_int(i), "__div__"), __mpy_tuple_assign(0, __mpy_obj_init_int(j), __mpy_obj_init_tuple(1)), NULL); |
| 91 | + __mpy_obj_ref_inc(a); |
| 92 | + |
| 93 | + __mpy_obj_ref_dec(a); |
| 94 | + |
| 95 | + printf("[DIV] i : %d --- j : %d\n",i,j); |
| 96 | + print_mpyobj_int(a); |
| 97 | + |
| 98 | + __mpy_builtins_cleanup(); |
| 99 | + |
| 100 | + return (*(int*)(a->content)) == (i/j) ? 1 : 0; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +TEST_CASE("GENERATE ADD OP"){ |
| 105 | + auto i = GENERATE(1, 3, 5); |
| 106 | + auto j = GENERATE(2, 4, 6); |
| 107 | + CHECK(add_op(i, j) == 1); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_CASE("GENERATE SUB OP"){ |
| 111 | + auto i = GENERATE(1, 3, 5); |
| 112 | + auto j = GENERATE(2, 4, 6); |
| 113 | + CHECK(sub_op(i, j) == 1); |
| 114 | +} |
| 115 | + |
| 116 | +TEST_CASE("GENERATE MUL OP"){ |
| 117 | + auto i = GENERATE(1, 3, 5); |
| 118 | + auto j = GENERATE(2, 4, 6); |
| 119 | + CHECK(mul_op(i, j) == 1); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_CASE("GENERATE DIV OP"){ |
| 123 | + auto i = GENERATE(3, 8, 12); |
| 124 | + auto j = GENERATE(2, 4, 6); |
| 125 | + CHECK(div_op(i, j) == 1); |
| 126 | +} |
0 commit comments