# test for Mpformat
#
#  this file is designed to be run from mpexpr/unix or mpexpr/win
#

# Commands covered:  mpformat
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.
#
# Copyright (c) 1991-1994 The Regents of the University of California.
# Copyright (c) 1994 Sun Microsystems, Inc.
#
# See the file "mpexpr.c" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.


# check if mpexpr is already loaded:

if {[info commands mpexpr] == "mpexpr"} {
  puts "mpexpr statically compiled into [info nameofexecutable]"
} elseif {![catch {package require Mpexpr} version]} {
  puts "Mpexpr $version loaded via package require"
} else {
  set extdll  ""
  global tcl_platform
  if {[llength $argv]>0} {
     set dllpat [lindex $argv 0]
  } elseif {[string compare "windows" $tcl_platform(platform)]==0} {
     set dllpat ./mpexpr*[info sharedlibextension]
  } else {
     set dllpat ./libMpexpr*[info sharedlibextension]
  }
  catch {set extdll [glob $dllpat] }
  if {[llength $extdll]<1} {
     puts "Can't find dynamic library using pattern: $dllpat"
     exit
  }
  if {[llength $extdll]>1} {
     puts "Multiple dynamic libraries found: $extdll"
     exit
  }
  set extdll [lindex $extdll 0]
  if {[catch {load $extdll}]} {
         puts "can't load $extdll"
         exit
  }
  puts "mpexpr $extdll dynamically loaded into [info nameofexecutable]"
}


cd ../tests

source defs

set mp_i 12345678901234567890
set mp_s 12345678901234567890.12345678901234567890
set mp_b 12345678901234567890.98765432109876543210

set mp_j1 4294967296
set mp_j2 18446744073709551616

test mpformat-1.1 {%d integer} {mpformat %d $mp_i} 12345678901234567890
test mpformat-1.2 {%d integer} {mpformat >%30d< $mp_i} {>          12345678901234567890<}
test mpformat-1.2 {%d integer} {mpformat >%-30d< $mp_i} {>12345678901234567890<}
test mpformat-1.3 {%d float}   {mpformat %d $mp_s} 12345678901234567890
test mpformat-1.4 {%d float}   {mpformat %d $mp_b} 12345678901234567890


test mpformat-2.1 {%f integer} {mpformat %f $mp_i} 12345678901234567890.00000000
test mpformat-2.2 {%f float}   {mpformat %f $mp_s} 12345678901234567890.12345679
test mpformat-2.3 {%f float}   {mpformat %f $mp_b} 12345678901234567890.98765432
test mpformat-2.4 {%f float}   {mpformat >%30f< $mp_b} {> 12345678901234567890.98765432<}
test mpformat-2.5 {%f float}   {mpformat >%30.30f< $mp_b} {>12345678901234567890.987654321098765432100000000000<}
test mpformat-2.6 {%f float}   {mpformat >%-30.30f< $mp_b} {>12345678901234567890.987654321098765432100000000000<}
test mpformat-2.7 {%f float}   {mpformat >%0.8f< $mp_b} {>12345678901234567890.98765432<}
test mpformat-2.8 {%f float}   {mpformat %.0f $mp_b} {12345678901234567891}

test mpformat-3.1 {%e integer} {mpformat %e $mp_i} {1.23456789e19}
test mpformat-3.2 {%e integer} {mpformat >%.40e< $mp_i} {>1.2345678901234567890000000000000000000000e19<}
test mpformat-3.3 {%e float}   {mpformat %e $mp_s} {1.23456789e19}
test mpformat-3.4 {%e float}   {mpformat %.5e $mp_s} {1.23457e19}
test mpformat-3.5 {%e float}   {mpformat %e 40000} {4.00000000e4}
test mpformat-3.6 {%e float}   {mpformat %e 3800000000} {3.80000000e9}

test mpformat-4.1 {%r integer} {mpformat %r $mp_i} {12345678901234567890}
test mpformat-4.2 {%r float}   {mpformat %r $mp_s} {123456789012345678901234567890123456789/10000000000000000000}
test mpformat-4.3 {%r float}   {mpformat %r $mp_b} {123456789012345678909876543210987654321/10000000000000000000}

test mpformat-5.1 {%N float}   {mpformat %N $mp_s} {123456789012345678901234567890123456789}

test mpformat-5.1 {%D float}   {mpformat %D $mp_s} {10000000000000000000}

test mpformat-6.1 {%o integer} {mpformat %o $mp_i} {01255245230635307605322}
test mpformat-6.2 {%o float}   {mpformat %o $mp_s} {01347016464530012776613253375062425616300425/01053071060221172000000}
test mpformat-6.3 {%o integer} {mpformat %o $mp_j1} {040000000000}
test mpformat-6.4 {%o integer} {mpformat %o $mp_j2} {02000000000000000000000}

test mpformat-7.1 {%x integer} {mpformat %x $mp_i} {0xab54a98ceb1f0ad2}
test mpformat-7.2 {%x float}   {mpformat %x $mp_s} {0x5ce0e9a56015fec5aadfa328ae398115/0x8ac7230489e80000}
test mpformat-7.3 {%o integer} {mpformat %x $mp_j1} {0x100000000}
test mpformat-7.4 {%o integer} {mpformat %x $mp_j2} {0x10000000000000000}

test mpformat-8.1 {%b integer} {mpformat %b $mp_i} {0b1010101101010100101010011000110011101011000111110000101011010010}
test mpformat-8.2 {%b float}   {mpformat %b $mp_s} {0b1011100111000001110100110100101011000000001010111111110110001011010101011011111101000110010100010101110001110011000000100010101/0b1000101011000111001000110000010010001001111010000000000000000000}

test mpformat-9.3 {Patch #2} {
    mpformat %e 60000
} 6.00000000e4
test mpformat-9.4 {Patch #2} {
    mpformat %e 4200000000
} 4.20000000e9

puts "mpformat tests complete"
