Commit 23253f0c authored by Carlos Galindo's avatar Carlos Galindo
Browse files

benchmark set without lambdas

parent d54e19b5
Loading
Loading
Loading
Loading
+151 −0
Original line number Diff line number Diff line
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
%-- bench1.erl
%--
%-- AUTHORS: 	 Anonymous
%-- DATE:        2016           
%-- PUBLISHED:   Software specially developed to test compression and expansion of
%-- 			 complex structures.
%--
%-- COPYLEFT:    This Erlang version is distributed in "Bencher: The Program Slicing 
%--              Benchmark Suite for Erlang" (Universitat Politècnica de València). 
%--              It is free of use and publicly available in the URL: 
%--              http://www.dsic.upv.es/~jsilva/slicing/bencher/
%--
%-- DESCRIPTION
%-- This benchmark consists in a program with a database of singers. It returns a set
%-- of information of each singer (Name,Age,Last Album Name, and Location and Year of 
%-- their next concert) by receiving a number between 1 and 6 as input.
%--
%-- SLICING CRITERION: (56,Year)
%-- SLICE: Available at: 
%--   http://www.dsic.upv.es/~jsilva/slicing/bencher/slices/b1_s56X.erl
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
-module(bench1).
-export([main/1]).

main(Number) when Number > 0 andalso Number < 7->
	Database = [
	["Rihanna",28,
	{albums,[{"Music of the Sun",2005},{"A Girl like me",2006},{"Good Girl Gone Bad",2007},{"Rater D",2009},{"Loud",2010},{"Talk That Talk",2011},{"Unapologetic",2012},{"ANTi",2016}]},
	{concerts,[{"Amsterdam",{2016,6,17}}, {"Manchester",{2016,6,29}}, {"Barcelona",{2016,7,21}}, {"Bucarest",{2017,8,14}}]}],
	["Christina Aguilera",35,
	{albums,[{"Christina Aguilera",1999},{"Mi Reflejo",2000},{"Stripped",2002},{"Back to Basics",2006},{"Bionic",2010},{"Lotus",2012}]},
	{concerts,[{"Tokio",{2007,6,21}},{"Abu Dabi",{2008,10,28}}]}],
	["Bruno Mars",30,
	{albums,[{"Doo-Wops & Hooligans",2010},{"Unorthodox Jukebox",2012}]},
	{concerts,[{"Santo Domingo",{2014,10,4}},{"Las Vegas",{2014,10,18}},{"Liverpool",{2013,11,24}}]}],
	["Daddy Yankee",39,
	{albums,[{"No Mercy",1995},{"El Cangri.Com",2002},{"Barrio Fino",2004},{"El Cartel: The Big Boss",2007},{"Mundial",2010},{"Prestige",2012},{"Cartel IV",2015}]},
	{concerts,[{"Mexico City",{2015,11,8}},{"Las Vegas",{2016,5,6}},{"New York",{2017,7,30}}]}],
	["Justin Bieber",22,
	{albums,[{"My World 2.0",2010},{"Under the mistletoe",2011},{"Believe",2012},{"Purpose",2015}]},
	{concerts,[{"Miami",{2016,7,3}},{"Munich",{2016,9,16}},{"Birmingham",{2017,10,24}}]}],
	["Adele",28,
	{albums,[{"19",2008},{"21",2011},{"25",2015}]},
	{concerts,[{"Lisboa",{2016,5,22}},{"Paris",{2016,6,10}},{"Oakland",{2016,8,2}},{"Toronto",{2017,10,6}}]}]
	],
	Artist = lists:nth(Number,Database),	
	ArtistName = getArtistName(Artist),	
	Age = getAge(Artist),
	LastAlbum = getLastAlbum(Artist),
	AlbumName = getAlbumName(LastAlbum),
	NextConcert = getNextConcert(Artist),
	Info = getConcertLocationAndYear(NextConcert),
	{Location,Year} = Info,		% Slice here
	{ArtistName,Age,AlbumName,Location,Year}.

getArtistName(Artist) ->
	[Name|_] = Artist,
	Name.

getAge(Artist) ->
	[_,Age|_] = Artist,
	Age.

getDiscography(Artist) ->
	[_,_,{albums,Discography}|_] = Artist,
	Discography.

getConcerts(Artist) ->
	[_,_,_,{concerts,Concerts}] = Artist,
	Concerts.

getLastAlbum(Artist) ->
	Albums = getDiscography(Artist),
	case getLast(Albums,empty) of 
		empty -> "No albums published";
		Album -> Album
	end.

getLast([],A) -> A;
getLast([Album|Albums],empty) ->
	getLast(Albums,Album);
getLast([Album|Albums],Newest) ->
	{_,YearN} = Newest,
	{_,YearA} = Album,
	case YearA > YearN of
		true -> getLast(Albums,Album);
		_ -> getLast(Albums,Newest)
	end.

getNextConcert(Artist) ->
	CurrentDate = {2017,3,28},
	Concerts = getConcerts(Artist),
	case getNext(Concerts,CurrentDate,empty) of
		empty -> "No future concerts planned";
		Concert -> Concert
	end.

getNext([],_,NextConcert) -> NextConcert;
getNext([Concert|Concerts],Current,empty) ->
	{YearA,MonthA,DayA} = Current,
	{_,Date}=Concert,
	{YearC,MonthC,DayC} = Date,
	case {YearA,YearC,MonthA,MonthC,DayA,DayC} of
		{YA,YC,_,_,_,_} when YC > YA -> getNext(Concerts,Current,Concert);
		{Y,Y,MA,MC,_,_} when MC > MA -> getNext(Concerts,Current,Concert);
		{Y,Y,M,M,DA,DC} when DC >= DA -> getNext(Concerts,Current,Concert);
		_ -> getNext(Concerts,Current,empty)
	end;
getNext([Concert|Concerts],Current,NextConcert) ->
	{YearA,MonthA,DayA} = Current,
	{_,DateN}=NextConcert,
	{YearN,MonthN,DayN} = DateN,
	{_,DateC}=Concert,
	{YearC,MonthC,DayC} = DateC,

	Next = case {YearA,YearC,MonthA,MonthC,DayA,DayC} of
		{YA,YC,_,_,_,_} when YC < YA -> getNext(Concerts,Current,NextConcert);
		{Y,Y,MA,MC,_,_} when MC < MA -> getNext(Concerts,Current,NextConcert);
		{Y,Y,M,M,DA,DC} when DC < DA -> getNext(Concerts,Current,NextConcert);
		_ -> empty
	end,
	case Next of
		empty ->
			case {YearN,YearC,MonthN,MonthC,DayN,DayC} of
				{YN,YC2,_,_,_,_} when YC2 < YN -> getNext(Concerts,Current,Concert);
				{Y2,Y2,MN,MC2,_,_} when MC2 < MN -> getNext(Concerts,Current,Concert);
				{Y2,Y2,M2,M2,DN,DC2} when DC2 < DN -> getNext(Concerts,Current,Concert);
				_ -> getNext(Concerts,Current,NextConcert)
			end;
		_ -> Next
	end.

getAlbumName(Album) -> 
	{Name,_} = Album,
	Name.

getStringDate(Concert) ->
	{_,Date} = Concert,
	{Y,M,D} = Date,
	integer_to_list(D)++"/"++integer_to_list(M)++"/"++integer_to_list(Y).

getConcertLocationAndYear("No future concerts planned") ->
	{"an undefined City", "Not planned yet"};
getConcertLocationAndYear(Concert) ->
	{Location,Date} = Concert,
	{Year,Month,Day} = Date,
	Info = {Location,Year},
	Info.
+55 −0
Original line number Diff line number Diff line
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
%-- bench10.erl
%--
%-- AUTHORS:     Tamarit
%-- DATE:        2016           
%-- PUBLISHED:   https://github.com/tamarit/hackerrank/tree/master/common-divisors (2016)
%--
%-- COPYLEFT:    This Erlang version is distributed in "Bencher: The Program Slicing 
%--              Benchmark Suite for Erlang" (Universitat Politècnica de València). 
%--              It is free of use and publicly available in the URL: 
%--              http://www.dsic.upv.es/~jsilva/slicing/bencher/
%--
%-- DESCRIPTION
%-- The program receives a list with pairs of integers and compute the number of common 
%-- divisors of each pair. The program returns a list with the number of common divisors
%-- in the same order as the input pairs.
%--
%-- SLICING CRITERION: (34,DB)
%-- SLICE: Available at: 
%--   http://www.dsic.upv.es/~jsilva/slicing/bencher/slices/b10_s34DB.erl
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
-module(bench10).
-export([main/1]).
main(L) ->
  Res = calculate(L).

calculate(List) ->
    calculate(List, []).

calculate([{A,B}|T], Acc) ->
    DA = divs(A),
    DB = divs(B),       % Slice here
    calculate(T, 
        [sets:size(
            sets:intersection(
                sets:from_list(DA), 
                sets:from_list(DB)))
        | Acc]);
calculate([], Acc) ->
    lists:reverse(Acc).

divs(0) -> [];
divs(1) -> [1];
divs(N) -> [1, N] ++ divisors(2,N,math:sqrt(N)).
 
divisors(K,_N,Q) when K > Q -> [];
divisors(K,N,Q) when N rem K =/= 0 -> 
    divisors(K+1,N,Q);
divisors(K,N,Q) when K * K  == N -> 
    [K] ++ divisors(K+1,N,Q);
divisors(K,N,Q) ->
    [K, N div K] ++ divisors(K+1,N,Q).
    
 No newline at end of file
+38 −0
Original line number Diff line number Diff line
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
%-- bench11.erl
%--
%-- AUTHORS: 	 Anonymous
%-- DATE:        2016           
%-- PUBLISHED:   Software specially developed to test non-called funtions.
%--
%-- COPYLEFT:    This Erlang version is distributed in "Bencher: The Program Slicing 
%--              Benchmark Suite for Erlang" (Universitat Politècnica de València). 
%--              It is free of use and publicly available in the URL: 
%--              http://www.dsic.upv.es/~jsilva/slicing/bencher/
%--
%-- DESCRIPTION
%-- This benchmark consists in a function that receives two lists as inputs and calls
%-- another one named fl with a conditional structure. There is also another function 
%-- gl that is not called by lists or fl.
%--
%-- SLICING CRITERION: (28,C)
%-- SLICE: Available at: 
%--   http://www.dsic.upv.es/~jsilva/slicing/bencher/slices/b11_s28C.erl
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
-module(bench11).
-export([lists/2]).

lists(A,B) -> 
	C=fl(A,B).		% Slice here
fl([H1|T1],[H2|T2]) -> 
	if	
		H1 >= 3 -> 
			H2;
		true -> 
			[H|_]=T2,
			H1-H  
	end.
gl([H|T]) -> 
	1.
 No newline at end of file
+116 −0
Original line number Diff line number Diff line
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
%-- bench12.erl
%--
%-- AUTHORS: 	 Tamarit-Emartinm
%-- DATE:        2013           
%-- PUBLISHED:   https://github.com/tamarit/edd/blob/master/examples/ternary
%--
%-- COPYLEFT:    This Erlang version is distributed in "Bencher: The Program Slicing 
%--              Benchmark Suite for Erlang" (Universitat Politècnica de València). 
%--              It is free of use and publicly available in the URL: 
%--              http://www.dsic.upv.es/~jsilva/slicing/bencher/
%--
%-- DESCRIPTION
%-- The program performs translations and operations with balanced ternary. Its inputs are
%-- two numbers in balanced ternary representation and a decimal number. It converts the 
%-- three numbers to the oposite representation and performs an operation with their 
%-- balanced ternary representation. The output is a tuple that contains the input numbers
%-- and the result in both representations.
%--
%-- SLICING CRITERION: (40,BS)
%-- SLICE: Available at: 
%--   http://www.dsic.upv.es/~jsilva/slicing/bencher/slices/b12_s40BS.erl
%--
%-- SLICING CRITERION: (92,A)
%-- SLICE: Available at: 
%--   http://www.dsic.upv.es/~jsilva/slicing/bencher/slices/b12_s92A.erl
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
-module(bench12).
-export([main/3]).

main(AS,B,CS) ->
    AT = from_string(AS), A = from_ternary(AT),	
    BT = to_ternary(B), BS = to_string(BT),     
    CT = from_string(CS), C = from_ternary(CT),
    RT = mul(AT,sub(BT,CT)),
    R = from_ternary(RT),
    RS = to_string(RT),
    [{AS,A},{BS,B},{CS,C},{RS,R}].      % Slice here

to_string(T) -> [to_char(X) || X <- T].
 
from_string(S) -> [from_char(X) || X <- S].
 
to_char(-1) -> $-;
to_char(0) -> $0;
to_char(1) -> $+.
 
from_char($-) -> -1;
from_char($0) -> 0;
from_char($+) -> 1.
 
to_ternary(N) when N > 0 ->
    to_ternary(N,[]);
to_ternary(N) ->
    neg(to_ternary(-N)).
 
to_ternary(0,Acc) ->
    Acc;
to_ternary(N,Acc) when N rem 3 == 0 ->
    to_ternary(N div 3, [0|Acc]);
to_ternary(N,Acc) when N rem 3 == 1 ->
    to_ternary(N div 3, [1|Acc]);
to_ternary(N,Acc) ->
    X = to_ternary((N+1) div 3, [-1|Acc]).
 
from_ternary(T) -> from_ternary(T,0).
 
from_ternary([],Acc) ->
    Acc;
from_ternary([H|T],Acc) ->
    from_ternary(T,Acc*3 + H).
 
mul(A,B) -> mul(B,A,[]).
 
mul(_,[],Acc) ->
    Acc;
mul(B,[A|As],Acc) ->
    BP = case A of
             -1 -> neg(B);
             0 ->  [0];
             1 ->  B
         end,
    A1 = Acc++[0],
    A2=add(BP,A1),
    mul(B,As,A2).
 
neg(T) -> [ -H || H <- T].
 
sub(A,B) -> 
	add(A,neg(B)).     % Slice here

add(A,B) when length(A) < length(B) ->
    add(lists:duplicate(length(B)-length(A),0)++A,B);
add(A,B) when length(A) > length(B) ->
   add(B,A);
add(A,B) ->
    add(lists:reverse(A),lists:reverse(B),0,[]).
 
add([],[],0,Acc) ->
    Acc;
add([],[],C,Acc) ->
    [C|Acc];
add([A|As],[B|Bs],C,Acc) ->
    [C1,D] = add_util(A+B+C),
    add(As,Bs,C1,[D|Acc]).
 
add_util(-3) -> [-1,0];
add_util(-2) -> [-1,1];
add_util(-1) -> [0,-1];
add_util(3) -> [1,0];
add_util(2) -> [1,-1];
add_util(1) -> [0,1];
add_util(0) -> [0,0].
 
+46 −0
Original line number Diff line number Diff line
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
%-- bench13.erl
%--
%-- AUTHORS:     Susan Horwitz
%-- DATE:        1990         
%-- PUBLISHED:   Interprocedural slicing using dependence graphs
%--              ACM Transactions on Programming Languages and Systems
%--              Volume 12 Issue 1, Jan. 1990 Pages 26-60 
%--
%-- COPYLEFT:    This Erlang version is distributed in "Bencher: The Program Slicing 
%--              Benchmark Suite for Erlang" (Universitat Politècnica de València). 
%--              It is free of use and publicly available in the URL: 
%--              http://www.dsic.upv.es/~jsilva/slicing/bencher/
%--
%-- DESCRIPTION
%-- The program proposed by Susan Horwitz was designed to ilustrate the problem of the 
%-- Weiser's slicing algorithm in interprocedural slicing. The program makes a recursive 
%-- call to add up the first 11 naturals.
%-- 
%-- SLICING CRITERION: (38,NewI)
%-- SLICE: Available at: 
%--   http://www.dsic.upv.es/~jsilva/slicing/bencher/slices/b13_s38NewI.erl
%-----------------------------------------------------------------------------------------
%-----------------------------------------------------------------------------------------
-module(bench13).
-export([main/0]).

main() -> 
  Sum = 0,
  I = 1,
  {Result,_} = while(Sum,I,11),
  Result.

while(S,I,Top) ->
  if Top /= 0 ->
        NewS = add(S,I),
        NewI = increment(I),      % Slice here
        while(NewS,NewI,Top-1);
      Top == 0 ->
        {S,I}
  end.

add(A,B) -> A+B.
increment(A) -> add(A,1).
Loading