###############################################################################
#
# tcl-encoding.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]

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

runTest {test tclEncoding-1.1 {proper encodings for Unicode round-trip} -setup {
  unset -nocomplain interp
  set tcl_loaded 0
  if {![tcl ready]} then {tcl load; set tcl_loaded 1}
  set interp [tcl create]
} -body {
  unset -nocomplain x y
  append x "╔════════════════════════════════════════════════════╗\n"
  append x "║     These box characters are actually Unicode.     ║\n"
  append x "║ You should see two boxes with double-line borders. ║\n"
  append x "╚════════════════════════════════════════════════════╝\n"

  set y [tcl eval $interp [list set x $x]]
  tcl eval $interp [list puts $test_channel $x]
  tcl eval $interp [list puts $test_channel $y]

  list [expr {[string compare $x $y] == 0}]
} -cleanup {
  if {[info exists interp]} then {tcl delete $interp}
  if {$tcl_loaded} then {tcl unload}
  unset -nocomplain tcl_loaded interp x y
} -constraints \
{eagle native dynamic interactive tclLibrary command.tcl compile.NATIVE\
compile.TCL primaryThread} -result {True}}

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

runTest {test tclEncoding-1.2 {proper encodings for byte round-trip} -setup {
  unset -nocomplain interp
  set tcl_loaded 0
  if {![tcl ready]} then {tcl load; set tcl_loaded 1}
  set interp [tcl create]
} -body {
  unset -nocomplain x1
  append x1 "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"
  append x1 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"
  append x1 "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F"
  append x1 "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F"
  append x1 "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F"
  append x1 "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F"
  append x1 "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F"
  append x1 "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F"
  append x1 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
  append x1 "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
  append x1 "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
  append x1 "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF"
  append x1 "\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF"
  append x1 "\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF"
  append x1 "\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF"
  append x1 "\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"

  set y1 [tcl eval $interp [list set y1 $x1]]

  set x2 [string reverse $x1]
  set y2 [tcl eval $interp [list set y2 $x2]]

  set x3 [expr randstr(20000)]
  set y3 [tcl eval $interp [list set y3 $x3]]

  list [expr {[string compare $x1 $y1] == 0}] \
       [expr {[string compare $x2 $y2] == 0}] \
       [expr {[string compare $x3 $y3] == 0}]
} -cleanup {
  if {[info exists interp]} then {tcl delete $interp}
  if {$tcl_loaded} then {tcl unload}
  unset -nocomplain tcl_loaded interp x1 x2 x3 y1 y2 y3
} -constraints {eagle native dynamic tclLibrary command.tcl compile.NATIVE\
compile.TCL primaryThread} -result {True True True}}

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

runTest {test tclEncoding-1.3 {proper encodings for Unicode round-trip} -setup {
  unset -nocomplain interp
  set tcl_loaded 0
  if {![tcl ready]} then {tcl load; set tcl_loaded 1}
  set interp [tcl create]
} -body {
  unset -nocomplain x1
  append x1 "\u0100\u0302\u0504\u0706\u0908\u0B0A\u0D0C\u0F0E"
  append x1 "\u1110\u1312\u1514\u1716\u1918\u1B1A\u1D1C\u1F1E"
  append x1 "\u2120\u2322\u2524\u2726\u2928\u2B2A\u2D2C\u2F2E"
  append x1 "\u3130\u3332\u3534\u3736\u3938\u3B3A\u3D3C\u3F3E"
  append x1 "\u4140\u4342\u4544\u4746\u4948\u4B4A\u4D4C\u4F4E"
  append x1 "\u5150\u5352\u5554\u5756\u5958\u5B5A\u5D5C\u5F5E"
  append x1 "\u6160\u6362\u6564\u6766\u6968\u6B6A\u6D6C\u6F6E"
  append x1 "\u7170\u7372\u7574\u7776\u7978\u7B7A\u7D7C\u7F7E"
  append x1 "\u8180\u8382\u8584\u8786\u8988\u8B8A\u8D8C\u8F8E"
  append x1 "\u9190\u9392\u9594\u9796\u9998\u9B9A\u9D9C\u9F9E"
  append x1 "\uA1A0\uA3A2\uA5A4\uA7A6\uA9A8\uABAA\uADAC\uAFAE"
  append x1 "\uB1B0\uB3B2\uB5B4\uB7B6\uB9B8\uBBBA\uBDBC\uBFBE"
  append x1 "\uC1C0\uC3C2\uC5C4\uC7C6\uC9C8\uCBCA\uCDCC\uCFCE"
  append x1 "\uD1D0\uD3D2\uD5D4\uD7D6\uD9D8\uDBDA\uDDDC\uDFDE"
  append x1 "\uE1E0\uE3E2\uE5E4\uE7E6\uE9E8\uEBEA\uEDEC\uEFEE"
  append x1 "\uF1F0\uF3F2\uF5F4\uF7F6\uF9F8\uFBFA\uFDFC\uFFFE"

  set y1 [tcl eval $interp [list set y1 $x1]]

  set x2 [string reverse $x1]
  set y2 [tcl eval $interp [list set y2 $x2]]

  list [expr {[string compare $x1 $y1] == 0}] \
       [expr {[string compare $x2 $y2] == 0}]
} -cleanup {
  if {[info exists interp]} then {tcl delete $interp}
  if {$tcl_loaded} then {tcl unload}
  unset -nocomplain tcl_loaded interp x1 x2 y1 y2
} -constraints {eagle native dynamic tclLibrary command.tcl compile.NATIVE\
compile.TCL primaryThread} -result {True True}}

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

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