4
:- asserta(user:file_search_path(foreign, '.')).
5
:- asserta(user:file_search_path(foreign, '../clib')).
6
:- asserta(user:file_search_path(library, '.')).
7
:- asserta(user:file_search_path(library, '../plunit')).
8
:- asserta(user:file_search_path(library, '../clib')).
10
:- use_module(user:library(zlib)).
11
:- use_module(user:library(plunit)).
12
:- use_module(user:library(readutil)).
13
:- use_module(user:library(socket)).
14
:- use_module(library(debug)).
16
read_file_to_codes(File, Codes) :-
18
call_cleanup(read_stream_to_codes(In, Codes), close(In)).
22
% gunzip: can we read a file compressed with gzip
25
[ setup(shell('gzip < ztest.pl > plunit-tmp.gz')),
26
cleanup(delete_file('plunit-tmp.gz'))
28
gzopen('plunit-tmp.gz', read, ZIn),
29
call_cleanup(read_stream_to_codes(ZIn, Codes0), close(ZIn)),
30
read_file_to_codes('ztest.pl', Codes1),
33
% gzip: Can gunzip read our compressed file
36
[ cleanup(delete_file('plunit-tmp.gz'))
38
read_file_to_codes('ztest.pl', Codes),
39
gzopen('plunit-tmp.gz', write, ZOut),
40
format(ZOut, '~s', [Codes]),
42
read_file_to_codes(pipe('gunzip < plunit-tmp.gz'), Codes1),
45
% deflate: test read/write of deflate format
48
[ cleanup(delete_file('plunit-tmp.z'))
50
read_file_to_codes('ztest.pl', Codes),
51
open('plunit-tmp.z', write, Out),
53
format(ZOut, '~s', [Codes]),
55
open('plunit-tmp.z', read, In),
57
read_stream_to_codes(ZIn, Codes1),
61
% zstream: test compressed stream flushing and processing
65
debug(server, 'Server at ~w~n', [Port]),
67
thread_join(server, Exit),
74
tcp_open_socket(S, AcceptFd, _),
75
thread_create(process(AcceptFd), _, [alias(server)]).
78
tcp_accept(AcceptFd, S2, _Peer),
79
tcp_open_socket(S2, ZIn, ZOut),
83
close(Out), close(In).
87
debug(server, 'Read ~w', [Term]),
88
format(Out, '~q.~n', [Term]),
90
debug(server, 'Replied', [Term]),
98
client(localhost:Port).
101
tcp_connect(S, Address),
102
tcp_open_socket(S, ZIn, ZOut),
104
zopen(ZOut, Out, []),
105
process_client(In, Out),
109
process_client(In, Out) :-
110
forall(between(0, 50, X),
111
( format(Out, '~q.~n', [X]),
117
; format('Wrong reply~n'),
121
format(Out, 'quit.~n', []).
124
/*******************************
126
*******************************/
129
forall(between(1, 5, I),
135
tcp_setopt(SockFd, reuseaddr),
136
tcp_bind(SockFd, Port),
137
tcp_listen(SockFd, 5),
138
thread_create(client_test(Port, N), Client, []),
139
tcp_accept(SockFd, ClientFd, _Peer),
140
tcp_open_socket(ClientFd, InStream, OutStream),
141
zopen(OutStream, ZOut, [close_parent(false), format(deflate)]),
142
send_data(1, N, ZOut),
144
character_count(ZOut, RawCnt),
146
character_count(OutStream, CompressedCnt),
147
debug(zlib, 'compressed ~d into ~d bytes~n',
148
[RawCnt, CompressedCnt]),
150
tcp_close_socket(SockFd),
151
thread_join(Client, Status),
152
assertion(Status == true).
154
send_data(I, N, ZOut) :-
156
format(ZOut, '~d.~n', [I]),
158
send_data(I2, N, ZOut).
162
client_test(Port, N) :-
164
tcp_connect(SockFd, localhost:Port),
165
tcp_open_socket(SockFd, In, Out),
166
zopen(In, ZIn, [format(deflate)]),
177
format('EOF at ~w~n', [X])
184
( Term == end_of_file
186
assertion(X =:= N + 1)
187
; assertion(Term == X),