[m-rev.] for review: move string__format tests

Peter Ross pro at missioncriticalit.com
Tue Nov 19 03:26:20 AEDT 2002


Hi,

Note the full diff is in ~petdr/zzreview.string_format

I have also investigate the -(max+max) and why it comes out as NaN
and on my machine it happens in the C code

	result = 0.000000000000 - (max+max)

so if it's a bug, then it is a bug in the C compiler.

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


Estimated hours taken: 4
Branches: main

tests/general/Mmakefile:
	Move the string__format tests to the string_format directory.
	
tests/general/string_format/Mercury.options:
tests/general/string_format/Mmakefile:
	Only output the last 20 lines of output if a program aborts as
	the output can be very big and it is only the last few lines
	that we are interested in.
	If the acutal output doesn't match the expect output, don't
	output the diff to stdout because the files are generally too
	big.
	Sort the output files before comparing it because the C and IL
	backends use a different lexical ordering.

tests/general/string_format_p.exp:
tests/general/string_format_p.m:
	Remove completely from the test suite as how to print a
	pointer is implementation defined and we always get different
	results.


tests/general/string_format/string_format_c.exp:
tests/general/string_format/string_format_c.m:
tests/general/string_format/string_format_d.exp:
tests/general/string_format/string_format_d.m:
tests/general/string_format/string_format_e.exp:
tests/general/string_format/string_format_e.m:
tests/general/string_format/string_format_f.exp:
tests/general/string_format/string_format_f.m:
tests/general/string_format/string_format_g.exp:
tests/general/string_format/string_format_g.m:
tests/general/string_format/string_format_lib.m:
tests/general/string_format/string_format_o.exp:
tests/general/string_format/string_format_o.m:
tests/general/string_format/string_format_s.exp:
tests/general/string_format/string_format_s.m:
tests/general/string_format/string_format_u.exp:
tests/general/string_format/string_format_u.m:
tests/general/string_format/string_format_x.exp:
tests/general/string_format/string_format_x.m:
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_lib.m:
tests/general/string_format_o.exp:
tests/general/string_format_o.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:
	Move the files and refactor the code a bit.

Index: tests/general/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/general/Mmakefile,v
retrieving revision 1.45
diff -u -r1.45 Mmakefile
--- tests/general/Mmakefile	14 Nov 2002 09:46:29 -0000	1.45
+++ tests/general/Mmakefile	18 Nov 2002 16:01:39 -0000
@@ -69,18 +69,6 @@
 		map_corresponding \
 		unused_arg_determinism
 
-STRING_FORMAT_PROGS = \
-		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 
-
 ifneq "$(findstring profdeep,$(GRADE))" ""
 	PROGS = $(ORDINARY_PROGS)
 else
@@ -91,7 +79,7 @@
 # (i.e. tabling) and tabling is not yet implemented for non-gc grades.
 NOT_WORKING =	dnf
 
-SUBDIRS = accumulator
+SUBDIRS = accumulator string_format
 TESTS=$(PROGS)
 TESTS_DIR=..
 include $(TESTS_DIR)/Mmake.common
Index: tests/general/string_format_c.m
===================================================================
RCS file: tests/general/string_format_c.m
diff -N tests/general/string_format_c.m
--- tests/general/string_format_c.m	12 Nov 2002 15:20:41 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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'), c(' ')] },
-	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
--- tests/general/string_format_d.m	12 Nov 2002 15:20:42 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,32 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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
--- tests/general/string_format_e.m	12 Nov 2002 15:20:44 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,36 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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(100000000000000000.0), f(-100000000000000000.0),
-			f(55.5555555555), f(-55.5555555555), 
-			f(min), f(-min), f(max), f(-max),
-			f(min / 2.0), f(-min / 2.0), f(max*max), f(-max*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
--- tests/general/string_format_f.m	12 Nov 2002 15:20:46 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,35 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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(100000000000000000.0), f(-100000000000000000.0),
-			f(55.5555555555), f(-55.5555555555), 
-			f(min), f(-min), f(max), f(-max),
-			f(min / 2.0), f(-min / 2.0), f(max*max), f(-max*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
--- tests/general/string_format_g.m	12 Nov 2002 15:20:47 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,36 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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(100000000000000000.0), f(-100000000000000000.0),
-			f(55.5555555555), f(-55.5555555555), 
-			f(min), f(-min), f(max), f(-max),
-			f(min / 2.0), f(-min / 2.0), f(max*max), f(-max*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
--- tests/general/string_format_lib.m	13 Nov 2002 09:02:27 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,112 +0,0 @@
-:- 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
--- tests/general/string_format_o.m	12 Nov 2002 15:20:48 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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
--- tests/general/string_format_p.m	12 Nov 2002 15:20:48 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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.m
===================================================================
RCS file: tests/general/string_format_s.m
diff -N tests/general/string_format_s.m
--- tests/general/string_format_s.m	12 Nov 2002 15:20:48 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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
--- tests/general/string_format_u.m	12 Nov 2002 15:20:49 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,30 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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
--- tests/general/string_format_x.m	12 Nov 2002 15:20:49 -0000	1.1
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,31 +0,0 @@
-%------------------------------------------------------------------------------%
-% 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")).
-
-%------------------------------------------------------------------------------%
-%------------------------------------------------------------------------------%
Index: tests/general/string_format/Mercury.options
===================================================================
RCS file: tests/general/string_format/Mercury.options
diff -N tests/general/string_format/Mercury.options
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/Mercury.options	18 Nov 2002 16:01:39 -0000
@@ -0,0 +1,2 @@
+MCFLAGS=--no-line-numbers
+MS_ILASMFLAGS=/debug
Index: tests/general/string_format/Mmakefile
===================================================================
RCS file: tests/general/string_format/Mmakefile
diff -N tests/general/string_format/Mmakefile
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/Mmakefile	18 Nov 2002 16:01:39 -0000
@@ -0,0 +1,88 @@
+#-----------------------------------------------------------------------------#
+
+THIS_DIR = general/string_format
+
+#-----------------------------------------------------------------------------#
+
+# Any program added here should also be added to the `.cvsignore' file.
+
+PROGS=	\
+		string_format_c \
+		string_format_d \
+		string_format_e \
+		string_format_f \
+		string_format_g \
+		string_format_o \
+		string_format_s \
+		string_format_u \
+		string_format_x 
+
+SUBDIRS = 
+TESTS=$(PROGS)
+TESTS_DIR=../..
+include $(TESTS_DIR)/Mmake.common
+
+# Module-specific options should go in Mercury.options so they
+# can be found by `mmc --make'.
+include Mercury.options
+
+%.runtest: %.res ;
+
+#
+# This is adapted from the rule in ../../Mmake.common.  The
+# difference is that we only display the last 20 lines of the output
+# file if the file fails, due to the size of the output file.
+#
+%.out: %
+	{ [ -f $*.inp ] && cat $*.inp; } | ./$< > $@ 2>&1 || \
+		{ grep . $@ /dev/null | tail -n 20; exit 1; }
+
+#
+# This is adapted from the rule in ../../Mmake.common.  The only
+# differences are that we don't cat the difference to stdout, as
+# generally it is quite big and that we sort the output before the
+# comparison, as the IL and C backends use a different ordering for
+# lexical comparisons.
+#
+%.res: %.exp %.out
+	@echo "Comparing $*.sorted_out with $*.exp*,"
+	@echo "	results in $@"
+	@-rm -f $@ $*.res[1-5]
+	@cat $*.out | sort > $*.sorted_out
+	@{ diff $(DIFF_OPTS) $*.exp $*.sorted_out > $*.res1 && \
+		echo "Matched $*.exp" && \
+		cp $*.res1 $@; } || \
+	{ test -f $*.exp2 && \
+		diff $(DIFF_OPTS) $*.exp2 $*.sorted_out > $*.res2 && \
+		echo "Matched $*.exp2" && \
+		cp $*.res2 $@; } || \
+	{ test -f $*.exp3 && \
+		diff $(DIFF_OPTS) $*.exp3 $*.sorted_out > $*.res3 && \
+		echo "Matched $*.exp3" && \
+		cp $*.res3 $@; } || \
+	{ test -f $*.exp4 && \
+		diff $(DIFF_OPTS) $*.exp4 $*.sorted_out > $*.res4 && \
+		echo "Matched $*.exp4" && \
+		cp $*.res4 $@; } || \
+	{ test -f $*.exp5 && \
+		diff $(DIFF_OPTS) $*.exp5 $*.sorted_out > $*.res5 && \
+		echo "Matched $*.exp5" && \
+		cp $*.res5 $@; } || \
+	{ shortest=`wc -l $*.res[1-5] | grep -v total | sort -n | \
+		head -1 | awk '{ print $$2; }' `; \
+		echo "** $*.sorted_out did not match the expected output"; \
+		echo "** (closest match was $$shortest)"; \
+		echo "** The diff is located in $@"; \
+		cp $$shortest $@; \
+		exit 1; }
+
+#-----------------------------------------------------------------------------#
+
+realclean_local: clean_sorted_outs
+clean_local: clean_sorted_outs
+
+clean_sorted_outs:
+	rm -f *.sorted_out
+
+#-----------------------------------------------------------------------------#
+#-----------------------------------------------------------------------------#
Index: tests/general/string_format/string_format_c.m
===================================================================
RCS file: tests/general/string_format/string_format_c.m
diff -N tests/general/string_format/string_format_c.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_c.m	18 Nov 2002 16:01:39 -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'), c(' ')] },
+	list__foldl(output_list(Chars), format_strings("c")).
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format/string_format_d.m
===================================================================
RCS file: tests/general/string_format/string_format_d.m
diff -N tests/general/string_format/string_format_d.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_d.m	18 Nov 2002 16:01:40 -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/string_format_e.m
===================================================================
RCS file: tests/general/string_format/string_format_e.m
diff -N tests/general/string_format/string_format_e.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_e.m	18 Nov 2002 16:01:43 -0000
@@ -0,0 +1,45 @@
+%------------------------------------------------------------------------------%
+% 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 -->
+	{ FormatStrs_e = format_strings("e") },
+	{ FormatStrs_E = format_strings("E") },
+
+	list__foldl(output_list(standard_floats), FormatStrs_e),
+	list__foldl(output_list(trailing_zero_floats), FormatStrs_e),
+	list__foldl(output_list(rounding_floats), FormatStrs_e),
+	list__foldl(output_list(extreme_floats), FormatStrs_e),
+	list__foldl(output_list(denormal_floats), FormatStrs_e),
+	list__foldl(output_list(infinite_floats), FormatStrs_e),
+
+	list__foldl(output_list(standard_floats), FormatStrs_E),
+	list__foldl(output_list(trailing_zero_floats), FormatStrs_E),
+	list__foldl(output_list(rounding_floats), FormatStrs_E),
+	list__foldl(output_list(extreme_floats), FormatStrs_E),
+	list__foldl(output_list(denormal_floats), FormatStrs_E),
+	list__foldl(output_list(infinite_floats), FormatStrs_E),
+	[].
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format/string_format_f.m
===================================================================
RCS file: tests/general/string_format/string_format_f.m
diff -N tests/general/string_format/string_format_f.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_f.m	18 Nov 2002 16:01:45 -0000
@@ -0,0 +1,35 @@
+%------------------------------------------------------------------------------%
+% 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 -->
+	list__foldl(output_list(standard_floats), format_strings("f")),
+	list__foldl(output_list(trailing_zero_floats), format_strings("f")),
+	list__foldl(output_list(rounding_floats), format_strings("f")),
+	list__foldl(output_list(extreme_floats), format_strings("f")),
+	list__foldl(output_list(denormal_floats), format_strings("f")),
+	list__foldl(output_list(infinite_floats), format_strings("f")),
+	[].
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format/string_format_g.m
===================================================================
RCS file: tests/general/string_format/string_format_g.m
diff -N tests/general/string_format/string_format_g.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_g.m	18 Nov 2002 16:01:49 -0000
@@ -0,0 +1,42 @@
+%------------------------------------------------------------------------------%
+% 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 -->
+	list__foldl(output_list(standard_floats), format_strings("g")),
+	list__foldl(output_list(trailing_zero_floats), format_strings("g")),
+	list__foldl(output_list(rounding_floats), format_strings("g")),
+	list__foldl(output_list(extreme_floats), format_strings("g")),
+	list__foldl(output_list(denormal_floats), format_strings("g")),
+	list__foldl(output_list(infinite_floats), format_strings("g")),
+
+	list__foldl(output_list(standard_floats), format_strings("G")),
+	list__foldl(output_list(trailing_zero_floats), format_strings("G")),
+	list__foldl(output_list(rounding_floats), format_strings("G")),
+	list__foldl(output_list(extreme_floats), format_strings("G")),
+	list__foldl(output_list(denormal_floats), format_strings("G")),
+	list__foldl(output_list(infinite_floats), format_strings("G")),
+	[].
+
+%------------------------------------------------------------------------------%
+%------------------------------------------------------------------------------%
Index: tests/general/string_format/string_format_lib.m
===================================================================
RCS file: tests/general/string_format/string_format_lib.m
diff -N tests/general/string_format/string_format_lib.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_lib.m	18 Nov 2002 16:01:49 -0000
@@ -0,0 +1,134 @@
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+:- 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.
+
+
+:- func standard_floats = list(string__poly_type).
+:- func trailing_zero_floats = list(string__poly_type).
+:- func rounding_floats = list(string__poly_type).
+:- func extreme_floats = list(string__poly_type).
+:- func denormal_floats = list(string__poly_type).
+:- func infinite_floats = list(string__poly_type).
+
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module float, std_util.
+
+%-----------------------------------------------------------------------------%
+
+standard_floats = [f(0.0), f(1.0), f(-1.0), f(10.0),
+				f(-10.0), f(100.0), f(-100.0)].
+trailing_zero_floats = [f(100000000000000000.0), f(-100000000000000000.0)].
+rounding_floats = [f(55.555555555), f(-55.555555555)].
+extreme_floats	= [f(-max), f(-min), f(min), f(max)].
+denormal_floats	= [f(min / 2.0), f(-min / 2.0)].
+infinite_floats = [f(-(max+max)), f(max+max)].
+
+%-----------------------------------------------------------------------------%
+
+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).
+all_combinations(List, 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/string_format_o.m
===================================================================
RCS file: tests/general/string_format/string_format_o.m
diff -N tests/general/string_format/string_format_o.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_o.m	18 Nov 2002 16:01:49 -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/string_format_s.m
===================================================================
RCS file: tests/general/string_format/string_format_s.m
diff -N tests/general/string_format/string_format_s.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_s.m	18 Nov 2002 16:01:49 -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/string_format_u.m
===================================================================
RCS file: tests/general/string_format/string_format_u.m
diff -N tests/general/string_format/string_format_u.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_u.m	18 Nov 2002 16:01:50 -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/string_format_x.m
===================================================================
RCS file: tests/general/string_format/string_format_x.m
diff -N tests/general/string_format/string_format_x.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/general/string_format/string_format_x.m	18 Nov 2002 16:01:51 -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