#!/usr/local/bin/python
#
# File:        overloadtest
# Copyright:   (c) 2001 The Regents of the University of California
# Release:     $Name: release-0-8-8 $
# Revision:    @(#) $Revision: 1.3 $
# Date:        $Date: 2002/07/26 00:18:31 $
# Description: Exercise overloaded methods
#
# Try to exercise everything possible in Python.

import Overload.Test
import Overload.AnException
import Overload.AClass
import Overload.BClass
import sys
import Numeric

def toFloat(d):
  tmp = Numeric.zeros((1,), Numeric.Float32)
  try:
    tmp.savespace(1)
    tmp[0] = d
  except AttributeError:
    # don't die!
    tmp[0] = d
  return tmp[0]

class TestCounter:
  partno = 0
  numpass = 0
  numfail = 0
  numxfail = 0
  numxpass = 0

  def __init__(self, numparts = -1):
    self.numparts = numparts
    print "NPARTS", numparts

  def describeTest(self, description):
    self.partno = self.partno + 1
    print "PART", self.partno
    print "COMMENT:", description

  def evalTest(self, result, expected):
    if (result):
      if (expected):
        self.numpass = self.numpass + 1
        print "RESULT", self.partno, "PASS"
      else:
        self.numxpass = self.numxpass + 1
        print "RESULT", self.partno, "XPASS"
    else:
      if (expected):
        self.numfail = self.numfail + 1
        print "RESULT", self.partno, "FAIL"
      else:
        self.numxfail = self.numxfail + 1
        print "RESULT", self.partno, "XFAIL"
        
  def finish(self):
    if (self.numparts < 0):
      self.numparts = self.partno
    if (self.numpass == self.numparts):
      print "TEST_RESULT PASS"
    else:
      if (self.numpass + self.numxfail == self.numparts):
        print "TEST_RESULT XFAIL"
      else:
        print "TEST_RESULT FAIL"
      

if __name__ == '__main__':
  b1 = 1
  d1 = 1.0
  i1 = 1
  f1 = toFloat(1.0)
  did = d1 + i1
  didf = did + f1
  s1 = "AnException"
  fc1 = toFloat(1.1) + toFloat(1.1) * 1.0j
  dc1 = 2.2 + 2.2 * 1.0j

  counter = TestCounter(23)
  counter.describeTest("t = Overload.Test.Test()")
  t = Overload.Test.Test()
  counter.evalTest(t != None, 1)
  
  # 0-argument test
  counter.describeTest("t.getValue() -> expect a 1 returned")
  counter.evalTest(t.getValue() == 1, 1)
  
  # 1-argument tests
  counter.describeTest("t.getValueBool()")
  counter.evalTest(t.getValueBool(b1) == b1, 1)
  counter.describeTest("t.getValueDouble()")
  counter.evalTest(t.getValueDouble(d1) == d1, 1)
  counter.describeTest("t.getValueDcomplex()")
  counter.evalTest(t.getValueDcomplex(dc1) == dc1, 1)
  counter.describeTest("t.getValueFloat()")
  counter.evalTest(t.getValueFloat(f1) == f1, 1)
  counter.describeTest("t.getValueFcomplex()")
  counter.evalTest(t.getValueFcomplex(fc1) == fc1, 1)
  counter.describeTest("t.getValueInt()")
  counter.evalTest(t.getValueInt(i1) == i1, 1)
  counter.describeTest("t.getValueString()")
  counter.evalTest(t.getValueString(s1) == s1, 1)

  counter.describeTest("ae = Overload.AnException.AnException()")
  ae = Overload.AnException.AnException()
  counter.evalTest(ae != None, 1)
  counter.describeTest("t.getValueException() -> expect success")
  counter.evalTest(t.getValueException(ae) == s1, 1)

  counter.describeTest("ac = Overload.AClass.AClass()")
  ac = Overload.AClass.AClass()
  counter.evalTest(ac != None, 1)
  counter.describeTest("t.getValueAClass() -> expect a 2 returned")
  counter.evalTest(t.getValueAClass(ac) == 2, 1)
  
  counter.describeTest("bc = Overload.BClass.BClass()")
  bc = Overload.BClass.BClass()
  counter.evalTest(bc != None, 1)
  counter.describeTest("t.getValueBClass() -> expect a 2 returned")
  counter.evalTest(t.getValueBClass(bc) == 2, 1)
  
  # 2-argument tests
  counter.describeTest("t.getValueDoubleInt() -> expect success")
  counter.evalTest(t.getValueDoubleInt(d1, i1) == did, 1)
  counter.describeTest("t.getValueIntDouble() -> expect success")
  counter.evalTest(t.getValueIntDouble(i1, d1) == did, 1)

  # 3-argument tests
  counter.describeTest("t.getValueDoubleIntFloat() -> expect success")
  counter.evalTest(t.getValueDoubleIntFloat(d1, i1, f1) == didf, 1)
  counter.describeTest("t.getValueIntDoubleFloat() -> expect success")
  counter.evalTest(t.getValueIntDoubleFloat(i1, d1, f1) == didf, 1)

  counter.describeTest("t.getValueDoubleFloatInt() -> expect success")
  counter.evalTest(t.getValueDoubleFloatInt(d1, f1, i1) == didf, 1)
  counter.describeTest("t.getValueIntFloatDouble() -> expect success")
  counter.evalTest(t.getValueIntFloatDouble(i1, f1, d1) == didf, 1)

  counter.describeTest("t.getValueFloatDoubleInt() -> expect success")
  counter.evalTest(t.getValueFloatDoubleInt(f1, d1, i1) == didf, 1)
  counter.describeTest("t.getValueFloatIntDouble() -> expect success")
  counter.evalTest(t.getValueFloatIntDouble(f1, i1, d1) == didf, 1)

  counter.finish()
0
