-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
167 lines (160 loc) · 3.84 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "fractions.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
void test_substraction();
void test_simplification();
void test_multiplication();
void test_comparison(void);
int
main()
{
test_comparison();
test_substraction();
test_simplification();
test_multiplication();
printf("All good.\n");
return EXIT_SUCCESS;
}
void
test_substraction()
{
{
/* Standard substraction */
fraction frac1 = {0, 8, 3};
fraction frac2 = {0, 4, 2};
fraction result = {0};
substract_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 2, 3};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Substracting zero */
fraction frac1 = {0, 2, 7};
fraction frac2 = {0, 0, 5};
fraction result = {0};
substract_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 2, 7};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Substracting whole numbers */
fraction frac1 = {0, 18, 1};
fraction frac2 = {0, 5, 1};
fraction result = {0};
substract_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 13, 1};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Substracting a negative number */
fraction frac1 = {0, 3, 1};
fraction frac2 = {1, 2, 1};
fraction result = {0};
substract_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 5, 1};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Substracting a bigger number */
fraction frac1 = {0, 5, 1};
fraction frac2 = {0, 7, 1};
fraction result = {0};
substract_fractions(&frac1, &frac2, &result);
fraction theorical = {1, 2, 1};
assert(compare_fractions(&result, &theorical) == 0);
}
}
void
test_simplification()
{
{
/* Multiples */
fraction frac1 = {0, 4, 2};
simplify_fraction(&frac1);
fraction theorical = {0, 2, 1};
assert(compare_fractions(&frac1, &theorical) == 0);
}
{
/* Unit fraction */
fraction frac1 = {1, 5, 5};
simplify_fraction(&frac1);
fraction theorical = {1, 1, 1};
assert(compare_fractions(&frac1, &theorical) == 0);
}
}
void
test_multiplication()
{
{
/* Regular, same denominator, no simplification */
fraction frac1 = {0, 3, 5};
fraction frac2 = {0, 2, 5};
fraction result = {0};
multiply_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 6, 25};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Division */
fraction frac1 = {0, 1, 2};
fraction frac2 = {0, 4, 1};
fraction result = {0};
multiply_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 2, 1};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* One negative */
fraction frac1 = {1, 8, 7};
fraction frac2 = {0, 3, 10};
fraction result = {0};
multiply_fractions(&frac1, &frac2, &result);
fraction theorical = {1, 12, 35};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Two negatives */
fraction frac1 = {1, 2, 3};
fraction frac2 = {1, 7, 5};
fraction result = {0};
multiply_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 14, 15};
assert(compare_fractions(&result, &theorical) == 0);
}
{
/* Whole numbers */
fraction frac1 = {0, 5, 1};
fraction frac2 = {0, 4, 1};
fraction result = {0};
multiply_fractions(&frac1, &frac2, &result);
fraction theorical = {0, 20, 1};
assert(compare_fractions(&result, &theorical) == 0);
}
}
void
test_comparison(void) {
{
/* Same fraction */
fraction frac1 = {0, 2, 1};
fraction frac2 = {0, 2, 1};
assert(compare_fractions(&frac1, &frac2) == 0);
}
{
/* Different signs */
fraction frac1 = {0, 5, 7};
fraction frac2 = {1, 5, 7};
assert(compare_fractions(&frac1, &frac2) == 1);
}
{
/* Smaller */
fraction frac1 = {0, 3, 8};
fraction frac2 = {0, 15, 11};
assert(compare_fractions(&frac1, &frac2) == -1);
}
{
fraction frac1 = {1, 6, 4};
fraction frac2 = {0, 9, 7};
assert(compare_fractions(&frac1, &frac2) == -1);
}
}