### ----------------------------------------------------
### -- Format::Lisp
### -- Licenses: Artistic-2.0
### -- Authors: Jeffrey Goff
### -- File: t/36-format-x.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
 s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~@X} );
		for 1 .. 1000 {
	#		my $x = 2 ** ( 2 + 80.rand.Int );
	#		my $i = ( ( $x + $x ).rand - $x );
	#		my $s1 = $*fl.format( Q{~B}, $i );
	#		my $s2 = formatter-call-to-string( $fn, $i );
	#		# XXX convert $s1 to base-10
		}
	}, [ ]
	), Q{format.x.18};
	)

	#`(
	# (deftest format.x.19
	#   (let ((fn (formatter "~:x")))
	#     (loop for x in *mini-universe*
	#           for s1 = (format nil "~:x" x)
	#           for s2 = (let ((*print-base* 16)) (format nil "~A" x))
	#           for s3 = (formatter-call-to-string fn x)
	#           unless (or (integerp x) (and (string= s1 s2) (string= s2 s3)))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~@X} );
		for 1 .. 1000 {
	#		my $x = 2 ** ( 2 + 80.rand.Int );
	#		my $i = ( ( $x + $x ).rand - $x );
	#		my $s1 = $*fl.format( Q{~B}, $i );
	#		my $s2 = formatter-call-to-string( $fn, $i );
	#		# XXX convert $s1 to base-10
		}
	}, [ ]
	), Q{format.x.19};
	)

	#`(
	# (deftest format.x.20
	#   (let ((fn (formatter "~@x")))
	#     (loop for x in *mini-universe*
	#           for s1 = (format nil "~@x" x)
	#           for s2 = (let ((*print-base* 16)) (format nil "~A" x))
	#           for s3 = (formatter-call-to-string fn x)
	#           unless (or (integerp x) (and (string= s1 s2) (string= s2 s3)))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~@X} );
		for 1 .. 1000 {
	#		my $x = 2 ** ( 2 + 80.rand.Int );
	#		my $i = ( ( $x + $x ).rand - $x );
	#		my $s1 = $*fl.format( Q{~B}, $i );
	#		my $s2 = formatter-call-to-string( $fn, $i );
	#		# XXX convert $s1 to base-10
		}
	}, [ ]
	), Q{format.x.20};
	)

	#`(
	# (deftest format.x.21
	#   (let ((fn (formatter "~:@x")))
	#     (loop for x in *mini-universe*
	#           for s1 = (let ((*print-base* 16)) (format nil "~A" x))
	#           for s2 = (format nil "~@:x" x)
	#           for s3 = (formatter-call-to-string fn x)
	#           for s4 = (let ((*print-base* 16)) (format nil "~A" x))
	#           unless (or (string/= s1 s4)
	#                      (integerp x)
	#                      (and (string= s1 s2) (string= s2 s3)))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~@X} );
		for 1 .. 1000 {
	#		my $x = 2 ** ( 2 + 80.rand.Int );
	#		my $i = ( ( $x + $x ).rand - $x );
	#		my $s1 = $*fl.format( Q{~B}, $i );
	#		my $s2 = formatter-call-to-string( $fn, $i );
	#		# XXX convert $s1 to base-10
		}
	}, [ ]
	), Q{format.x.21};
	)
}, Q{When the argument is not an integer, print as if using ~A and base 10};

# ;;; Must add tests for non-integers when the parameters
# ;;; are specified, but it's not clear what the meaning is.
# ;;; Does mincol apply to the ~A equivalent?  What about padchar?
# ;;; Are comma-char and comma-interval always ignored?

subtest {
	#`(
	# (deftest format.x.22
	#   (apply
	#    #'values
	#    (let ((fn (formatter "~#X"))
	#          (n #x1b3fe))
	#      (loop for i from 0 to 10
	#            for args = (make-list i)
	#            for s = (apply #Q{format nil "~#x" n args)
	#            for s2 = (with-output-to-string
	#                       (stream)
	#                       (assert (equal (apply fn stream n args) args)))
	#            do (assert (string= s s2))
	#            collect (string-upcase s))))
	#   "1B3FE"
	#   "1B3FE"
	#   "1B3FE"
	#   "1B3FE"
	#   "1B3FE"
	#   " 1B3FE"
	#   "  1B3FE"
	#   "   1B3FE"
	#   "    1B3FE"
	#   "     1B3FE"
	#   "      1B3FE")
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~@X} );
		for 1 .. 1000 {
	#		my $x = 2 ** ( 2 + 80.rand.Int );
	#		my $i = ( ( $x + $x ).rand - $x );
	#		my $s1 = $*fl.format( Q{~B}, $i );
	#		my $s2 = formatter-call-to-string( $fn, $i );
	#		# XXX convert $s1 to base-10
		}
	}, [ ]
	), Q{format.x.22};
	)

	#`(
	# (deftest format.x.23
	#   (apply
	#    #'values
	#    (let ((fn (formatter "~,,,#:X"))
	#          (n #x1234567890))
	#      (loop for i from 0 to 10
	#            for args = (make-list i)
	#            for s = (apply #Q{format nil "~,,,#:x" n args)
	#            for s2 = (with-output-to-string
	#                       (stream)
	#                       (assert (equal (apply fn stream n args) args)))
	#            do (assert (string= s s2))
	#            collect s)))
	#   "1,2,3,4,5,6,7,8,9,0"
	#   "12,34,56,78,90"
	#   "1,234,567,890"
	#   "12,3456,7890"
	#   "12345,67890"
	#   "1234,567890"
	#   "123,4567890"
	#   "12,34567890"
	#   "1,234567890"
	#   "1234567890"
	#   "1234567890")
	# 
	ok deftest( {
		my $fn = $*fl.formatter( Q{~@X} );
		for 1 .. 1000 {
	#		my $x = 2 ** ( 2 + 80.rand.Int );
	#		my $i = ( ( $x + $x ).rand - $x );
	#		my $s1 = $*fl.format( Q{~B}, $i );
	#		my $s2 = formatter-call-to-string( $fn, $i );
	#		# XXX convert $s1 to base-10
		}
	}, [ ]
	), Q{format.x.23};
	)

	#`(
	# (deftest format.x.24
	#   (apply
	#    #'values
	#    (let ((fn (formatter "~,,,#@:X"))
	#          (n #x1234567890))
	#      (loop for i from 0 to 10
	#            for args = (make-list i)
	#            for s = (apply #Q{format nil "~,,,#@:X" n args)
	#            for s2 = (with-output-to-string
	#                       (stream)
	#                       (assert (equal (apply fn stream n args) args)))
	#            do (assert (string= s s2))
	#            collect s)))
	#   "+1,2,3,4,5,6,7,8,9,0"
	#   "+12,34,56,78,90"
	#   "+1,234,567,890"
	#   "+12,3456,7890"
	#   "+12345,67890"
	#   "+1234,567890"
	#   "+123,4567890"
	#   "+12,34567890"
	#   "+1,234567890"
	#   "+1234567890"
	#   "+1234567890")
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~#X} );
		my $n = ox1234567890;
		for 0 .. 10 -> $i {
	#		my @args = 
	##		my $s = $*fl.format( Q{~v,,2A}, $i, Q{ABC} );
	#		my $s2 = formatter-call-to-string( $fn, $i, Q{ABC} );
	#		is $s, $s2;
	#		@collected.append( $s );
		}
		@collected;
	}, [	Q{1,2,3,4,5,6,7,0,1,2},
		Q{12,34,56,70,12},
		Q{1,234,567,012},
		Q{12,3456,7012},
		Q{12345,67012},
		Q{1234,567012},
		Q{123,4567012},
		Q{12,34567012},
		Q{1,234567012},
		Q{1234567012},
		Q{1234567012}
	]
	), Q{format.x.24};
	)

	# (def-format-test format.x.25
	#   "~+10x" (#x1234) "      1234")
	# 
	ok def-format-test(
		Q{~+10X}, ( 0x1234 ), Q{      1234}
	), Q{format.x.25};

	# (def-format-test format.x.26
	#   "~+10@X" (#x1234) "     +1234")
	# 
	ok def-format-test(
		Q{~+10@X}, ( 0x1234 ), Q{     +1234}
	), Q{format.x.26};

	# (def-format-test format.x.27
	#   "~-1X" (#x1234) "1234")
	# 
	ok def-format-test(
		Q{~-1X}, ( 0x1234 ), Q{1234}
	), Q{format.x.27};

	# (def-format-test format.x.28
	#   "~-1000000000000000000x" (#x1234) "1234")
	# 
	ok def-format-test(
		Q{~-1000000000000000000x}, ( 0x1234 ), Q{1234}
	), Q{format.x.28};

	# (def-format-test format.x.29
	#   "~vx" ((1- most-negative-fixnum) #x1234) "1234")
	# 
	# XXX Don't think it's applicable?
}, Q{# arguments};

# ;;; Randomized test
# 
#`(
# (deftest format.x.30
#   (let ((fn (formatter "~v,v,v,vx")))
#     (loop
#      for mincol = (and (coin) (random 50))
#      for padchar = (and (coin)
#                         (random-from-seq +standard-chars+))
#      for commachar = (and (coin)
#                           (random-from-seq +standard-chars+))
#      for commaint = (and (coin) (1+ (random 10)))
#      for k = (ash 1 (+ 2 (random 30)))
#      for x = (- (random (+ k k)) k)
#      for fmt = (concatenate
#                 'string
#                 (if mincol (format nil "~~~d," mincol) "~,")
#                 (if padchar (format nil "'~c," padchar) ",")
#                 (if commachar (format nil "'~c," commachar) ",")
#                 (if commaint (format nil "~dx" commaint) "x"))
#      for s1 = (format nil fmt x)
#      for s2 = (format nil "~v,v,v,vx" mincol padchar commachar commaint x)
#      for s3 = (formatter-call-to-string fn mincol padchar commachar commaint x)
#      repeat 2000
#      unless (and (string= s1 s2) (string= s2 s3))
#      collect (list mincol padchar commachar commaint fmt x s1 s2 s3)))
#   nil)
# 
)

done-testing;

# vim: ft=perl6


