Skip to content

Commit 1155292

Browse files
committed
is_in_range / in_range
tests fixed
1 parent 6a6bf50 commit 1155292

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

test_fixtures.pl

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
11
/***********************************************************************
22
Program : Kalah game in PROLOG
3-
43
Written by : Ken Egozi
5-
64
File : test_fixtures.pl
7-
85
Description : unit tests
96
***********************************************************************/
107

118

129
/***********************************************************************
13-
% utils.pl
10+
% utils/is_in_range
1411
***********************************************************************/
15-
16-
tests(utils/in_range, [
17-
in_range__when_given_correct_input__satisfied,
18-
in_range__when_equal_to_min__satisfied,
19-
in_range__when_equal_to_max__satisfied,
20-
in_range__when_given_out_of_range_input__dissatisfied
12+
tests(utils/is_in_range, [
13+
is_in_range__when_given_correct_input__satisfied,
14+
is_in_range__when_equal_to_min__satisfied,
15+
is_in_range__when_equal_to_max__satisfied,
16+
is_in_range__when_given_out_of_range_input__dissatisfied
2117
]).
2218

23-
in_range__when_given_correct_input__satisfied :-
24-
in_range(2, 1-3).
19+
is_in_range__when_given_correct_input__satisfied :-
20+
is_in_range(2, 1-3).
2521

26-
in_range__when_equal_to_min__satisfied :-
27-
in_range(1, 1-2).
22+
is_in_range__when_equal_to_min__satisfied :-
23+
is_in_range(1, 1-2).
2824

29-
in_range__when_equal_to_max__satisfied :-
30-
in_range(2, 1-2).
25+
is_in_range__when_equal_to_max__satisfied :-
26+
is_in_range(2, 1-2).
3127

32-
in_range__when_given_out_of_range_input__dissatisfied :-
33-
not in_range(4, 1-3).
28+
is_in_range__when_given_out_of_range_input__dissatisfied :-
29+
not is_in_range(4, 1-3).
3430

3531

32+
/***********************************************************************
33+
% utils/in_range
34+
***********************************************************************/
35+
tests(utils/in_range, [
36+
in_range__when_given_correct_input__generates,
37+
in_range__when_given_incorrect_input__fails
38+
]).
39+
40+
in_range__when_given_correct_input__generates :-
41+
bagof(X, in_range(X, 1-3), Xs),
42+
Xs = [1,2,3].
43+
44+
in_range__when_given_incorrect_input__fails :-
45+
not bagof(X, in_range(X, 3-1), _).
46+
47+
48+
/***********************************************************************
49+
% utils/create_list
50+
***********************************************************************/
3651
tests(utils/create_list, [
3752
create_list__always__creates_the_list_with_the_correct_data,
3853
create_list__always__creates_the_list_with_the_correct_length
@@ -49,9 +64,33 @@
4964

5065

5166
/***********************************************************************
52-
% moves.pl
67+
% utils/conc
5368
***********************************************************************/
69+
tests(utils/conc, [
70+
conc__empty_and_empty__return_empty,
71+
conc__empty_and_nonempty__return_L2,
72+
conc__nonempty_and_empty__return_L1,
73+
conc__nonempty_and_nonempty__return_L1concL2
74+
]).
75+
76+
conc__empty_and_empty__return_empty :-
77+
conc([], [], []).
78+
79+
conc__empty_and_nonempty__return_L2 :-
80+
conc([], [1,2], [1,2]).
81+
82+
conc__nonempty_and_empty__return_L1 :-
83+
conc([1,2], [], [1,2]).
84+
85+
conc__nonempty_and_nonempty__return_L1concL2 :-
86+
conc([1,2], [3,4], [1,2,3,4]).
87+
88+
89+
5490

91+
/***********************************************************************
92+
% moves/change_list
93+
***********************************************************************/
5594
tests(moves/change_list, [
5695
change_list__add_first__works,
5796
change_list__add_middle__works,
@@ -94,9 +133,8 @@
94133

95134

96135
/***********************************************************************
97-
% moves.pl - move
136+
% moves/move
98137
***********************************************************************/
99-
100138
tests(moves/move, [
101139
move__when_ends_within_same_player_pits__works
102140
]).
@@ -109,9 +147,8 @@
109147

110148

111149
/***********************************************************************
112-
% moves.pl - step
150+
% moves/step
113151
***********************************************************************/
114-
115152
tests(moves/step, [
116153
step__when_ends_within_same_player_pits__works,
117154
step__when_ends_within_next_player_pits__works
@@ -120,15 +157,12 @@
120157
step__when_ends_within_same_player_pits__works :-
121158
P1P=pits(player1,0,0,0,0),
122159
P2P=pits(player2,0,0,0,0),
123-
Last=player1/1,
124-
step(P1P/P2P, Last, 0, NewP1P/P2P, _, _),
160+
step(P1P/P2P, player1/1, 1, NewP1P/P2P, _, _),
125161
NewP1P=pits(player1,0,1,0,0).
126162

127163
step__when_ends_within_next_player_pits__works :-
128164
P1P=pits(player1,0,0,0,0),
129165
P2P=pits(player2,0,0,0,0),
130-
Last=player1/1,
131-
step(P1P/P2P, Last, 4, NewP1P/NewP2P, _, _),
166+
step(P1P/P2P, player1/1, 4, NewP1P/NewP2P, _, _),
132167
NewP1P=pits(player1,0,1,1,1),
133168
NewP2P=pits(player2,1,0,0,0).
134-

utils.pl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,27 @@
99
***********************************************************************/
1010

1111

12+
% Usage: conc(L1,L2,L1_concatenated_with_L2)
1213
conc([], L, L) :- !.
1314
conc([H|T1], L2, [H|T2]) :-
1415
conc(T1, L2, T2).
1516

1617
% Usage: in_range(THE_INPUT, MINVALUE-MAXVALUE)
17-
% The predicate is non deterministic so do not use it in
1818
% a different manner
19-
in_range(X, Min-Max) :-
20-
X >= Min,
21-
X =< Max.
19+
is_in_range(X, LBound-UBound) :-
20+
X >= LBound,
21+
X =< UBound.
22+
23+
% in_range(LBound, X, UBound)
24+
% The predicate is non deterministic so do not use it in
25+
in_range(X, X-X) :- !.
26+
in_range(X, X-UBound) :-
27+
X < UBound.
28+
in_range(X, LBound-UBound) :-
29+
LBound =< UBound,
30+
NewLBound is LBound + 1,
31+
in_range(X, NewLBound-UBound).
32+
2233

2334
% Useful to initialise a list with repeating values
2435
% Usage: create_list(LIST_TO_CREATE, LENGTH, VALUE_TO_PUT_IN_EACH_NODE)

0 commit comments

Comments
 (0)