### ----------------------------------------------------
### -- Format::Lisp
### -- Licenses: Artistic-2.0
### -- Authors: Jeffrey Goff
### -- File: t/24-format-o.rakutest
### ----------------------------------------------------
### -- Chunk 2 of 2
-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{~:o} );
	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.o.19};
)

#`(
# (deftest format.o.20
#   (let ((fn (formatter "~@o")))
#     (loop for x in *mini-universe*
#           for s1 = (format nil "~@o" x)
#           for s2 = (let ((*print-base* 8)) (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{~@o} );
	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.o.20};
)

#`(
# (deftest format.o.21
#   (let ((fn (formatter "~:@o")))
#     (loop for x in *mini-universe*
#           for s1 = (let ((*print-base* 8)) (format nil "~A" x))
#           for s2 = (format nil "~@:o" x)
#           for s3 = (formatter-call-to-string fn x)
#           for s4 = (let ((*print-base* 8)) (format nil "~A" x))
#           unless (or (integerp x) (and (string= s1 s2) (string= s2 s3))
#                      (string/= s1 s4))
#           collect (list x s1 s2 s3)))
#   nil)
# 
ok deftest( {
	my $fn = $*fl.formatter( Q{~:@o} );
	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.o.20};
)

# ;;; 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?
# 
# ;;; # arguments
# 
#`(
# (deftest format.o.22
#   (apply
#    #'values
#    (let ((fn (formatter "~#o"))
#          (n #o12345))
#      (loop for i from 0 to 10
#            for args = (make-list i)
#            for s = (apply #Q{format nil "~#o" n args)
#            for s2 = (with-output-to-string
#                       (stream)
#                       (assert (equal (apply fn stream n args) args)))
#            do (assert (string= s s2))
#            collect s)))
#   "12345"
#   "12345"
#   "12345"
#   "12345"
#   "12345"
#   " 12345"
#   "  12345"
#   "   12345"
#   "    12345"
#   "     12345"
#   "      12345")
# 
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{12345},
	Q{12345},
	Q{12345},
	Q{12345},
	Q{12345},
	Q{ 12345},
	Q{  12345},
	Q{   12345},
	Q{    12345},
	Q{     12345},
	Q{      12345}
]
), Q{format.o.22};
)

#`(
# (deftest format.o.23
#   (apply
#    #'values
#    (let ((fn (formatter "~,,,#:o"))
#          (n #o1234567012))
#      (loop for i from 0 to 10
#            for args = (make-list i)
#            for s = (apply #Q{format nil "~,,,#:o" 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,0,1,2"
#   "12,34,56,70,12"
#   "1,234,567,012"
#   "12,3456,7012"
#   "12345,67012"
#   "1234,567012"
#   "123,4567012"
#   "12,34567012"
#   "1,234567012"
#   "1234567012"
#   "1234567012")
# 
ok deftest( {
	my @collected;
	my $fn = $*fl.formatter( Q{~#O} );
	my $n = 0o1234567012;
	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.o.23};
)

#`(
# (deftest format.o.24
#   (apply
#    #'values
#    (let ((fn (formatter "~,,,#:@o"))
#          (n #o1234567012))
#      (loop for i from 0 to 10
#            for args = (make-list i)
#            for s = (apply #Q{format nil "~,,,#@:O" 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,0,1,2"
#   "+12,34,56,70,12"
#   "+1,234,567,012"
#   "+12,3456,7012"
#   "+12345,67012"
#   "+1234,567012"
#   "+123,4567012"
#   "+12,34567012"
#   "+1,234567012"
#   "+1234567012"
#   "+1234567012")
# 
ok deftest( {
	my @collected;
	my $fn = $*fl.formatter( Q{~#B} );
	my $n = 0o1234567012;
	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,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.o.24};
)

#`(
# (def-format-test format.o.25
#   "~+10o" (#o1234) "      1234")
# 
ok def-format-test( Q{~+10o}, ( 0o1234 ), Q{      1234} ), Q{format.o.25};
)

#`(
# (def-format-test format.o.26
#   "~+10@O" (#o1234) "     +1234")
# 
ok def-format-test( Q{~+10@o}, ( 0o1234 ), Q{     +1234} ), Q{format.o.26};
)

#`(
# (def-format-test format.o.27
#   "~-1O" (#o1234) "1234")
# 
ok def-format-test( Q{~-1O}, ( 0o1234 ), Q{1234} ), Q{format.o.27};
)

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

# (def-format-test format.o.29
#   "~vo" ((1- most-negative-fixnum) #o1234) "1234")
# 
# XXX Don't think it's applicable?

# ;;; Randomized test
# 
#`(
# (deftest format.o.30
#   (let ((fn (formatter "~v,v,v,vo")))
#     (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 "~do" commaint) "o"))
#      for s1 = (format nil fmt x)
#      for s2 = (format nil "~v,v,v,vo" 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)
# 
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{format.o.30};
)

done-testing;

# vim: ft=perl6


