###############################################################################
#
# format.eagle --
#
# Extensible Adaptable Generalized Logic Engine (Eagle)
#
# Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: $
#
###############################################################################

source [file join [file normalize [file dirname [info script]]] prologue.eagle]

###############################################################################
# MONO: Due to a Mono bug, some tests are disabled on Mono.
#       See: https://bugzilla.novell.com/show_bug.cgi?id=472845
###############################################################################

#
# NOTE: We are going to run the tests from the Tcl test suite verbatim.
#
# \
runTest ; # fake out runAllTests proc.

###############################################################################

if {[isEagle]} then {
  if {![info exists no(skipSomeTests)] && \
      ![info exists no(skipSomeFormatTests)]} then {
    set skipNames [list]

    lappend skipNames format-1.12; # NOTE: Needs BigNum.
    lappend skipNames format-20.1; # NOTE: Needs [dict].

    #
    # NOTE: All of these tests require the floating point formatting provided
    #       by the native snprintf function (i.e. from the C runtime library).
    #       Apparently, Mono cannot handle using the "undocumented" __arglist
    #       keyword supported by C#.  It produces the following error:
    #
    #       System.InvalidProgramException: Invalid IL code in
    #           Eagle._Components.Private.NativeOps:PrintDouble (
    #               System.Text.StringBuilder,string,double,int&,
    #               Eagle._Components.Public.Result&):
    #       IL_0019: call 0x0a0009ae
    #
    #       See: https://bugzilla.novell.com/show_bug.cgi?id=472845
    #
    if {![haveConstraint native]} then {
      tputs $test_channel "---- skipping floating point formatting tests...\n"

      lappend skipNames format-4.* format-5.* format-6.* format-7.1
      lappend skipNames format-7.2 format-8.5 format-8.6 format-8.7
      lappend skipNames format-8.8 format-8.10 format-8.15 format-8.16
      lappend skipNames format-13.* format-17.4 format-19.3
    }

    foreach skipName $skipNames {
      if {[lsearch -exact $eagle_tests(SkipNames) $skipName] == -1} then {
        lappend eagle_tests(SkipNames) $skipName
      }
    }

    unset -nocomplain skipName
  }
} else {
  #
  # NOTE: Save the "cleanupTests" procedure now and create a stub as we do not
  #       want the "format.test" file to actually "cleanup" after itself, that
  #       will be done at the very end.
  #
  rename ::tcltest::cleanupTests ::tcltest::savedCleanupTests
  proc ::tcltest::cleanupTests { args } {}
}

###############################################################################

tsource [file join $test_path tcl format.test] false false

###############################################################################

if {[isEagle]} then {
  if {[info exists skipNames]} then {
    #
    # NOTE: Remove the test names to be skipped that were added above.
    #
    foreach skipName $skipNames {
      set skipIndex [lsearch -exact $eagle_tests(SkipNames) $skipName]

      if {$skipIndex != -1} then {
        set eagle_tests(SkipNames) [lreplace $eagle_tests(SkipNames) \
            $skipIndex $skipIndex]
      }
    }

    unset -nocomplain skipNames skipName skipIndex
  }
} else {
  #
  # NOTE: Restore the original "cleanupTests" procedure now.
  #
  rename ::tcltest::cleanupTests ""
  rename ::tcltest::savedCleanupTests ::tcltest::cleanupTests
}

###############################################################################

unset -nocomplain i msg result x; # HACK: Cleanup leftovers.

###############################################################################

runTest {test format-90.1 {hexadecimal byte formatting} -body {
  list [format %yx 0x78] [format %yx 0x5678] [format %yx 0x345678] \
      [format %yx 0x12345678]
} -constraints {eagle} -result {78 78 78 78}}

###############################################################################

runTest {test format-91.1 {decimal byte formatting} -body {
  list [format %yd 0x78] [format %yd 0x5678] [format %yd 0x345678] \
      [format %yd 0x12345678]
} -constraints {eagle} -result {120 120 120 120}}

###############################################################################

source [file join [file normalize [file dirname [info script]]] epilogue.eagle]
