-
Notifications
You must be signed in to change notification settings - Fork 16
/
Chapter_02_Tests.fs
148 lines (129 loc) · 4.72 KB
/
Chapter_02_Tests.fs
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
module Chapter_02_Tests
open System
open NUnit.Framework
open FsUnit
open Chapter_02
[<TestFixture>]
type ``Chapter_02_Tests``() =
[<Test>]
member x.``2.1``() =
f 5 |> should equal false
f 6 |> should equal true
f 15 |> should equal false
f 24 |> should equal true
f 27 |> should equal true
f 29 |> should equal false
f 30 |> should equal false
member x.``powTester`` powf =
powf "a" -1 |> should equal "" // I've chosen to define it like this
powf "a" 0 |> should equal ""
powf "a" 1 |> should equal "a"
powf "a" 2 |> should equal "aa"
powf "a" 42 |> should equal "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
powf "" 0 |> should equal ""
powf "" 1 |> should equal ""
powf "" 2 |> should equal ""
powf "" 42 |> should equal ""
[<Test>]
member x.``2.2_recursive``() =
x.powTester pow
[<Test>]
member x.``2.2_stringBuilder``() =
x.powTester powWithBuilder
[<Test>]
member x.``2.2_stringConcat``() =
x.powTester powWithStringConcat
[<Test>]
member x.``2.3 isIthChar``() =
isIthChar ("abcdef", 2, 'c') |> should equal true
isIthChar ("abcdef", 2, 'd') |> should equal false
isIthChar ("abcdef", 17, 'g') |> should equal false
[<Test>]
member x.``2.4 occFromIth``() =
occFromIth ("abcabcabc", -1, 'a') |> should equal 0
occFromIth ("abcabcabc", 0, 'a') |> should equal 3
occFromIth ("abcabcabc", 5, 'a') |> should equal 1
occFromIth ("abcabcabc", 15, 'a') |> should equal 0
[<Test>]
member x.``2.5 occInString``() =
occInString ("abcbcecde", 'a') |> should equal 1
occInString ("abcbcecde", 'b') |> should equal 2
occInString ("abcbcecde", 'c') |> should equal 3
occInString ("abcbcecde", 'e') |> should equal 2
occInString ("abcbcecde", 'f') |> should equal 0
[<Test>]
member x.``2.6 notDivisible``() =
notDivisible (-2, -5) |> should equal true
notDivisible (2, 5) |> should equal true
notDivisible (9, 3) |> should equal false
[<Test>]
member x.``2.7 1. test``() =
test(4, 6, 2) |> should equal false
test(6, 9, 5) |> should equal true
test_if(4, 6, 2) |> should equal false
test_if(6, 9, 5) |> should equal true
[<Test>]
member x.``2.7 2. prime``() =
prime 1 |> should equal true
prime 2 |> should equal true
prime 3 |> should equal true
prime 4 |> should equal false
prime 5 |> should equal true
prime 6 |> should equal false
prime 7 |> should equal true
prime 8 |> should equal false
prime 9 |> should equal false
prime 10 |> should equal false
prime 23 |> should equal true
[<Test>]
member x.``2.7 3. nextPrime``() =
nextPrime 1 |> should equal 2
nextPrime 2 |> should equal 3
nextPrime 3 |> should equal 5
nextPrime 4 |> should equal 5
nextPrime 5 |> should equal 7
nextPrime 6 |> should equal 7
nextPrime 7 |> should equal 11
nextPrime 8 |> should equal 11
nextPrime 9 |> should equal 11
nextPrime 10 |> should equal 11
nextPrime 23 |> should equal 29
[<Test>]
member x.``2.8 binomial coefficients``() =
bin(0,0) |> should equal 1
bin(1,0) |> should equal 1
bin(1,1) |> should equal 1
bin(2,0) |> should equal 1
bin(2,1) |> should equal 2
bin(2,2) |> should equal 1
bin(3,0) |> should equal 1
bin(3,1) |> should equal 3
bin(3,2) |> should equal 3
bin(3,3) |> should equal 1
bin(4,0) |> should equal 1
bin(4,1) |> should equal 4
bin(4,2) |> should equal 6
bin(4,3) |> should equal 4
bin(4,4) |> should equal 1
bin(5,0) |> should equal 1
bin(5,1) |> should equal 5
bin(5,2) |> should equal 10
bin(5,3) |> should equal 10
bin(5,4) |> should equal 5
bin(5,5) |> should equal 1
[<Test>]
member x.``2.11 VAT``() =
VAT 10 100.0 |> should equal 110.0
VAT 0 1.0 |> should equal 1.0
[<Test>]
member x.``2.11 unVAT``() =
unVAT 10 110.0 |> should (equalWithin 0.01) 100.0//be (greaterThan 99.99)
unVAT 0 1.0 |> should equal 1.0
unVAT 10 (VAT 10 100.0) |> should (equalWithin 0.01) 100.0 //be (greaterThan 99.99)
unVAT 0 (VAT 0 1.0) |> should equal 1.0
[<Test>]
member x.``2.12 min(f)``() =
min(fun n -> n % 2) |> should equal 2
// Disabled - way to slow ;)
// min(fun n -> if n = 0 then 0 else 1) |> should equal 0
min(fun n -> n % 10000) |> should equal 10000