### ----------------------------------------------------
### -- Format::Lisp
### -- Licenses: Artistic-2.0
### -- Authors: Jeffrey Goff
### -- File: t/18-format-f.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
 fn x)
	#           unless (and (string= s "     100.0") (string= s s2))
	#           collect (list x s s2)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~10,1,,f} );
		my @collected;
		for 100, 100.0 -> $x {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.25};
	)

	#`(
	# (deftest format.f.27
	#   (let ((fn (formatter "~10,1,,,'*F")))
	#     (loop for x in (remove-duplicates
	#                     '(100 100.0s0 100.0f0 100.0d0 100.0l0))
	#           for s = (format nil "~10,1,,,'*f" x)
	#           for s2 = (formatter-call-to-string fn x)
	#           unless (and (string= s "*****100.0") (string= s s2))
	#           collect (list x s s2)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~10,1,,,'*F} );
		my @collected;
		for 100, 100.0 -> $x {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.27};
	)
}, Q{padchar};

subtest {
	1;
	#`(
	# (deftest format.f.28
	#   (let ((fn (formatter "~VF")))
	#     (loop for x = (random 100.0)
	#           for s1 = (format nil "~f" x)
	#           for s2 = (format nil "~vf" nil x)
	#           for s3 = (formatter-call-to-string fn nil x)
	#           repeat 100
	#           unless (and (string= s1 s2) (string= s2 s3))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~VF} );
		my @collected;
		for 1 .. 100 {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.28};
	)

	#`(
	# (deftest format.f.29
	#   (let ((fn (formatter "~,vf")))
	#     (loop for x = (random 100.0)
	#           for s1 = (format nil "~f" x)
	#           for s2 = (format nil "~,vf" nil x)
	#           for s3 = (formatter-call-to-string fn nil x)
	#           repeat 100
	#           unless (and (string= s1 s2) (string= s2 s3))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~,vf} );
		my @collected;
		for 1 .. 100 {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.29};
	)

	#`(
	# (deftest format.f.30
	#   (let ((fn (formatter "~,,Vf")))
	#     (loop for x = (random 100.0)
	#           for s1 = (format nil "~f" x)
	#           for s2 = (format nil "~,,vf" nil x)
	#           for s3 = (formatter-call-to-string fn nil x)
	#           repeat 100
	#           unless (and (string= s1 s2) (string= s2 s3))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~,,Vf} );
		my @collected;
		for 1 .. 100 {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.30};
	)

	#`(
	# (deftest format.f.31
	#   (let ((fn (formatter "~,,,vF")))
	#     (loop for x = (random 100.0)
	#           for s1 = (format nil "~f" x)
	#           for s2 = (format nil "~,,,vf" nil x)
	#           for s3 = (formatter-call-to-string fn nil x)
	#           repeat 100
	#           unless (and (string= s1 s2) (string= s2 s3))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~,,,vF} );
		my @collected;
		for 1 .. 100 {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.31};
	)

	#`(
	# (deftest format.f.32
	#   (let ((fn (formatter "~,,,,VF")))
	#     (loop for x = (random 100.0)
	#           for s1 = (format nil "~f" x)
	#           for s2 = (format nil "~,,,,vf" nil x)
	#           for s3 = (formatter-call-to-string fn nil x)
	#           repeat 100
	#           unless (and (string= s1 s2) (string= s2 s3))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~,,,,VF} );
		my @collected;
		for 1 .. 100 {
	#		my $s1 = $c;
	#		my $s2 = $*fl.format( Q{~a}, $s1 );
	#		my $s3 = formatter-call-to-string( $fn, $s1 );
	#		unless $s1 eq $s1 and $s2 eq $s3 {
	#			@collected.append( [ $c, $s1, $s2, $s3 ] );
	#		}
		}
		@collected;
	}, [ ]
	), Q{format.f.32};
	)
}, Q{v parameters};

# ;;; Randomized tests
# 
# #|
# (deftest format.f.33
#   (let ((bound (if (> 10000000 most-positive-short-float)
#                    most-positive-short-float
#                  (coerce 10000000 'short-float))))
#     (loop for d = (random 10)
#           for w = (+ 1 d (random 10))
#           for x = (random bound)
#           for xr = (rational x)
#           for s = (format nil "~v,vf" w d x)
#           for sr = (decode-fixed-decimal-string s)
#           for eps = (expt 1/10 d)
#           for abs-xr-sr = (abs (- xr sr))
#           for abs-xr-sr-hi = (abs (- xr (+ sr eps)))
#           for abs-xr-sr-lo = (abs (- xr (- sr eps)))
#           repeat 100
#           unless (and (<= abs-xr-sr abs-xr-sr-hi)
#                       (<= abs-xr-sr abs-xr-sr-lo))
#           collect (list d w x xr s sr eps abs-xr-sr abs-xr-sr-hi abs-xr-sr-lo)))
#   nil)
#
# XXX Don't think it's applicable?

# |#
# 

#`(
# (deftest format.f.34
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'short-float))
#      (loop for i from (- 1 (ash 1 13)) below (ash 1 13)
#            for sf = (coerce i 'short-float)
#            for s = (format nil "~f" sf)
#            for i2 = (floor (read-from-string s))
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for (1 >> 13) - 1 .. (1 >> 13) -> $i {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.34};
)

#`(
# (deftest format.f.35
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'single-float))
#      (loop for i = (- (random (1- (ash 1 25))) -1 (ash 1 24))
#            for sf = (coerce i 'single-float)
#            for s = (format nil "~f" sf)
#            for i2 = (floor (read-from-string s))
#            repeat 2000
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.35};
)

#`(
# (deftest format.f.36
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'double-float))
#      (loop for i = (- (random (1- (ash 1 51))) -1 (ash 1 50))
#            for sf = (coerce i 'double-float)
#            for s = (format nil "~f" sf)
#            for i2 = (floor (read-from-string s))
#            repeat 2000
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.36};
)

#`(
# (deftest format.f.37
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'long-float))
#      (loop for i = (- (random (1- (ash 1 51))) -1 (ash 1 50))
#            for sf = (coerce i 'long-float)
#            for s = (format nil "~f" sf)
#            for i2 = (floor (read-from-string s))
#            repeat 2000
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.37};
)

#`(
# (deftest format.f.38
#   (funcall
#    (compile
#     nil
#     '(lambda ()
#        (with-standard-io-syntax
#         (let ((*read-default-float-format* 'short-float)
#               (total 0)
#               (len 0))
#           (loop for i from (- 1 (ash 1 13)) below (ash 1 13)
#                 unless (zerop i)
#                 nconc
#                 (loop for sf = (coerce i 'short-float)
#                       for w = (random 8)
#                       for d = (random 4)
#                       for s = (format nil "~v,vf" w d sf)
#                       for i2 = (ignore-errors (floor (read-from-string s)))
#                       repeat 5
#                       ; do (print (list w d s i i2))
#                       unless (eql i i2)
#                       do (incf total)
#                       and collect (list i sf w d s i2))
#                 when (> total 100) collect "count limit exceeded"
#                 and do (loop-finish)))))))
#   nil)
# 
ok deftest( {
	my @collected;
#	for @standard-chars -> $c {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
#	}
	@collected;
}, [ ]
), Q{format.f.38};
)

#`(
# (deftest format.f.39
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'single-float))
#      (loop for i = (- (random (1- (ash 1 25))) -1 (ash 1 24))
#            for sf = (coerce i 'single-float)
#            for w = (and (coin) (random 16))
#            for d = (random 4)
#            for s = (format nil "~v,vf" w d sf)
#            for i2 = (floor (read-from-string s))
#            repeat 2000
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf w d s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.39};
)

#`(
# (deftest format.f.40
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'double-float))
#      (loop for i = (- (random (1- (ash 1 51))) -1 (ash 1 50))
#            for sf = (coerce i 'double-float)
#            for w = (and (coin) (random 30))
#            for d = (random 6)
#            for s = (format nil "~v,vf" w d sf)
#            for i2 = (floor (read-from-string s))
#            repeat 2000
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf w d s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.40};
)

#`(
# (deftest format.f.41
#   (with-standard-io-syntax
#    (let ((*read-default-float-format* 'long-float))
#      (loop for i = (- (random (1- (ash 1 51))) -1 (ash 1 50))
#            for sf = (coerce i 'long-float)
#            for w = (and (coin) (random 30))
#            for d = (random 6)
#            for s = (format nil "~v,vf" w d sf)
#            for i2 = (floor (read-from-string s))
#            repeat 2000
#            unless (or (zerop i) (eql i i2))
#            collect (list i sf w d s i2))))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.41};
)

#`(
# (deftest format.f.42
#   (let ((chars +standard-chars+))
#     (loop
#      for k = (and (coin) (random 6))
#      for x = (random (/ (random-from-seq #(#.(coerce (* 32 (1- (ash 1 13))) 'short-float)
#                                              #.(coerce (* 256 (1- (ash 1 24))) 'single-float)
#                                              #.(coerce (* 256 (1- (ash 1 50))) 'double-float)
#                                              #.(coerce (* 256 (1- (ash 1 50))) 'long-float)))
#                         (if k (expt 10 k) 1)))
#      for w = (and (coin) (random 30))
#      for d = (and (coin) (random 10))
#      for overflowchar = (and (coin) (random-from-seq chars))
#      for padchar = (and (coin) (random-from-seq chars))
#      for f1 = (concatenate 'string
#                            "~"
#                            (if w (format nil "~d" w) "")
#                            ","
#                            (if d (format nil "~d" d) "")
#                            ","
#                            (if k (format nil "~d" k) "")
#                            ","
#                            (if overflowchar (format nil "'~c" overflowchar) "")
#                            ","
#                            (if padchar (format nil "'~c" padchar) "")
#                            (string (random-from-seq "fF")))
#      for s1 = (format nil f1 x)
#      for s2 = (format nil "~v,v,v,v,vf" w d k overflowchar padchar x)
#      repeat 2000
#      unless (string= s1 s2)
#      collect (list x w d k overflowchar padchar f1 s1 s2)))
#   nil)
# 
ok deftest( {
	my @collected;
	for 1 .. 2000 {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
	}
	@collected;
}, [ ]
), Q{format.f.42};
)

# ;;; This failed in sbcl 0.8.12.25
# 
#`(
# (def-format-test format.f.43
#   "~,,,,',f" (0.0) "0.0")
# 
ok def-format-test( Q{~,,,,',f}, ( 0.0 ), Q{0.0} ), Q{format.f.43};
)

#`(
# (deftest format.f.44
#   (loop for i from 0 below (min #x10000 char-code-limit)
#         for x = 2312.9817
#         for c = (code-char i)
#         for f1 = (and c (format nil "~~,,,,'~cf" c))
#         for s1 = (and c (ignore-errors (format nil f1 x)))
#         for s2 = (and c (format nil "~,,,,vf" c x))
#         unless (equal s1 s2)
#         collect (list i c f1 s1 s2))
#   nil)
# 
ok deftest( {
	my @collected;
#	for @standard-chars -> $c {
#		my $s1 = $c;
#		my $s2 = $*fl.format( Q{~a}, $s1 );
#		my $s3 = formatter-call-to-string( $fn, $s1 );
#		unless $s1 eq $s1 and $s2 eq $s3 {
#			@collected.append( [ $c, $s1, $s2, $s3 ] );
#		}
#	}
	@collected;
}, [ ]
), Q{format.f.44};
)

#`(
# (def-format-test format.f.45
#     "~2f" (1.1) "1.0")
# 
ok def-format-test( Q{~2f}, ( 1.1 ), Q{1.0} ), Q{format.f.45};
)

# (def-format-test format.f.45b
#     "~3f" (1.1) "1.1")
# 
ok def-format-test( Q{~3f}, ( 1.1 ), Q{1.1} ), Q{format.f.45b};

# ;; This fails on ECL 15.3.7
#`(
# (def-format-test format.f.46
#     "~0f" (0.01) ".0")
# 
ok def-format-test( Q{~0f}, ( 0.01 ), Q{.0} ), Q{format.f.46};
)

# ;; sbcl prints "."
#`(
# (def-format-test format.f.46b
#     "~0,0f" (0.01) "0.")
# 
ok def-format-test( Q{~0,0f}, ( 0.01 ), Q{0.} ), Q{format.f.46b};
)

# ;; Most implementations print .00
# (def-format-test format.f.47
#     "~3f" (0.000001) "0.0")
# 
ok def-format-test( Q{~3f}, ( 0.000001 ), Q{0.0} ), Q{format.f.47};

# ;; CCL 1.10 and ECL 15.3.7 ignore k parameter when w and d aren't set
#`(
# (def-format-test format.f.48
#     "~,,2f" (0.1) "10.0")
# 
ok def-format-test( Q{~,,2f}, ( 0.1 ), Q{10.0} ), Q{format.f.48};
)

done-testing;

# vim: ft=perl6


