testFarray.py :  » Math » Numerical-Python » numpy » doc » swig » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Math » Numerical Python 
Numerical Python » numpy » doc » swig » test » testFarray.py
#! /usr/bin/env python

# System imports
from distutils.util import get_platform
import os
import sys
import unittest

# Import NumPy
import numpy as np
major, minor = [ int(d) for d in np.__version__.split(".")[:2] ]
if major == 0: BadListError = TypeError
else:          BadListError = ValueError

# Add the distutils-generated build directory to the python search path and then
# import the extension module
libDir = "lib.%s-%s" % (get_platform(), sys.version[:3])
sys.path.insert(0,os.path.join("build", libDir))
import Farray

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

class FarrayTestCase(unittest.TestCase):

    def setUp(self):
        self.nrows = 5
        self.ncols = 4
        self.array = Farray.Farray(self.nrows, self.ncols)

    def testConstructor1(self):
        "Test Farray size constructor"
        self.failUnless(isinstance(self.array, Farray.Farray))

    def testConstructor2(self):
        "Test Farray copy constructor"
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.array[i,j] = i + j
        arrayCopy = Farray.Farray(self.array)
        self.failUnless(arrayCopy == self.array)

    def testConstructorBad1(self):
        "Test Farray size constructor, negative nrows"
        self.assertRaises(ValueError, Farray.Farray, -4, 4)

    def testConstructorBad2(self):
        "Test Farray size constructor, negative ncols"
        self.assertRaises(ValueError, Farray.Farray, 4, -4)

    def testNrows(self):
        "Test Farray nrows method"
        self.failUnless(self.array.nrows() == self.nrows)

    def testNcols(self):
        "Test Farray ncols method"
        self.failUnless(self.array.ncols() == self.ncols)

    def testLen(self):
        "Test Farray __len__ method"
        self.failUnless(len(self.array) == self.nrows*self.ncols)

    def testSetGet(self):
        "Test Farray __setitem__, __getitem__ methods"
        m = self.nrows
        n = self.ncols
        for i in range(m):
            for j in range(n):
                self.array[i,j] = i*j
        for i in range(m):
            for j in range(n):
                self.failUnless(self.array[i,j] == i*j)

    def testSetBad1(self):
        "Test Farray __setitem__ method, negative row"
        self.assertRaises(IndexError, self.array.__setitem__, (-1, 3), 0)

    def testSetBad2(self):
        "Test Farray __setitem__ method, negative col"
        self.assertRaises(IndexError, self.array.__setitem__, (1, -3), 0)

    def testSetBad3(self):
        "Test Farray __setitem__ method, out-of-range row"
        self.assertRaises(IndexError, self.array.__setitem__, (self.nrows+1, 0), 0)

    def testSetBad4(self):
        "Test Farray __setitem__ method, out-of-range col"
        self.assertRaises(IndexError, self.array.__setitem__, (0, self.ncols+1), 0)

    def testGetBad1(self):
        "Test Farray __getitem__ method, negative row"
        self.assertRaises(IndexError, self.array.__getitem__, (-1, 3))

    def testGetBad2(self):
        "Test Farray __getitem__ method, negative col"
        self.assertRaises(IndexError, self.array.__getitem__, (1, -3))

    def testGetBad3(self):
        "Test Farray __getitem__ method, out-of-range row"
        self.assertRaises(IndexError, self.array.__getitem__, (self.nrows+1, 0))

    def testGetBad4(self):
        "Test Farray __getitem__ method, out-of-range col"
        self.assertRaises(IndexError, self.array.__getitem__, (0, self.ncols+1))

    def testAsString(self):
        "Test Farray asString method"
        result = """\
[ [ 0, 1, 2, 3 ],
  [ 1, 2, 3, 4 ],
  [ 2, 3, 4, 5 ],
  [ 3, 4, 5, 6 ],
  [ 4, 5, 6, 7 ] ]
"""
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.array[i,j] = i+j
        self.failUnless(self.array.asString() == result)

    def testStr(self):
        "Test Farray __str__ method"
        result = """\
[ [ 0, -1, -2, -3 ],
  [ 1, 0, -1, -2 ],
  [ 2, 1, 0, -1 ],
  [ 3, 2, 1, 0 ],
  [ 4, 3, 2, 1 ] ]
"""
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.array[i,j] = i-j
        self.failUnless(str(self.array) == result)

    def testView(self):
        "Test Farray view method"
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.array[i,j] = i+j
        a = self.array.view()
        self.failUnless(isinstance(a, np.ndarray))
        self.failUnless(a.flags.f_contiguous)
        for i in range(self.nrows):
            for j in range(self.ncols):
                self.failUnless(a[i,j] == i+j)

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

if __name__ == "__main__":

    # Build the test suite
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(FarrayTestCase))

    # Execute the test suite
    print "Testing Classes of Module Farray"
    print "NumPy version", np.__version__
    print
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(len(result.errors) + len(result.failures))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.