### ----------------------------------------------------
### -- Format::Lisp
### -- Licenses: Artistic-2.0
### -- Authors: Jeffrey Goff
### -- File: t/12-format-b.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
, $s1, $s2, $s3 ] );
			}
		}
		@collected;
	}, [ ]
	), Q{format.b.18};
	)

	#`(
	# (deftest format.b.19
	#   (let ((fn (formatter "~:b")))
	#     (loop for x in *mini-universe*
	#           for s1 = (format nil "~:B" x)
	#           for s2 = (let ((*print-base* 2)) (format nil "~A" x))
	#           for s3 = (formatter-call-to-string fn x)
	#           unless (or (integerp x) (and (string= s1 s2) (string= s1 s3)))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~:b} );
		for @MINI-UNIVERSE -> $x {
			my $s1 = $*fl.format( Q{~:B}, $x );
			my $s2 = do {
				my $*PRINT-BASE = 2;
				return $*fl.format( Q{~A}, $x );
			};
			my $s3 = formatter-call-to-string( $fn, $x );
			unless $x ~~ Int or ( $s1 eq $s2 and $s1 eq $s3 ) {
				@collected.append( [ $i, $s1, $s2, $s3 ] );
			}
		}
		@collected.elems;
	}, [ ]
	), Q{format.b.19};
	)

	#`(
	# (deftest format.b.20
	#   (let ((fn (formatter "~@b")))
	#     (loop for x in *mini-universe*
	#           for s1 = (format nil "~@b" x)
	#           for s2 = (let ((*print-base* 2)) (format nil "~A" x))
	#           for s3 = (formatter-call-to-string fn x)
	#           unless (or (integerp x) (and (string= s1 s2) (string= s1 s3)))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~@b} );
		for @MINI-UNIVERSE -> $x {
			my $s1 = $*fl.format( Q{~@b}, $x );
			my $s2 = do {
				my $*PRINT-BASE = 2;
				return $*fl.format( Q{~A}, $x );
			};
			my $s3 = formatter-call-to-string( $fn, $x );
			unless $x ~~ Int or ( $s1 eq $s2 and $s1 eq $s3 ) {
				@collected.append( [ $i, $s1, $s2, $s3 ] );
			}
		}
		@collected.elems;
	}, [ ]
	), Q{format.b.20};
	)

	#`(
	# (deftest format.b.21
	#   (let ((fn (formatter "~:@b")))
	#     (loop for x in *mini-universe*
	#           for s1 = (let ((*print-base* 2)) (format nil "~A" x))
	#           for s2 = (format nil "~@:B" x)
	#           for s3 = (formatter-call-to-string fn x)
	#           for s4 = (let ((*print-base* 2)) (format nil "~A" x))
	#           unless (or (integerp x) (and (string= s1 s2) (string= s1 s3))
	#                      (string/= s1 s4))
	#           collect (list x s1 s2 s3)))
	#   nil)
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~:@b} );
		for @MINI-UNIVERSE -> $x {
			my $s1 = $*fl.format( Q{~@:b}, $x );
			my $s2 = do {
				my $*PRINT-BASE = 2;
				return $*fl.format( Q{~A}, $x );
			};
			my $s3 = formatter-call-to-string( $fn, $x );
			unless $x ~~ Int or ( $s1 eq $s2 and $s1 eq $s3 ) {
				@collected.append( [ $i, $s1, $s2, $s3 ] );
			}
		}
		@collected.elems;
	}, [ ]
	), Q{format.b.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.b.22
	#   (apply
	#    #'values
	#    (let ((fn (formatter "~#B"))
	#          (bv #b11001))
	#      (loop for i from 0 to 10
	#            for args = (make-list i)
	#            for s = (apply #'format nil "~#b" bv args)
	#            for s2 = (with-output-to-string
	#                       (stream)
	#                       (assert (equal (apply fn stream bv args) args)))
	#            do (assert (string= s s2))
	#            collect s)))
	#   "11001"
	#   "11001"
	#   "11001"
	#   "11001"
	#   "11001"
	#   " 11001"
	#   "  11001"
	#   "   11001"
	#   "    11001"
	#   "     11001"
	#   "      11001")
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~#B} );
		my $bv = 0b11001;
		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{11001},
		Q{11001},
		Q{11001},
		Q{11001},
		Q{11001},
		Q{ 11001},
		Q{  11001},
		Q{   11001},
		Q{    11001},
		Q{     11001},
		Q{      11001}
	]
	), Q{format.b.22};
	)

	#`(
	# (deftest format.b.23
	#   (apply
	#    #'values
	#    (let ((fn (formatter "~,,,#:b"))
	#          (bv #b1100100010))
	#      (loop for i from 0 to 10
	#          for args = (make-list i)
	#          for s = (apply #'format nil "~,,,#:B" bv args)
	#          for s2 = (with-output-to-string
	#                       (stream)
	#                       (assert (equal (apply fn stream bv args) args)))
	#            do (assert (string= s s2))
	#            collect s)))
	#   "1,1,0,0,1,0,0,0,1,0"
	#   "11,00,10,00,10"
	#   "1,100,100,010"
	#   "11,0010,0010"
	#   "11001,00010"
	#   "1100,100010"
	#   "110,0100010"
	#   "11,00100010"
	#   "1,100100010"
	#   "1100100010"
	#   "1100100010")
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~#B} );
		my $bv = 0b11001;
		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,1,0,0,1,0,0,0,1,0},
		Q{11,00,10,00,10},
		Q{1,100,100,010},
		Q{11,0010,0010},
		Q{11001,00010},
		Q{1100,100010},
		Q{110,0100010},
		Q{11,00100010},
		Q{1,100100010},
		Q{1100100010},
		Q{1100100010}
	]
	), Q{format.b.23};
	)

	#`(
	# (deftest format.b.24
	#   (apply
	#    #'values
	#    (let ((fn (formatter "~,,,#@:B"))
	#          (bv #b1100100010))
	#      (loop for i from 0 to 10
	#            for args = (make-list i)
	#            for s = (apply #'format nil "~,,,#@:B" bv args)
	#            for s2 = (with-output-to-string
	#                       (stream)
	#                       (assert (equal (apply fn stream bv args) args)))
	#            do (assert (string= s s2))
	#            collect s)))
	#   "+1,1,0,0,1,0,0,0,1,0"
	#   "+11,00,10,00,10"
	#   "+1,100,100,010"
	#   "+11,0010,0010"
	#   "+11001,00010"
	#   "+1100,100010"
	#   "+110,0100010"
	#   "+11,00100010"
	#   "+1,100100010"
	#   "+1100100010"
	#   "+1100100010")
	# 
	ok deftest( {
		my @collected;
		my $fn = $*fl.formatter( Q{~,,,#@:B} );
		my $bv = 0b1100100010;
		for 0 .. 10 -> $i {
	#		my @args = 
	#		my $s = $*fl.format( Q{~v,,2A}, $i, 'ABC' );
	#		my $s2 = formatter-call-to-string( $fn, $i, 'ABC' );
	#		is $s, $s2;
	#		@collected.append( $s );
		}
		@collected;
	}, [	Q{+1,1,0,0,1,0,0,0,1,0},
		Q{+11,00,10,00,10},
		Q{+1,100,100,010},
		Q{+11,0010,0010},
		Q{+11001,00010},
		Q{+1100,100010},
		Q{+110,0100010},
		Q{+11,00100010},
		Q{+1,100100010},
		Q{+1100100010},
		Q{+1100100010}
	]
	), Q{format.b.24};
	)

	# (def-format-test format.b.25
	#   "~+10b" (#b1101) "      1101")
	# 
	ok def-format-test(
		Q{~+10b}, ( 0b1101 ), Q{      1101}
	), Q{format.b.25};

	# (def-format-test format.b.26
	#   "~+10@B" (#b1101) "     +1101")
	# 
	ok def-format-test(
		 Q{~+10@b}, ( 0b1101 ), Q{     +1101}
	), Q{format.b.26};

	# (def-format-test format.b.27
	#   "~-1b" (#b1101) "1101")
	# 
	ok def-format-test( Q{~+1b}, ( 0b1101 ), Q{1101} ), Q{format.b.27};

	# (def-format-test format.b.28
	#   "~-1000000000000000000B" (#b1101) "1101")
	# 
	ok def-format-test(
		Q{~-1000000000000000000B}, ( 0b1101 ), Q{1101}
	), Q{format.b.28};

	# (def-format-test format.b.29
	#   "~vb" ((1- most-negative-fixnum) #b1101) "1101")
	# 
	# XXX Don't think it's applicable?
}, Q{arguments};

# ;;; Randomized test
# 
#`(
# (deftest format.b.30
#   (let ((fn (formatter "~V,V,V,VB")))
#     (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 "~db" commaint) "b"))
#      for s1 = (format nil fmt x)
#      for s2 = (format nil "~v,v,v,vb" 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)))
#   nil)
#
ok deftest( {
	my @collected;
	my $fn = $*fl.formatter( Q{~V,V,V,VB} );
	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
	}
	@collected;
}, [ ]
), Q{format.b.30};
)

done-testing;

# vim: ft=perl6


