<commit_before>%%
%% This file is part of Triq - Trifork QuickCheck
%%
%% Copyright (c) 2010 by Trifork
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%  
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%


-module(triq_simplify).

-include("triq_domain.hrl").

-export([simplify_value/2, simplify_internal/2]).
-import(triq_domain, [generate/2, elem_gen/2]).

%%
%% public API looks like this
%%
simplify_value(Dom=#?DOM{simplify=SFun}, Val) ->
    SFun(Dom,Val);

simplify_value(Dom,Val) ->
    simplify_internal(Dom,Val).


%%
%% when the domain is a tuple...
%%
simplify_tuple(TupDom,{},_) ->
    {};

simplify_tuple(TupDom,Tup,0) ->
    Tup;

simplify_tuple(TupDom,Tup,NAttempts) when is_tuple(TupDom), is_tuple(Tup) ->
    case simplify_member(Tup, TupDom, random:uniform(len(Tup)+1)) of
	Tup -> simplify_tuple(TupDom, Tup, NAttempts-1);
	NewTup -> NewTup
    end.

%%
%% when the domain is a list...
%%
simplify_list(ListDom,[],_) ->
    [];

simplify_list(ListDom,List,0) ->
    List;

simplify_list(ListDom,List,NAttempts) when is_list(ListDom), is_list(List) ->
    case simplify_member(List, ListDom, random:uniform(len(List)+1)) of
	List -> simplify_list(ListDom, List, NAttempts-1);
	NewList -> NewList
    end.

%%
%% atoms...
%%
simplify_atom(_,'',_) ->
    '';
simplify_atom(TupDom,Tup,0) ->
    Tup;

simplify_atom(TupDom,Tup,NAttempts) when is_atom(Tup) ->
    case simplify_member(Tup, TupDom, random:uniform(len(Tup)+1)) of
	Tup -> simplify_atom(TupDom, Tup, NAttempts-1);
	NewTup -> NewTup
    end.

%%
%% the meat of the shrinking function
%%
simplify_internal(_, []) -> 
    [];

simplify_internal(_, {}) -> 
    {};

simplify_internal(_, '') -> 
    '';

simplify_internal(_, 0) -> 
    0;

simplify_internal(_, 0.0) -> 
    0.0;

simplify_internal(#?DOM{kind=any}=Dom, Val) when is_tuple(Val) ->
    list_to_tuple(simplify_internal(Dom,tuple_to_list(Val)));

simplify_internal(#?DOM{kind=any}=Dom, Val) when is_atom(Val) ->
    list_to_atom(simplify_internal(Dom,atom_to_list(Val)));



simplify_internal(TupDom,Tup) when is_tuple(TupDom), is_tuple(Tup) ->
    %% try to simplify it 10 times...
    simplify_tuple(TupDom, Tup, 10);

simplify_internal(ListDom,List) when is_tuple(ListDom), is_tuple(List) ->
    %% try to simplify it 10 times...
    simplify_tuple(ListDom, List, 10);


%%
%% aggregates (tuple or list) are simplified either by 
%% simplifying one of the elements, or ... by removing
%% one of the elements
%%
simplify_internal(AggrDom,Aggr) when is_list(Aggr); is_tuple(Aggr)->
    NewAggr = 
    case random:uniform(2) of
	1 -> remove_any(Aggr);
	2 -> simplify_member(Aggr, AggrDom, random:uniform(len(Aggr)+1))
    end,

    %% if the above did not change anything; 
    %% or the change was illegal according to AggrDom ...
    case NewAggr of 
	Aggr -> case random:uniform(2) of<commit_msg>Fix bug in simplifying values in the list-domain<commit_after>%%
%% This file is part of Triq - Trifork QuickCheck
%%
%% Copyright (c) 2010 by Trifork
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%  
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%


-module(triq_simplify).

-include("triq_domain.hrl").

-export([simplify_value/2, simplify_internal/2]).
-import(triq_domain, [generate/2, elem_gen/2]).

%%
%% public API looks like this
%%
simplify_value(Dom=#?DOM{simplify=SFun}, Val) ->
    SFun(Dom,Val);

simplify_value(Dom,Val) ->
    simplify_internal(Dom,Val).


%%
%% when the domain is a tuple...
%%
simplify_tuple(TupDom,{},_) ->
    {};

simplify_tuple(TupDom,Tup,0) ->
    Tup;

simplify_tuple(TupDom,Tup,NAttempts) when is_tuple(TupDom), is_tuple(Tup) ->
    case simplify_member(Tup, TupDom, random:uniform(len(Tup)+1)) of
	Tup -> simplify_tuple(TupDom, Tup, NAttempts-1);
	NewTup -> NewTup
    end.

%%
%% when the domain is a list...
%%
simplify_list(ListDom,[],_) ->
    [];

simplify_list(ListDom,List,0) ->
    List;

simplify_list(ListDom,List,NAttempts) when is_list(ListDom), is_list(List) ->
    case simplify_member(List, ListDom, random:uniform(len(List)+1)) of
	List -> simplify_list(ListDom, List, NAttempts-1);
	NewList -> NewList
    end.

%%
%% atoms...
%%
simplify_atom(_,'',_) ->
    '';
simplify_atom(TupDom,Tup,0) ->
    Tup;

simplify_atom(TupDom,Tup,NAttempts) when is_atom(Tup) ->
    case simplify_member(Tup, TupDom, random:uniform(len(Tup)+1)) of
	Tup -> simplify_atom(TupDom, Tup, NAttempts-1);
	NewTup -> NewTup
    end.

%%
%% the meat of the shrinking function
%%
simplify_internal(_, []) -> 
    [];

simplify_internal(_, {}) -> 
    {};

simplify_internal(_, '') -> 
    '';

simplify_internal(_, 0) -> 
    0;

simplify_internal(_, 0.0) -> 
    0.0;

simplify_internal(#?DOM{kind=any}=Dom, Val) when is_tuple(Val) ->
    list_to_tuple(simplify_internal(Dom,tuple_to_list(Val)));

simplify_internal(#?DOM{kind=any}=Dom, Val) when is_atom(Val) ->
    list_to_atom(simplify_internal(Dom,atom_to_list(Val)));



simplify_internal(TupDom,Tup) when is_tuple(TupDom), is_tuple(Tup) ->
    %% try to simplify it 10 times...
    simplify_tuple(TupDom, Tup, 10);

simplify_internal(ListDom,List) when is_list(ListDom), is_list(List) ->
    %% try to simplify it 10 times...
    simplify_list(ListDom, List, 10);


%%
%% aggregates (tuple or list) are simplified either by 
%% simplifying one of the elements, or ... by removing
%% one of the elements
%%
simplify_internal(AggrDom,Aggr) when is_list(Aggr); is_tuple(Aggr)->
    NewAggr = 
    case random:uniform(2) of
	1 -> remove_any(Aggr);
	2 -> simplify_member(Aggr, AggrDom, random:uniform(len(Aggr)+1))
    end,

    %% if the above did not change anything; 
    %% or the change was illegal according to AggrDom ...
    case NewAggr of 
	Aggr -> case random:uniform(2) of
{'fraction_non_alphanumeric': 0.14831019915509958, 'fraction_numerical': 0.012070006035003017, 'mean_word_length': 4.223798266351458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1838496', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2360, 'n_words': 578}