[m-rev.] for review: add comprehensive string__format tests

Peter Ross peter.ross at miscrit.be
Tue Nov 12 06:53:42 AEDT 2002


Hi,

The full diff for this file can be found in my home directory: zzreview.format


===================================================================


Estimated hours taken: 3
Branches: main

Fully test the string__format predicate by automatically generating a
comprehensive set of format strings.

tests/general/Mmakefile:
	Add the new test cases.

tests/general/string_format_lib.m:
	Predicate to generate all the interesting format strings for a
	particular type specifier.  We avoid generating format strings
	which are implementation dependent.

tests/general/string_format_c.exp:
tests/general/string_format_c.m:
tests/general/string_format_d.exp:
tests/general/string_format_d.m:
tests/general/string_format_e.exp:
tests/general/string_format_e.m:
tests/general/string_format_f.exp:
tests/general/string_format_f.m:
tests/general/string_format_g.exp:
tests/general/string_format_g.m:
tests/general/string_format_o.exp:
tests/general/string_format_o.m:
tests/general/string_format_p.exp:
tests/general/string_format_p.m:
tests/general/string_format_s.exp:
tests/general/string_format_s.m:
tests/general/string_format_u.exp:
tests/general/string_format_u.m:
tests/general/string_format_x.exp:
tests/general/string_format_x.m:
	Test the various type specifiers.

Index: tests/general/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/general/Mmakefile,v
retrieving revision 1.43
diff -u -r1.43 Mmakefile
--- tests/general/Mmakefile	29 Oct 2002 07:10:57 -0000	1.43
+++ tests/general/Mmakefile	11 Nov 2002 19:45:21 -0000
@@ -55,6 +55,16 @@
 		state_vars_typeclasses \
 		string_foldl_substring \
 		string_foldr_substring \
+		string_format_c \
+		string_format_d \
+		string_format_e \
+		string_format_f \
+		string_format_g \
+		string_format_o \
+		string_format_p \
+		string_format_s \
+		string_format_u \
+		string_format_x \
 		string_format_test \
 		string_format_test_2 \
 		string_format_test_3 \
Index: tests/general/string_format_c.exp
===================================================================
RCS file: tests/general/string_format_c.exp
diff -N tests/general/string_format_c.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_c.exp	11 Nov 2002 19:45:21 -0000
@@ -0,0 +1,40 @@
+    % +-0c:a
+    % +-1c:a
+    % +-2c:a 
+    % +-5c:a    
+     % +-c:a
+     % +0c:a
+     % +1c:a
+     % +2c: a
+     % +5c:    a
+      % +c:a
+     % -0c:a
+     % -1c:a
+     % -2c:a 
+     % -5c:a    
+      % -c:a
+      % 0c:a
+      % 1c:a
+      % 2c: a
+      % 5c:    a
+       % c:a
+     %+-0c:a
+     %+-1c:a
+     %+-2c:a 
+     %+-5c:a    
+      %+-c:a
+      %+0c:a
+      %+1c:a
+      %+2c: a
+      %+5c:    a
+       %+c:a
+      %-0c:a
+      %-1c:a
+      %-2c:a 
+      %-5c:a    
+       %-c:a
+       %0c:a
+       %1c:a
+       %2c: a
+       %5c:    a
+        %c:a
Index: tests/general/string_format_c.m
===================================================================
RCS file: tests/general/string_format_c.m
diff -N tests/general/string_format_c.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_c.m	11 Nov 2002 19:45:21 -0000
@@ -0,0 +1,30 @@
+%------------------------------------------------------------------------------%
+% string_format_c.m
+%
+% Test the c specifier of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_c.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Chars = [c('a')] },
+	list__foldl(output_list(Chars), format_strings("c")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_d.m
===================================================================
RCS file: tests/general/string_format_d.m
diff -N tests/general/string_format_d.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_d.m	11 Nov 2002 19:45:22 -0000
@@ -0,0 +1,32 @@
+%------------------------------------------------------------------------------%
+% string_format_d.m
+%
+% Test the d,i specifiers of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_d.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module int, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Ints = [i(0), i(-1), i(1), i(10), i(-10),
+			i(100), i(-100), i(min_int), i(max_int)] },
+	list__foldl(output_list(Ints), format_strings("d")),
+	list__foldl(output_list(Ints), format_strings("i")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_e.m
===================================================================
RCS file: tests/general/string_format_e.m
diff -N tests/general/string_format_e.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_e.m	11 Nov 2002 19:45:24 -0000
@@ -0,0 +1,32 @@
+%------------------------------------------------------------------------------%
+% string_format_e.m
+%
+% Test the e,E specifiers of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_e.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module float, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Floats = [f(0.0), f(-1.0), f(1.0), f(10.0), f(-10.0),
+			f(100.0), f(-100.0), f(min), f(max)] },
+	list__foldl(output_list(Floats), format_strings("e")),
+	list__foldl(output_list(Floats), format_strings("E")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_f.m
===================================================================
RCS file: tests/general/string_format_f.m
diff -N tests/general/string_format_f.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_f.m	11 Nov 2002 19:45:26 -0000
@@ -0,0 +1,31 @@
+%------------------------------------------------------------------------------%
+% string_format_f.m
+%
+% Test the f specifier of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_f.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module float, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Floats = [f(0.0), f(-1.0), f(1.0), f(10.0), f(-10.0),
+			f(100.0), f(-100.0), f(min), f(max)] },
+	list__foldl(output_list(Floats), format_strings("f")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_g.m
===================================================================
RCS file: tests/general/string_format_g.m
diff -N tests/general/string_format_g.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_g.m	11 Nov 2002 19:45:29 -0000
@@ -0,0 +1,32 @@
+%------------------------------------------------------------------------------%
+% string_format_g.m
+%
+% Test the g,G specifiers of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_g.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module float, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Floats = [f(0.0), f(-1.0), f(1.0), f(10.0), f(-10.0),
+			f(100.0), f(-100.0), f(min), f(max)] },
+	list__foldl(output_list(Floats), format_strings("g")),
+	list__foldl(output_list(Floats), format_strings("G")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_lib.m
===================================================================
RCS file: tests/general/string_format_lib.m
diff -N tests/general/string_format_lib.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_lib.m	11 Nov 2002 19:45:29 -0000
@@ -0,0 +1,112 @@
+:- module string_format_lib.
+
+:- interface.
+
+:- import_module io, list, string.
+
+	% Given a specifier return all possible format strings for that
+	% specifier.
+:- func format_strings(string) = list(string).
+
+	% Output each of the polytypes with the format string supplied.
+:- pred output_list(list(string__poly_type)::in, string::in,
+        io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module std_util.
+
+%-----------------------------------------------------------------------------%
+
+output_list(PolyTypes, FormatStr) -->
+	list__foldl(output_format(FormatStr), PolyTypes).
+
+:- pred output_format(string::in, string__poly_type::in, io::di, io::uo) is det.
+
+output_format(FormatStr, PolyType) -->
+	io__format("%10s:" ++ FormatStr, [s(FormatStr), PolyType]),
+	io__nl.
+
+%-----------------------------------------------------------------------------%
+
+format_strings(Specifier) = FormatStrings :-
+	solutions(format_string(Specifier), FormatStrings).
+
+%-----------------------------------------------------------------------------%
+
+:- pred format_string(string::in, string::out) is nondet.
+
+format_string(Specifier, FormatStr) :-
+	flags_combinations(Specifier, Flags),
+	width_and_prec(Specifier, WidthAndPrec),
+	FormatStr = format_string(Flags, WidthAndPrec, Specifier).
+
+:- func format_string(list(string), pair(maybe(string)), string) = string.
+		
+format_string(Flags, Width - Prec, Specifier) = Str :-
+	FlagsStr = string__append_list(Flags),
+	( Width = yes(WidthStr) ->
+		Str0 = WidthStr
+	;
+		Str0 = ""
+	),
+	( Prec = yes(PrecStr) ->
+		Str1 = Str0 ++ "." ++ PrecStr ++ Specifier
+	;
+		Str1 = Str0 ++ Specifier
+	),
+	Str = "%" ++ FlagsStr ++ Str1.
+
+%-----------------------------------------------------------------------------%
+
+:- pred flags_combinations(string::in, list(string)::out) is multi.
+
+flags_combinations(Specifier, X) :-
+	all_combinations(flags(Specifier), X).
+
+:- func flags(string) = list(string).
+
+flags(Specifier) = Flags :-
+	Flags0 = ["-", "+", " "],
+	( member(Specifier, ["o", "x", "X", "e", "E", "f", "F", "g", "G"]) ->
+		Flags1 = ["#" | Flags0]
+	;
+		Flags1 = Flags0
+	),
+	( ( member(Specifier, ["d", "i", "u"]) ; Flags1 = ["#" | _] ) ->
+		Flags = ["0" | Flags1]
+	;
+		Flags = Flags1
+	).
+
+:- pred all_combinations(list(T)::in, list(T)::out) is multi.
+
+all_combinations(List, list__sort(List)).
+all_combinations(List, list__sort(Combination)) :-
+	list__delete(List, _, SubList),
+	all_combinations(SubList, Combination).
+
+:- pred width_and_prec(string::in, pair(maybe(string))::out) is nondet.
+
+width_and_prec(Specifier, Width - Prec) :-
+	maybe_num(Width),
+	maybe_num(Prec),
+	( Prec = yes(_) ->
+		member(Specifier, ["d", "i", "o", "u", "x", "X",
+				"e", "E", "f", "F", "g", "G"])
+	;
+		true
+	).
+
+:- pred maybe_num(maybe(string)::out) is multi.
+
+maybe_num(no).
+maybe_num(yes("0")).
+maybe_num(yes("1")).
+maybe_num(yes("2")).
+maybe_num(yes("5")).
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
Index: tests/general/string_format_o.m
===================================================================
RCS file: tests/general/string_format_o.m
diff -N tests/general/string_format_o.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_o.m	11 Nov 2002 19:45:30 -0000
@@ -0,0 +1,30 @@
+%------------------------------------------------------------------------------%
+% string_format_o.m
+%
+% Test the o specifier of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_o.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module int, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Ints = [i(0), i(1), i(10), i(100), i(max_int)] },
+	list__foldl(output_list(Ints), format_strings("o")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_p.m
===================================================================
RCS file: tests/general/string_format_p.m
diff -N tests/general/string_format_p.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_p.m	11 Nov 2002 19:45:30 -0000
@@ -0,0 +1,30 @@
+%------------------------------------------------------------------------------%
+% string_format_p.m
+%
+% Test the p specifier of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_p.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module int, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Ints = [i(0), i(1), i(10), i(100), i(max_int)] },
+	list__foldl(output_list(Ints), format_strings("p")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_s.exp
===================================================================
RCS file: tests/general/string_format_s.exp
diff -N tests/general/string_format_s.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_s.exp	11 Nov 2002 19:45:30 -0000
@@ -0,0 +1,200 @@
+    % +-0s:
+    % +-0s:a
+    % +-0s:ab
+    % +-0s:abc
+    % +-0s:abcdefghijklmno
+    % +-1s: 
+    % +-1s:a
+    % +-1s:ab
+    % +-1s:abc
+    % +-1s:abcdefghijklmno
+    % +-2s:  
+    % +-2s:a 
+    % +-2s:ab
+    % +-2s:abc
+    % +-2s:abcdefghijklmno
+    % +-5s:     
+    % +-5s:a    
+    % +-5s:ab   
+    % +-5s:abc  
+    % +-5s:abcdefghijklmno
+     % +-s:
+     % +-s:a
+     % +-s:ab
+     % +-s:abc
+     % +-s:abcdefghijklmno
+     % +0s:
+     % +0s:a
+     % +0s:ab
+     % +0s:abc
+     % +0s:abcdefghijklmno
+     % +1s: 
+     % +1s:a
+     % +1s:ab
+     % +1s:abc
+     % +1s:abcdefghijklmno
+     % +2s:  
+     % +2s: a
+     % +2s:ab
+     % +2s:abc
+     % +2s:abcdefghijklmno
+     % +5s:     
+     % +5s:    a
+     % +5s:   ab
+     % +5s:  abc
+     % +5s:abcdefghijklmno
+      % +s:
+      % +s:a
+      % +s:ab
+      % +s:abc
+      % +s:abcdefghijklmno
+     % -0s:
+     % -0s:a
+     % -0s:ab
+     % -0s:abc
+     % -0s:abcdefghijklmno
+     % -1s: 
+     % -1s:a
+     % -1s:ab
+     % -1s:abc
+     % -1s:abcdefghijklmno
+     % -2s:  
+     % -2s:a 
+     % -2s:ab
+     % -2s:abc
+     % -2s:abcdefghijklmno
+     % -5s:     
+     % -5s:a    
+     % -5s:ab   
+     % -5s:abc  
+     % -5s:abcdefghijklmno
+      % -s:
+      % -s:a
+      % -s:ab
+      % -s:abc
+      % -s:abcdefghijklmno
+      % 0s:
+      % 0s:a
+      % 0s:ab
+      % 0s:abc
+      % 0s:abcdefghijklmno
+      % 1s: 
+      % 1s:a
+      % 1s:ab
+      % 1s:abc
+      % 1s:abcdefghijklmno
+      % 2s:  
+      % 2s: a
+      % 2s:ab
+      % 2s:abc
+      % 2s:abcdefghijklmno
+      % 5s:     
+      % 5s:    a
+      % 5s:   ab
+      % 5s:  abc
+      % 5s:abcdefghijklmno
+       % s:
+       % s:a
+       % s:ab
+       % s:abc
+       % s:abcdefghijklmno
+     %+-0s:
+     %+-0s:a
+     %+-0s:ab
+     %+-0s:abc
+     %+-0s:abcdefghijklmno
+     %+-1s: 
+     %+-1s:a
+     %+-1s:ab
+     %+-1s:abc
+     %+-1s:abcdefghijklmno
+     %+-2s:  
+     %+-2s:a 
+     %+-2s:ab
+     %+-2s:abc
+     %+-2s:abcdefghijklmno
+     %+-5s:     
+     %+-5s:a    
+     %+-5s:ab   
+     %+-5s:abc  
+     %+-5s:abcdefghijklmno
+      %+-s:
+      %+-s:a
+      %+-s:ab
+      %+-s:abc
+      %+-s:abcdefghijklmno
+      %+0s:
+      %+0s:a
+      %+0s:ab
+      %+0s:abc
+      %+0s:abcdefghijklmno
+      %+1s: 
+      %+1s:a
+      %+1s:ab
+      %+1s:abc
+      %+1s:abcdefghijklmno
+      %+2s:  
+      %+2s: a
+      %+2s:ab
+      %+2s:abc
+      %+2s:abcdefghijklmno
+      %+5s:     
+      %+5s:    a
+      %+5s:   ab
+      %+5s:  abc
+      %+5s:abcdefghijklmno
+       %+s:
+       %+s:a
+       %+s:ab
+       %+s:abc
+       %+s:abcdefghijklmno
+      %-0s:
+      %-0s:a
+      %-0s:ab
+      %-0s:abc
+      %-0s:abcdefghijklmno
+      %-1s: 
+      %-1s:a
+      %-1s:ab
+      %-1s:abc
+      %-1s:abcdefghijklmno
+      %-2s:  
+      %-2s:a 
+      %-2s:ab
+      %-2s:abc
+      %-2s:abcdefghijklmno
+      %-5s:     
+      %-5s:a    
+      %-5s:ab   
+      %-5s:abc  
+      %-5s:abcdefghijklmno
+       %-s:
+       %-s:a
+       %-s:ab
+       %-s:abc
+       %-s:abcdefghijklmno
+       %0s:
+       %0s:a
+       %0s:ab
+       %0s:abc
+       %0s:abcdefghijklmno
+       %1s: 
+       %1s:a
+       %1s:ab
+       %1s:abc
+       %1s:abcdefghijklmno
+       %2s:  
+       %2s: a
+       %2s:ab
+       %2s:abc
+       %2s:abcdefghijklmno
+       %5s:     
+       %5s:    a
+       %5s:   ab
+       %5s:  abc
+       %5s:abcdefghijklmno
+        %s:
+        %s:a
+        %s:ab
+        %s:abc
+        %s:abcdefghijklmno
Index: tests/general/string_format_s.m
===================================================================
RCS file: tests/general/string_format_s.m
diff -N tests/general/string_format_s.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_s.m	11 Nov 2002 19:45:30 -0000
@@ -0,0 +1,30 @@
+%------------------------------------------------------------------------------%
+% string_format_s.m
+%
+% Test the s specifier of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_s.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Strings = [s(""), s("a"), s("ab"), s("abc"), s("abcdefghijklmno")] },
+	list__foldl(output_list(Strings), format_strings("s")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_u.m
===================================================================
RCS file: tests/general/string_format_u.m
diff -N tests/general/string_format_u.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_u.m	11 Nov 2002 19:45:30 -0000
@@ -0,0 +1,30 @@
+%------------------------------------------------------------------------------%
+% string_format_u.m
+%
+% Test the u specifier of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_u.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module int, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Ints = [i(0), i(1), i(10), i(100), i(max_int)] },
+	list__foldl(output_list(Ints), format_strings("u")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format_x.m
===================================================================
RCS file: tests/general/string_format_x.m
diff -N tests/general/string_format_x.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format_x.m	11 Nov 2002 19:45:32 -0000
@@ -0,0 +1,31 @@
+%------------------------------------------------------------------------------%
+% string_format_x.m
+%
+% Test the x,X specifiers of string__format.
+%------------------------------------------------------------------------------%
+
+:- module string_format_x.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module string_format_lib.
+:- import_module int, list, string.
+
+%------------------------------------------------------------------------------%
+
+main -->
+	{ Ints = [i(0), i(1), i(10), i(100), i(max_int)] },
+	list__foldl(output_list(Ints), format_strings("x")),
+	list__foldl(output_list(Ints), format_strings("X")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list