irrelevance_common.plt 7.87 KB
Newer Older
1
:- ensure_loaded('irrelevance_common.pl').
Siebers, Michael's avatar
Siebers, Michael committed
2
:- ensure_loaded('tests/plunit/data_theory_common.pl').
3

4
:- set_test_options([run(manual)]).
Siebers, Michael's avatar
Siebers, Michael committed
5
6
7

:- initialization(run_all_tests, main).

8
9
10
11
12
13
run_all_tests :-
    show_coverage(run_tests, [user]).
    

:- begin_tests(theory_common).

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
%%
%% file/1
%% 

%  file(+Item) is semidet.
test(file_true, forall(theory_bg:file(FileId))) :-
    file(FileId).
test(file_false, [forall(theory_bg:file_false(FileId)), fail]) :-
    file(FileId).
    
%  file(-Item) is nondet.
test(file_findall, [true(Fs == TrueFs), setup(findall(TF, theory_bg:file(TF),TrueFs)) ] ) :-
    findall(F, file(F), Fs).

    
%%
%% abs_path/1
%% 

%  abs_path(+Item,+Path) is semidet.
%  abs_path(+Item,-Path) is semidet.
%  abs_path(-Item,+Path) is semidet.
test(abs_path_semibound, forall(theory_bg:abs_path(TrueItemId,TrueAbsPath))) :-
    at_least_one_bound(abs_path/2, [TrueItemId, TrueAbsPath]).

%  abs_path(+Item, -Path) det if item is known, false else
test(abs_path_semidet) :-
    semidet_for_item(abs_path/2).
    
%%
%% file_size/1
%% 

%  file_size(+Item,+Size) is semidet.
%  file_size(+Item,-Size) is semidet.
test(file_size_semibound, forall(theory_bg:file_size(TrueFileId,TrueSize))) :-
Siebers, Michael's avatar
Siebers, Michael committed
50
    first_bound(file_size/2, [TrueFileId, TrueSize]).
51
    
52
%  file_size(+File, -Size) det if file is known, false else
53
54
test(file_size_semidet) :-
    semidet_for_file(file_size/2).
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
%%
%% directory/1
%% 

%  directory(+Item) is semidet.
test(directory_true, forall(theory_bg:directory(DirectoryId))) :-
    directory(DirectoryId).
test(directory_false, [forall(theory_bg:directory_false(DirectoryId)), fail]) :-
    directory(DirectoryId).
    
%  directory(-Item) is nondet.
test(directory_findall, [true(Fs == TrueFs), setup(findall(TF, theory_bg:directory(TF),TrueFs)) ] ) :-
    findall(F, directory(F), Fs).
    

%%
%% in_directory/2
%%
    
%  in_directory(++Item,+Directory) is semidet.
%  in_directory(++Item,-Directory) is semidet.
test(in_directory_bound, forall(theory_bg:in_directory(TrueItemId,TrueDirectory))) :-
80
    first_bound(in_directory/2,[TrueItemId, TrueDirectory]).
81
82
83
84
test(in_directory_false, [forall(theory_bg:in_directory_false(ItemId,DirectoryId)), fail]) :-
    in_directory(ItemId,DirectoryId).
    

85
86
87
88
89
90
91
92
93
94
95
%%
%% in_directory_recursive/2
%%

%  in_directory_recursive(++Item,+Directory) holds for the immediate parent
test(in_directory_parent, [ forall(theory_bg:in_directory(TrueItemId,TrueDirectory)), nondet]) :-
    in_directory_recursive(TrueItemId, TrueDirectory).


%  in_directory_recursive(++Item,+Directory) is semidet.
%  in_directory_recursive(++Item,-Directory) is semidet.
Siebers, Michael's avatar
Siebers, Michael committed
96
test(in_directory_recursive_bound, [ forall(theory_bg:in_directory_recursive(TrueItemId,TrueDirectory)), nondet]) :-
97
    first_bound(in_directory_recursive/2,[TrueItemId, TrueDirectory]).
98
test(in_directory_recursive_false, [ forall(theory_bg:in_directory_recursive_false(TrueItemId,TrueDirectory)), fail]) :-
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    in_directory_recursive(TrueItemId, TrueDirectory).

%%
%% item_name/2
%%

%  item_name(++Item,+Name) is semidet
%  item_name(++Item,-Name) is semidet
test(item_name_true, forall(theory_bg:item_name(TrueItemId,TrueName))) :-
    first_bound(item_name/2,[TrueItemId,TrueName]).
test(item_name_false, [forall(theory_bg:item_name_false(TrueItemId,TrueName)), fail]) :-
    item_name(TrueItemId,TrueName).
    
%  item_name(+Item, -Name) det if item is known, false else
test(item_name_semidet) :-
    semidet_for_item(item_name/2).

116
117
118
119
120
121
122
123
124
125
126
127
128

%%
%% item_base_name/2
%%

%  item_base_name(++Item,+Basename) is semidet
%  item_base_name(++Item,-Basename) is semidet
test(item_base_name_true, forall(theory_bg:item_base_name(TrueItemId,TrueBasename))) :-
    first_bound(item_base_name/2,[TrueItemId,TrueBasename]).
test(item_base_name_false, [forall(theory_bg:item_base_name_false(TrueItemId,TrueBasename)), fail]) :-
    item_name(TrueItemId,TrueBasename).

test(item_base_name_semidet) :-
Sebastian Seufert's avatar
Sebastian Seufert committed
129
    semidet_for_file(item_base_name/2).
130

131

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
%%
%% creation_time/2
%%

%  creation_time(+Item,-Time) is semidet.
test(creation_time_semidet_true, 
     [forall(theory_bg:creation_time(ItemId, ExpectedTime)),
      true(Time == ExpectedTime)
     ]) :- creation_time(ItemId, Time).
test(creation_time_semidet_false, 
     [forall(theory_bg:item_false(UnknownId)), fail]) :- 
  creation_time(UnknownId, _Time).

  
%%
%% modification_time/2
%%

150
151
152
153
154
155
156
157
158
159
160
161
162
163
%  modification_time(+File,-Time) is semidet.
test(modification_time_semidet_true, 
     [forall(theory_bg:modification_time(FileId, ExpectedTime)),
      true(Time == ExpectedTime)
     ]) :- modification_time(FileId, Time).
test(modification_time_semidet_false, 
     [forall(theory_bg:item_false(UnknownId)), fail]) :- 
  modification_time(UnknownId, _Time).
     
%  modification_time(+Directory, -Size) fails
test(modification_time_fails_for_directory, 
     [forall(theory_bg:file_false(DirId)), fail]) :-
  modification_time(DirId, _Time).

164

165
166
167
%%
%% access_time/2
%%
168

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
%  access_time(+File,-Time) is semidet.
test(access_time_semidet_true, 
     [forall(theory_bg:access_time(FileId, ExpectedTime)),
      true(Time == ExpectedTime)
     ]) :- access_time(FileId, Time).
test(access_time_semidet_false, 
     [forall(theory_bg:item_false(UnknownId)), fail]) :- 
  access_time(UnknownId, _Time).
     
%  access_time(+Directory, -Size) fails
test(access_time_fails_for_directory, 
     [forall(theory_bg:file_false(DirId)), fail]) :-
  access_time(DirId, _Time).


%%
%% change_time/2
%%

%  change_time(+File,-Time) is semidet.
test(change_time_semidet_true, 
     [forall(theory_bg:change_time(FileId, ExpectedTime)),
      true(Time == ExpectedTime)
     ]) :- change_time(FileId, Time).
test(change_time_semidet_false, 
     [forall(theory_bg:item_false(UnknownId)), fail]) :- 
  change_time(UnknownId, _Time).
     
%  change_time(+Directory, -Size) fails
test(change_time_fails_for_directory, 
     [forall(theory_bg:file_false(DirId)), fail]) :-
  change_time(DirId, _Time).
  
202

203
    
Siebers, Michael's avatar
Siebers, Michael committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
%%
%% newer/2
%%

%  newer(+File1,+File2) is semidet
test(newer_true, forall(theory_bg:newer(TrueItemId,TrueName))) :-
    newer(TrueItemId,TrueName).
test(newer_false, [forall(theory_bg:newer_false(TrueItemId,TrueName)), fail]) :-
    newer(TrueItemId,TrueName).
    
    
%%
%% same_directory/2
%% 

%  same_directory(+Item1,+Item2) is semidet.
%  same_directory(+Item1,-Item2) is semidet.
221
test(same_directory_semibound, [forall(theory_bg:same_directory(TrueItem1,TrueItem2)), nondet]) :-
Siebers, Michael's avatar
Siebers, Michael committed
222
223
224
225
    first_bound(same_directory/2, [TrueItem1,TrueItem2]).
test(same_directory_false, [forall(theory_bg:same_directory_false(TrueItem1,TrueItem2)), fail]) :-
    same_directory(TrueItem1,TrueItem2).
    
226
%%
227
228
%% Helper
%%
Siebers, Michael's avatar
Siebers, Michael committed
229
% test if the predicate succeeds if the first or both arguments is bound
230
231
232
233
first_bound(P/2, [TrueFirst, TrueSecond]) :-
    call(P, TrueFirst, TrueSecond), % both bound
    call(P, TrueFirst, Second), Second == TrueSecond. % second unbound
    
Siebers, Michael's avatar
Siebers, Michael committed
234
% test if the predicate succeeds if the first, the second, or both arguements are bound
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
at_least_one_bound(P/2, [TrueFirst, TrueSecond]) :-
    call(P, TrueFirst, TrueSecond), % both bound
    call(P, TrueFirst, Second), Second == TrueSecond, % first bound
    call(P, First, TrueSecond), First == TrueFirst. % second bound

semidet_for_item(P/2) :-
    findall(TI, theory_bg:item(TI), TrueItems),
    length(TrueItems, TrueItemCount),
    findall(FI, theory_bg:item_false(FI), FakeItems),
    
    findall(Sec, (member(I,TrueItems), call(P,I,Sec)), Seconds),
    length(Seconds,TrueItemCount), % succeed once for every item
    
    findall(Sec, (member(I,FakeItems), call(P, I, Sec)), []). % don't succeed for fake items
    
semidet_for_file(P/2) :-
    findall(TI, theory_bg:file(TI), TrueItems),
    length(TrueItems, TrueItemCount),
    findall(FI, theory_bg:item_false(FI), FakeItems),
    
    findall(Sec, (member(I,TrueItems), call(P,I,Sec)), Seconds),
    length(Seconds,TrueItemCount), % succeed once for every item
    
    findall(Sec, (member(I,FakeItems), call(P, I, Sec)), []). % don't succeed for fake items
    
    
:- end_tests(theory_common).