-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
104 lines (86 loc) · 2.23 KB
/
test.c
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
#include <stdio.h>
#include <stdlib.h>
#include "binary.h"
static int assertionCount = 0;
static int errorCount = 0;
static void
to_decimal_test(const long long input, const long expected) {
long result = to_decimal(input);
if (result != expected) {
errorCount++;
fprintf(stderr, "\033[31m");
fprintf(stderr, " (✖) For the binary number `%lld` expected `%ld`, got `%ld`",
input, expected, result);
fprintf(stderr, "\n\033[0m");
fprintf(stderr, "\n");
} else
{
printf("\033[32m (✓) Test passed\033[0m\n");
}
assertionCount++;
}
static void
to_binary_test(const long input, const long long expected) {
long long result = to_binary(input);
if (result != expected) {
errorCount++;
fprintf(stderr, "\033[31m");
fprintf(stderr, " (✖) For the decimal number `%ld` expected `%lld`, got `%lld`",
input, expected, result);
fprintf(stderr, "\n\033[0m");
fprintf(stderr, "\n");
} else
{
printf("\033[32m (✓) Test passed\033[0m\n");
}
assertionCount++;
}
static void
is_binary_test(const long long input, const bool expected) {
bool result = is_binary(input);
if (result != expected) {
errorCount++;
fprintf(stderr, "\033[31m");
fprintf(stderr, " (✖) For the decimal number `%lld` expected `%d`, got `%d`",
input, expected, result);
fprintf(stderr, "\n\033[0m");
fprintf(stderr, "\n");
} else
{
printf("\033[32m (✓) Test passed\033[0m\n");
}
assertionCount++;
}
int
main() {
to_decimal_test(1001001, 73);
to_decimal_test(1, 1);
to_decimal_test(111, 7);
to_decimal_test(11, 3);
to_decimal_test(0, 0);
to_binary_test(73, 1001001);
to_binary_test(1, 1);
to_binary_test(7, 111);
to_binary_test(3, 11);
to_binary_test(0, 0);
is_binary_test(111, 1);
is_binary_test(3377, 0);
is_binary_test(10101010, 1);
is_binary_test(19, 0);
is_binary_test(77777, 0);
// Log total errors.
printf("\n");
if (errorCount != 0) {
printf("\033[31m");
printf("(✖) Failed on %d of %d assertions", errorCount, assertionCount);
printf("\033[0m");
printf("\n");
exit(EXIT_FAILURE);
}
// Or, log total successes.
printf("\033[32m");
printf("(✓) Passed %d assertions without errors", assertionCount);
printf("\033[0m");
printf("\n");
return 0;
}