diff options
Diffstat (limited to 'logic.c')
-rw-r--r-- | logic.c | 107 |
1 files changed, 107 insertions, 0 deletions
@@ -0,0 +1,107 @@ +#define NDEBUG +#include <assert.h> + +#include "logic.h" +#include "error.h" + +enum BINARY +logic_not(enum BINARY input) { + switch (input) { + case LOGIC_HIGH: + return LOGIC_LOW; + case LOGIC_LOW: + return LOGIC_HIGH; + default: + emit_error("Severe: Invalid logic value found\n"); + } +} + +enum BINARY +logic_and(enum BINARY a, enum BINARY b) { + if (a == LOGIC_HIGH && b == LOGIC_HIGH) + return LOGIC_HIGH; + else + return LOGIC_LOW; +} + +enum BINARY +logic_or(enum BINARY a, enum BINARY b) { + if (a == LOGIC_HIGH || b == LOGIC_HIGH) + return LOGIC_HIGH; + else + return LOGIC_LOW; +} + +enum BINARY +logic_xor(enum BINARY a, enum BINARY b) { + if ( (a == LOGIC_HIGH && b == LOGIC_LOW) + || (a == LOGIC_LOW && b == LOGIC_HIGH)) + return LOGIC_HIGH; + else + return LOGIC_LOW; +} + +enum BINARY +logic_nand(enum BINARY a, enum BINARY b) { + return logic_not(logic_and(a, b)); +} + +enum BINARY +logic_nor(enum BINARY a, enum BINARY b) { + return logic_not(logic_or(a, b)); +} + +void +logic_test_and() { + emit_info("Testing AND\n"); + assert(LOGIC_HIGH == logic_and(LOGIC_HIGH, LOGIC_HIGH)); + assert(LOGIC_LOW == logic_and(LOGIC_HIGH, LOGIC_LOW )); + assert(LOGIC_LOW == logic_and(LOGIC_LOW , LOGIC_HIGH)); + assert(LOGIC_LOW == logic_and(LOGIC_LOW , LOGIC_LOW )); +} + +void +logic_test_or() { + emit_info("Testing OR\n"); + assert(LOGIC_HIGH == logic_or(LOGIC_HIGH, LOGIC_HIGH)); + assert(LOGIC_HIGH == logic_or(LOGIC_HIGH, LOGIC_LOW )); + assert(LOGIC_HIGH == logic_or(LOGIC_LOW , LOGIC_HIGH)); + assert(LOGIC_LOW == logic_or(LOGIC_LOW , LOGIC_LOW )); +} + +void +logic_test_xor() { + emit_info("Testing XOR\n"); + assert(LOGIC_HIGH == logic_xor(LOGIC_HIGH, LOGIC_LOW )); + assert(LOGIC_HIGH == logic_xor(LOGIC_LOW , LOGIC_HIGH)); + assert(LOGIC_LOW == logic_xor(LOGIC_HIGH, LOGIC_HIGH)); + assert(LOGIC_LOW == logic_xor(LOGIC_LOW , LOGIC_LOW )); +} + +void +logic_test_nand() { + emit_info("Testing NAND\n"); + assert(LOGIC_LOW == logic_nand(LOGIC_HIGH, LOGIC_HIGH)); + assert(LOGIC_HIGH == logic_nand(LOGIC_HIGH, LOGIC_LOW )); + assert(LOGIC_HIGH == logic_nand(LOGIC_LOW , LOGIC_HIGH)); + assert(LOGIC_HIGH == logic_nand(LOGIC_LOW , LOGIC_LOW )); +} + +void +logic_test_nor() { + emit_info("Testing NOR\n"); + assert(LOGIC_HIGH == logic_nor(LOGIC_LOW , LOGIC_LOW )); + assert(LOGIC_LOW == logic_nor(LOGIC_LOW , LOGIC_HIGH)); + assert(LOGIC_LOW == logic_nor(LOGIC_HIGH, LOGIC_LOW )); + assert(LOGIC_LOW == logic_nor(LOGIC_HIGH, LOGIC_HIGH)); +} + + +void +logic_test() { + logic_test_and(); + logic_test_or(); + logic_test_nand(); + logic_test_nor(); + logic_test_xor(); +} |