summaryrefslogtreecommitdiff
path: root/logic.c
blob: 8eabb6535a378dc258565dda817c41f525903862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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();
}