diff options
Diffstat (limited to 'libnormalform_false.c')
| -rw-r--r-- | libnormalform_false.c | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/libnormalform_false.c b/libnormalform_false.c new file mode 100644 index 0000000..e0708f5 --- /dev/null +++ b/libnormalform_false.c @@ -0,0 +1,155 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + + +/** + * See `.inverse` in `struct libnormalform_sentence` (FALSE implementation) + */ +static LIBNORMALFORM_SENTENCE * +false_inverse(LIBNORMALFORM_SENTENCE *this) +{ + (void) this; + return libnormalform_true(); +} + + +/** + * See `.equals` in `struct libnormalform_sentence` (FALSE implementation) + */ +static int +false_equals(LIBNORMALFORM_SENTENCE *this, LIBNORMALFORM_SENTENCE *other, int *inv_out) +{ + (void) this; + if (other->type == TYPE_FALSE) { + *inv_out = 0; + return 1; + } else if (other->type == TYPE_TRUE) { + *inv_out = 1; + return 1; + } else { + return 0; + } +} + + +/** + * See `.evaluate` in `struct libnormalform_sentence` (FALSE implementation) + */ +CONST static int +false_evaluate(LIBNORMALFORM_SENTENCE *this, void *input) +{ + (void) this; + (void) input; + return 0; +} + + +LIBNORMALFORM_SENTENCE * +(libnormalform_false)(void) +{ + static const struct libnormalform_sentence prototype = { + PROTOTYPE_COMMON, + .type = TYPE_FALSE, + .hash = TRUE_FALSE_HASH, + .inverse = &false_inverse, + .equals = &false_equals, + .evaluate = &false_evaluate + }; + + /* + * During normalisation, some fields may be set, + * therefore a new allocation is returned instead + * of a reference to a static allocation, so that + * converation can be done from the threads at + * the same time on different sentences, both + * including this constant. + */ + + LIBNORMALFORM_SENTENCE *ret = malloc(sizeof(*ret)); + if (ret) + *ret = prototype; + return ret; +} + + +#else + + +static int +tautology(void *user_data, void *input) +{ + (void) user_data; + (void) input; + return 1; +} + + +static int +contradiction(void *user_data, void *input) +{ + (void) user_data; + (void) input; + return 0; +} + + +int +main(void) +{ + TEST_BEGIN; + + struct libnormalform_variable var1, var2; + struct libnormalform_function fun1, fun2; + struct libnormalform_map domain; + LIBNORMALFORM_SENTENCE *a, *b, *v1, *v2, *f1, *f2; + + var1.value = LIBNORMALFORM_FALSE; + var2.value = LIBNORMALFORM_TRUE; + fun1.evaluate = &tautology; + fun2.evaluate = &contradiction; + + ASSUME(v1 = libnormalform_variable(&var1)); + ASSUME(v2 = libnormalform_variable(&var2)); + ASSUME(f1 = libnormalform_function(&fun1)); + ASSUME(f2 = libnormalform_function(&fun2)); + + ASSUME(a = libnormalform_false()); + ASSERT(a->type == TYPE_FALSE); + ASSERT(a->refcount == 1); + ASSERT_EQUAL(a, a); + + ASSERT(a->evaluate(a, NULL) == 0); + +#define CHECK(CMP, X)\ + do {\ + ASSUME(b = (X));\ + ASSERT_##CMP(a, b);\ + libnormalform_free(b);\ + } while (0) + + CHECK(EQUAL, libnormalform_false()); + CHECK(INVEQUAL, libnormalform_true()); + CHECK(NOTEQUAL, libnormalform_and2(REF(v1), REF(v2))); + CHECK(NOTEQUAL, libnormalform_or2(REF(v1), REF(v2))); + CHECK(NOTEQUAL, libnormalform_xor2(REF(v1), REF(v2))); + CHECK(NOTEQUAL, libnormalform_all(&domain, REF(f1), REF(f2))); + CHECK(NOTEQUAL, libnormalform_any(&domain, REF(f1), REF(f2))); + CHECK(NOTEQUAL, libnormalform_one(&domain, REF(f1), REF(f2))); + CHECK(NOTEQUAL, libnormalform_not(libnormalform_one(&domain, REF(f1), REF(f2)))); + CHECK(NOTEQUAL, REF(v1)); + CHECK(NOTEQUAL, REF(f1)); + +#undef CHECK + + libnormalform_free(a); + + libnormalform_free(v1); + libnormalform_free(v2); + libnormalform_free(f1); + libnormalform_free(f2); + + TEST_END; +} + +#endif |
