13
13
14
14
#include "rational.h"
15
15
16
+ START_TEST (test_rational_create )
17
+ {
18
+ struct Rational * r ;
19
+
20
+ r = rational_create ();
21
+
22
+ ck_assert_int_eq (r -> n , 0 );
23
+ ck_assert_int_eq (r -> d , 1 );
24
+
25
+ free (r );
26
+ }
27
+ END_TEST
28
+
16
29
START_TEST (test_rational_get )
17
30
{
18
31
struct Rational * r ;
@@ -26,6 +39,142 @@ START_TEST(test_rational_get)
26
39
}
27
40
END_TEST
28
41
42
+ START_TEST (test_rational_clone )
43
+ {
44
+ struct Rational * r , * s ;
45
+
46
+ r = rational_get (2 ,3 );
47
+ s = rational_clone (r );
48
+
49
+ ck_assert_int_eq (s -> n , 2 );
50
+ ck_assert_int_eq (s -> d , 3 );
51
+
52
+ free (r );
53
+ free (s );
54
+ }
55
+ END_TEST
56
+
57
+ START_TEST (test_rational_normalize )
58
+ {
59
+ struct Rational * r ;
60
+
61
+ r = rational_create ();
62
+ r -> n = 4 ;
63
+ r -> d = 6 ;
64
+
65
+ rational_normalize (r );
66
+
67
+ ck_assert_int_eq (r -> n , 2 );
68
+ ck_assert_int_eq (r -> d , 3 );
69
+
70
+ free (r );
71
+ }
72
+ END_TEST
73
+
74
+ START_TEST (test_rational_multiply )
75
+ {
76
+ struct Rational * r , * s , * t ;
77
+
78
+ r = rational_get (2 ,3 );
79
+ s = rational_get (6 ,1 );
80
+
81
+ t = rational_multiply (r , s );
82
+
83
+ ck_assert_int_eq (t -> n , 4 );
84
+ ck_assert_int_eq (t -> d , 1 );
85
+
86
+ free (r );
87
+ free (s );
88
+ free (t );
89
+ }
90
+ END_TEST
91
+
92
+ START_TEST (test_rational_divide )
93
+ {
94
+ struct Rational * r , * s , * t ;
95
+
96
+ r = rational_get (2 ,3 );
97
+ s = rational_get (2 ,3 );
98
+
99
+ t = rational_divide (r , s );
100
+
101
+ ck_assert_int_eq (t -> n , 1 );
102
+ ck_assert_int_eq (t -> d , 1 );
103
+
104
+ free (r );
105
+ free (s );
106
+ free (t );
107
+ }
108
+ END_TEST
109
+
110
+ START_TEST (test_rational_add )
111
+ {
112
+ struct Rational * r , * s , * t ;
113
+
114
+ r = rational_get (2 ,3 );
115
+ s = rational_get (3 ,4 );
116
+
117
+ t = rational_add (r , s );
118
+
119
+ ck_assert_int_eq (t -> n , 17 );
120
+ ck_assert_int_eq (t -> d , 12 );
121
+
122
+ free (r );
123
+ free (s );
124
+ free (t );
125
+ }
126
+ END_TEST
127
+
128
+ START_TEST (test_rational_subtract )
129
+ {
130
+ struct Rational * r , * s , * t ;
131
+
132
+ r = rational_get (2 ,3 );
133
+ s = rational_get (3 ,4 );
134
+
135
+ t = rational_subtract (r , s );
136
+
137
+ ck_assert_int_eq (t -> n , -1 );
138
+ ck_assert_int_eq (t -> d , 12 );
139
+
140
+ free (r );
141
+ free (s );
142
+ free (t );
143
+ }
144
+ END_TEST
145
+
146
+
147
+ START_TEST (test_rational_invert_sign )
148
+ {
149
+ struct Rational * r , * s ;
150
+
151
+ r = rational_get (2 ,3 );
152
+
153
+ s = rational_invert_sign (r );
154
+
155
+ ck_assert_int_eq (s -> n , -2 );
156
+ ck_assert_int_eq (s -> d , 3 );
157
+
158
+ free (r );
159
+ free (s );
160
+ }
161
+ END_TEST
162
+
163
+ START_TEST (test_rational_is_a_smaller_than_b )
164
+ {
165
+ struct Rational * r , * s ;
166
+
167
+ r = rational_get (2 ,3 );
168
+ s = rational_get (3 ,4 );
169
+
170
+ ck_assert_int_eq (rational_is_a_smaller_than_b (r , s ), 1 );
171
+ ck_assert_int_eq (rational_is_a_smaller_than_b (s , r ), 0 );
172
+
173
+ free (r );
174
+ free (s );
175
+ }
176
+ END_TEST
177
+
29
178
Suite * rational_suite (void )
30
179
{
31
180
Suite * s ;
@@ -35,7 +184,18 @@ Suite *rational_suite(void)
35
184
36
185
tc_core = tcase_create ("Core" );
37
186
187
+ tcase_add_test (tc_core , test_rational_create );
38
188
tcase_add_test (tc_core , test_rational_get );
189
+ tcase_add_test (tc_core , test_rational_clone );
190
+ tcase_add_test (tc_core , test_rational_normalize );
191
+ tcase_add_test (tc_core , test_rational_multiply );
192
+ tcase_add_test (tc_core , test_rational_divide );
193
+ tcase_add_test (tc_core , test_rational_add );
194
+ tcase_add_test (tc_core , test_rational_subtract );
195
+ tcase_add_test (tc_core , test_rational_invert_sign );
196
+ tcase_add_test (tc_core , test_rational_is_a_smaller_than_b );
197
+
198
+
39
199
suite_add_tcase (s , tc_core );
40
200
41
201
return s ;
0 commit comments