<commit_before>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME> and <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2007, University of Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

%:- set_prolog_flag(optimise, true).
%:- set_prolog_flag(trace_gc, true).
:- asserta(user:file_search_path(library, '../packages/plunit')).
:- [library(plunit)].
:- set_test_options([load(always), silent(true)]).

:- use_module(library(lists)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SWI-Prolog test file.  A test is a clause of the form:

	<TestSet>(<Name>-<Number>) :- Body.

If the body fails, an appropriate  error   message  is  printed. So, all
goals are supposed to  succeed.  The   predicate  testset/1  defines the
available test sets. The public goals are:

	?- runtest(+TestSet).
	?- test.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- format('SWI-Prolog test suite.  To run all tests run ?- test.~n~n', []).

% Required to get this always running regardless of user LANG setting.
% Without this the tests won't run on machines with -for example- LANG=ja
% according to <NAME>, <EMAIL>.  Thanks!
%
% NOTE: In 5.5.x we should not need this any longer

% :- getenv('LANG', _) -> setenv('LANG', 'C'); true.

% The test-suite Tests/library/test_date.pl depends on  the timezone. As
% correct results are only  provided  for   the  CET  (Central European)
% timezine we use this. Timezone cannot be  changed at runtime, so we do
% this early.

:- setenv('TZ', 'CET').

		 /*******************************
		 *    CONDITIONAL COMPILATION	*
		 *******************************/

:- dynamic
	user:include_code/1.

term_expansion((:- if(G)), []) :-
	prolog_load_context(module, M),
	(   catch(M:G, E, (print_message(error, E), fail))
	->  asserta(user:include_code(true))
	;   asserta(user:include_code(false))
	).
term_expansion((:- else), []) :-
	(   retract(user:include_code(X))
	->  (   X == true
	    ->  X2 = false 
	    ;   X2 = true
	    ),
	    asserta(user:include_code(X2))
	;   throw(error(context_error(no_if), _))
	).
term_expansion((:- endif), []) :-
	retract(user:include_code(_)), !.

term_expansion(_, []) :-
	user:include_code(X), !,
	X == false.
	    

		 /*******************************
		 *	      SYNTAX		*
		 *******************************/

syntax(op-1) :-
	atom_to_term("3+4*5", +(3,*(4,5)), []).
syntax(op-2) :-
	atom_to_term("1+2+3", +(+(1,2),3), []).
syntax(op-3) :-
	catch(atom_to_term("a:-b:-c", _, _), E, true),
	E = error(syntax_error(operator_clash), _).
syntax(op-4) :-
	op(600, fx, op1),
	atom_to_term("op1 1+2", op1(+(1,2)), []).
syntax(op-5) :-
	op(600, fx, op1),
	catch(atom_to_term("op1 op1 1", _, _), E, true),
	E = error(syntax_error(operator_clash), _).
syntax(op-6) :-
	op(600, fy, op1),
	atom_to_term("op1 op1 1", op1(op1(1)), []).
syntax(op-7) :-
	op(600, fy, op1),
	op(500, xf, op2),
	catch(atom_to_term("op1 a op2", op1(op2(a)), []), E, true),
	E = error(syntax_error(operator_clash), _).
syntax(op-8) :-				% assume 200 fy and 500 yfx
	atom_to_term("- - a", -(-(a)), []).
syntax(atom-1) :-
	atom_codes('\003\\'\n\x80\', X),
	X == [3, 39, 10, 128].
syntax(char-1) :-
	10 == 0'\n.
syntax(char-2) :-
	52 == 0'\x34.
syntax(char-3) :-
	"\\" =:= 0'\\.

syntax(string-1) :-
	'\c ' == ''.
syntax(string-2) :-
	'x\c y' == xy.
syntax(quote-1) :-			% not really clear this is ok
	'\x' == x.
syntax(quote-2) :-
	'\x61' == a.
syntax(quote-3) :-
	'\x61\' == a.
syntax(quote-4) :-
	char_code('\'', 39).
syntax(number-2) :-
	catch(atom_to_term('2\'', _, _), E, true),
	E = error(syntax_error(illegal_number), _).
syntax(zero-1) :-
	term_to_atom(T, 'hello(\000\"\000\x")'),
	T == hello([0, 120]).
syntax(latin-1) :-
	atom_codes(A, [247]),
	atom_to_term(A, T, []),
	atom_codes(T, [247]).
	

		 /*******************************
		 *	       WRITE		*
		 *******************************/

write_test(q-1) :-
	term_to_atom(-(0), X), X == '-(0)'.
write_test(q-2) :-
	term_to_atom(+(0), X), X == '+(0)'.
write_test(q-3) :-
	term_to_atom(+(a), X), X == '+a'.
write_test(q-4) :-
	term_to_atom('/*', X), X == '\'/*\''.	%'
write_test(q-5) :-
	term_to_atom('/**', X), X == '\'/**\''.
write_test(q-6) :-
	term_to_atom('*/*', X), X == '*/*'.
write_test(c-1) :-
	T = [a,b,c|T],
	term_to_atom(T, X), X == '[a, b, c|**]'.


		 /*******************************
		 *	      FORMAT		*
		 *******************************/

format_test(intD-1) :-
	format(atom(X), '~D', [1000]),
	X == '1,000'.
format_test(intD-2) :-
	format(atom(X), '~2D', [1000]),
	X == '10.00'.
format_test(intD-3) :-
	format(atom(X), '~2D', [100000]),
	X == '1,000.00'.
format_test(intr-1) :-
	format(atom(X), '~16r', [1000]),
	X == '3e8'.
format_test(intR-1) :-
	format(atom(X), '~16R', [1000]),
	X == '3E8'.
format_test(oncodes-1) :-
	format(codes(C), 'hello ~w', [world]),
	C == "hello world".
format_test(oncodes-2) :-
	format(codes(C,T), 'hello ~w', [world]),
	append("hello world", T2, C2),
	C-T =@= C2-T2.
format_test(onstring-1) :-
	format(string(S), 'hello ~w', [world]),
	string(S),
	string_to_list(S, "hello world").


		 /*******************************
		 *	       UNIFY		*
		 *******************************/

%	Some cyclic unification tests (normal unification should be fixed
%	already).

unify(cycle-1) :-			% Kuniaki Mukai
	X = f(Y), Y=f(X), X=Y.
unify(cycle-2) :-			% Kuniaki Mukai
	X = f(X), Y=f(Y), X=f(Y).


		 /*******************************
		 *    UNIFY-WITH-OCCURS-CHECK	*
		 *******************************/

occurs_check(simple-1) :-
	\+ unify_with_occurs_check(A, list(A)).
occurs_check(simple-2) :-
	unify_with_occurs_check(_A, _B).
occurs_check(attvar-1) :-		% test wakeup
	freeze(X, X = Y),
	unify_with_occurs_check(X, a), 
	Y == a.
occurs_check(attvar-2) :-		% test occurs-check
	freeze(A, true),
	\+ unify_with_occurs_check(A, list(A)).
occurs_check(attvar-3) :-
	freeze(A, true),
	unify_with_occurs_check(A, A).
occurs_check(attvar-4) :-
	freeze(A, true),
	freeze(B, true),
	unify_with_occurs_check(A, B).


		 /*******************************
		 *	       CYCLIC		*
		 *******************************/

cyclic(hash_term-1) :-
	X = f(X), hash_term(X, T),
	integer(T).
cyclic(streq-1) :-
	X = [X], Y = [Y], X =@= Y.
cyclic(test-1) :-
	X = f(X),
	cyclic_term(X).
cyclic(test-2) :-
	X = f(X),
	cyclic_term(a(X, a)).
cyclic(test-3) :-
	X = f(X),
	cyclic_term(a(a, X)).
cyclic(test-4) :-
	acyclic_term(f(x)).
cyclic(test-5) :-
	acyclic_term(_).
cyclic(test-6) :-
	X = f(a), acyclic_term(a(X, X)).
cyclic(list-1) :-
	L = [a|L], \+ is_list(L).


		 /*******************************
		 *	    UNIFIABLE		*
		 *******************************/

unifiable(unifiable-1) :-
	unifiable(X, 1, S),
	S == [X=1].
unifiable(unifiable-2) :-
	unifiable(a(X,X), a(Y,Z), S),
	S == [Z=X, Y=X].


		 /*******************************
		 *      INTEGER ARITHMETIC	*
		 *******************************/

arithmetic(between-1) :-
	between(0, 10, 5).
arithmetic(between-2) :-
	\+ between(0, 10, 20).
arithmetic(between-3) :-
	findall(X, between(1, 6, X), Xs),
	Xs == [1, 2, 3, 4, 5, 6].<commit_msg>* Added cycle check for sort/2<commit_after>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME> and <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2007, University of Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

%:- set_prolog_flag(optimise, true).
%:- set_prolog_flag(trace_gc, true).
:- asserta(user:file_search_path(library, '../packages/plunit')).
:- [library(plunit)].
:- set_test_options([load(always), silent(true)]).

:- use_module(library(lists)).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SWI-Prolog test file.  A test is a clause of the form:

	<TestSet>(<Name>-<Number>) :- Body.

If the body fails, an appropriate  error   message  is  printed. So, all
goals are supposed to  succeed.  The   predicate  testset/1  defines the
available test sets. The public goals are:

	?- runtest(+TestSet).
	?- test.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- format('SWI-Prolog test suite.  To run all tests run ?- test.~n~n', []).

% Required to get this always running regardless of user LANG setting.
% Without this the tests won't run on machines with -for example- LANG=ja
% according to <NAME>, <EMAIL>.  Thanks!
%
% NOTE: In 5.5.x we should not need this any longer

% :- getenv('LANG', _) -> setenv('LANG', 'C'); true.

% The test-suite Tests/library/test_date.pl depends on  the timezone. As
% correct results are only  provided  for   the  CET  (Central European)
% timezine we use this. Timezone cannot be  changed at runtime, so we do
% this early.

:- setenv('TZ', 'CET').

		 /*******************************
		 *    CONDITIONAL COMPILATION	*
		 *******************************/

:- dynamic
	user:include_code/1.

term_expansion((:- if(G)), []) :-
	prolog_load_context(module, M),
	(   catch(M:G, E, (print_message(error, E), fail))
	->  asserta(user:include_code(true))
	;   asserta(user:include_code(false))
	).
term_expansion((:- else), []) :-
	(   retract(user:include_code(X))
	->  (   X == true
	    ->  X2 = false 
	    ;   X2 = true
	    ),
	    asserta(user:include_code(X2))
	;   throw(error(context_error(no_if), _))
	).
term_expansion((:- endif), []) :-
	retract(user:include_code(_)), !.

term_expansion(_, []) :-
	user:include_code(X), !,
	X == false.
	    

		 /*******************************
		 *	      SYNTAX		*
		 *******************************/

syntax(op-1) :-
	atom_to_term("3+4*5", +(3,*(4,5)), []).
syntax(op-2) :-
	atom_to_term("1+2+3", +(+(1,2),3), []).
syntax(op-3) :-
	catch(atom_to_term("a:-b:-c", _, _), E, true),
	E = error(syntax_error(operator_clash), _).
syntax(op-4) :-
	op(600, fx, op1),
	atom_to_term("op1 1+2", op1(+(1,2)), []).
syntax(op-5) :-
	op(600, fx, op1),
	catch(atom_to_term("op1 op1 1", _, _), E, true),
	E = error(syntax_error(operator_clash), _).
syntax(op-6) :-
	op(600, fy, op1),
	atom_to_term("op1 op1 1", op1(op1(1)), []).
syntax(op-7) :-
	op(600, fy, op1),
	op(500, xf, op2),
	catch(atom_to_term("op1 a op2", op1(op2(a)), []), E, true),
	E = error(syntax_error(operator_clash), _).
syntax(op-8) :-				% assume 200 fy and 500 yfx
	atom_to_term("- - a", -(-(a)), []).
syntax(atom-1) :-
	atom_codes('\003\\'\n\x80\', X),
	X == [3, 39, 10, 128].
syntax(char-1) :-
	10 == 0'\n.
syntax(char-2) :-
	52 == 0'\x34.
syntax(char-3) :-
	"\\" =:= 0'\\.

syntax(string-1) :-
	'\c ' == ''.
syntax(string-2) :-
	'x\c y' == xy.
syntax(quote-1) :-			% not really clear this is ok
	'\x' == x.
syntax(quote-2) :-
	'\x61' == a.
syntax(quote-3) :-
	'\x61\' == a.
syntax(quote-4) :-
	char_code('\'', 39).
syntax(number-2) :-
	catch(atom_to_term('2\'', _, _), E, true),
	E = error(syntax_error(illegal_number), _).
syntax(zero-1) :-
	term_to_atom(T, 'hello(\000\"\000\x")'),
	T == hello([0, 120]).
syntax(latin-1) :-
	atom_codes(A, [247]),
	atom_to_term(A, T, []),
	atom_codes(T, [247]).
	

		 /*******************************
		 *	       WRITE		*
		 *******************************/

write_test(q-1) :-
	term_to_atom(-(0), X), X == '-(0)'.
write_test(q-2) :-
	term_to_atom(+(0), X), X == '+(0)'.
write_test(q-3) :-
	term_to_atom(+(a), X), X == '+a'.
write_test(q-4) :-
	term_to_atom('/*', X), X == '\'/*\''.	%'
write_test(q-5) :-
	term_to_atom('/**', X), X == '\'/**\''.
write_test(q-6) :-
	term_to_atom('*/*', X), X == '*/*'.
write_test(c-1) :-
	T = [a,b,c|T],
	term_to_atom(T, X), X == '[a, b, c|**]'.


		 /*******************************
		 *	      FORMAT		*
		 *******************************/

format_test(intD-1) :-
	format(atom(X), '~D', [1000]),
	X == '1,000'.
format_test(intD-2) :-
	format(atom(X), '~2D', [1000]),
	X == '10.00'.
format_test(intD-3) :-
	format(atom(X), '~2D', [100000]),
	X == '1,000.00'.
format_test(intr-1) :-
	format(atom(X), '~16r', [1000]),
	X == '3e8'.
format_test(intR-1) :-
	format(atom(X), '~16R', [1000]),
	X == '3E8'.
format_test(oncodes-1) :-
	format(codes(C), 'hello ~w', [world]),
	C == "hello world".
format_test(oncodes-2) :-
	format(codes(C,T), 'hello ~w', [world]),
	append("hello world", T2, C2),
	C-T =@= C2-T2.
format_test(onstring-1) :-
	format(string(S), 'hello ~w', [world]),
	string(S),
	string_to_list(S, "hello world").


		 /*******************************
		 *	       UNIFY		*
		 *******************************/

%	Some cyclic unification tests (normal unification should be fixed
%	already).

unify(cycle-1) :-			% Kuniaki Mukai
	X = f(Y), Y=f(X), X=Y.
unify(cycle-2) :-			% Kuniaki Mukai
	X = f(X), Y=f(Y), X=f(Y).


		 /*******************************
		 *    UNIFY-WITH-OCCURS-CHECK	*
		 *******************************/

occurs_check(simple-1) :-
	\+ unify_with_occurs_check(A, list(A)).
occurs_check(simple-2) :-
	unify_with_occurs_check(_A, _B).
occurs_check(attvar-1) :-		% test wakeup
	freeze(X, X = Y),
	unify_with_occurs_check(X, a), 
	Y == a.
occurs_check(attvar-2) :-		% test occurs-check
	freeze(A, true),
	\+ unify_with_occurs_check(A, list(A)).
occurs_check(attvar-3) :-
	freeze(A, true),
	unify_with_occurs_check(A, A).
occurs_check(attvar-4) :-
	freeze(A, true),
	freeze(B, true),
	unify_with_occurs_check(A, B).


		 /*******************************
		 *	       CYCLIC		*
		 *******************************/

cyclic(hash_term-1) :-
	X = f(X), hash_term(X, T),
	integer(T).
cyclic(streq-1) :-
	X = [X], Y = [Y], X =@= Y.
cyclic(test-1) :-
	X = f(X),
	cyclic_term(X).
cyclic(test-2) :-
	X = f(X),
	cyclic_term(a(X, a)).
cyclic(test-3) :-
	X = f(X),
	cyclic_term(a(a, X)).
cyclic(test-4) :-
	acyclic_term(f(x)).
cyclic(test-5) :-
	acyclic_term(_).
cyclic(test-6) :-
	X = f(a), acyclic_term(a(X, X)).
cyclic(list-1) :-
	L = [a|L], \+ is_list(L).
cyclic(sort-1) :-
	L = [a|L],
	catch(sort(L, _), error(E, _), true),
	E == type_error(list, L).


		 /*******************************
		 *	    UNIFIABLE		*
		 *******************************/

unifiable(unifiable-1) :-
	unifiable(X, 1, S),
	S == [X=1].
unifiable(unifiable-2) :-
	unifiable(a(X,X), a(Y,Z), S),
	S == [Z=X, Y=X].


		 /*******************************
		 *      INTEGER ARITHMETIC	*
		 *******************************/

arithmetic(between-1) :-
	between(0, 10, 5).
arithmetic(between-2) :-
	\+ between(0, 10, 20).
arithmetic(between-3) :-
	findall(X, between(1, 6, X), Xs),
	Xs == [1, 2, 3, 4, 5, 6].
{'fraction_non_alphanumeric': 0.2729697123626897, 'fraction_numerical': 0.033652930344783796, 'mean_word_length': 3.244139046079224, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2673519', 'n_tokens_mistral': 6950, 'n_tokens_neox': 6324, 'n_words': 1347}