-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_bank.c
147 lines (118 loc) · 3.49 KB
/
test_bank.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
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <google/cmockery.h>
#include "bank.h"
#include "money.h"
#include "sum.h"
static void
test_plus_returns_sum() {
Expression *five = dollar( 5 );
Expression *addend_five = dollar( 5 );
Expression *result = plus( five, addend_five );
Sum *sum = ( Sum * ) result->value;
assert_true( equal( money_from( five ), money_from( sum->augend ) ) );
assert_true( equal( money_from( addend_five ), money_from( sum->addend ) ) );
free_expression( five );
free_expression( addend_five );
free_expression( result );
delete_all_rates();
}
static void
test_reduce_sum() {
Expression *three_usd = dollar( 3 );
Expression *four_usd = dollar( 4 );
Expression *seven_usd = dollar( 7 );
Expression *exp = plus( three_usd, four_usd );
Money *result = reduce( exp , USD );
assert_true( equal( result , money_from( seven_usd ) ) );
free_expression( three_usd );
free_expression( four_usd );
free_expression( seven_usd );
free_expression( exp );
free_money( result );
delete_all_rates();
}
static void
test_reduce_different_currency() {
Expression *two_franc = franc( 2 );
Expression *one_dollar = dollar( 1 );
add_rate( CHF, USD, 2 );
Money *result = reduce( two_franc, USD );
assert_true( equal( result, money_from( one_dollar ) ) );
free_expression( two_franc );
free_expression( one_dollar );
free_money( result );
delete_all_rates();
}
static void
test_mixed_addition() {
Expression *five_bucks = dollar( 5 );
Expression *ten_francs = franc( 10 );
Expression *ten_usd = dollar( 10 );
add_rate( CHF, USD, 2 );
Expression *exp = plus( five_bucks, ten_francs );
Money *result = reduce( exp, USD );
assert_true( equal( money_from( ten_usd ), result ) );
free_expression( five_bucks );
free_expression( ten_francs );
free_expression( ten_usd );
free_expression( exp );
free_money( result );
delete_all_rates();
}
static void
test_multiple_addition() {
Expression *five_bucks = dollar( 5 );
Expression *ten_francs = franc( 10 );
Expression *fifteen_bucks = dollar( 15 );
add_rate( CHF, USD, 2 );
Expression *exp1 = plus( five_bucks, ten_francs );
Expression *exp2 = plus( exp1, five_bucks );
Money *result = reduce( exp2, USD );
assert_true( equal( money_from( fifteen_bucks ), result ) );
free_expression( five_bucks );
free_expression( ten_francs );
free_expression( fifteen_bucks );
free_expression( exp1 );
free_expression( exp2 );
free_money( result );
delete_all_rates();
}
static void
test_addition_multiply() {
Expression *five_bucks = dollar( 5 );
Expression *ten_francs = franc( 10 );
Expression *twenty_bucks = dollar( 20 );
add_rate( CHF, USD, 2 );
Expression *exp1 = plus( five_bucks, ten_francs );
Expression *exp2 = multiply( exp1, 2 );
Money *result = reduce( exp2, USD );
assert_true( equal( money_from( twenty_bucks ), result ) );
free_expression( five_bucks );
free_expression( ten_francs );
free_expression( twenty_bucks );
free_expression( exp1 );
free_expression( exp2 );
free_money( result );
delete_all_rates();
}
int
main() {
const UnitTest tests[] = {
unit_test( test_plus_returns_sum ),
unit_test( test_reduce_sum ),
unit_test( test_reduce_different_currency ),
unit_test( test_mixed_addition ),
unit_test( test_multiple_addition ),
unit_test( test_addition_multiply ),
};
return run_tests( tests );
}
/*
* Local variables:
* c-basic-offset: 2
* indent-tabs-mode: nil
* End:
*/