Changeset 13574


Ignore:
Timestamp:
Sep 23, 2019 11:13:28 AM (3 months ago)
Author:
randersson
Message:

#5819 Updated test to no longer use nose and use numpy instead due to bug with nose calling function round with invalid data types. This error does not occur with python 2 but does with python 3. Also updated all assert a == b with a proper error message in case test fails in future.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/dev-5819/Python/src/tests_jmodelica/test_core.py

    r10318 r13574  
    22# -*- coding: utf-8 -*-
    33
    4 # Copyright (C) 2010 Modelon AB
     4# Copyright (C) 2019 Modelon AB
    55#
    66# This program is free software: you can redistribute it and/or modify
     
    2222import os, os.path
    2323import sys
    24 
    25 import nose
    26 import nose.tools
     24import numpy as np
    2725
    2826from tests_jmodelica import testattr, get_files_path
     
    3230class Test_TrajectoryConstantInterpolationExtrapolation:
    3331   
     32    def _assert(self, result, expected, assert_index, n_asserts):
     33        err_msg = "Failed in assert {} of {}, got {} instead of {}".format(result, expected, assert_index, n_asserts)
     34        assert result == expected, err_msg
     35
    3436    @testattr(stddist_full = True)
    3537    def test_one_dimensional(self):
    36         t = N.array([1,2,3,4,5])
    37         x = N.array([[2,2.5,4,5,9]]).transpose()
     38        t = np.array([1,2,3,4,5])
     39        x = np.array([[2,2.5,4,5,9]]).transpose()
    3840       
    3941        interp = TrajectoryConstantInterpolationExtrapolation(t,x)
    4042       
    41         assert interp.eval(0)[:,0] == 2.0
    42         assert interp.eval(6)[:,0] == 9.0
    43         assert interp.eval(1.5)[:,0] == 2.0
    44         assert interp.eval(2.6)[:,0] == 2.5
     43        n_tests = 16
     44        self._assert(interp.eval(0)[:,0], 2.0, 1, n_tests)
     45        self._assert(interp.eval(6)[:,0], 9.0, 2, n_tests)
     46        self._assert(interp.eval(1.5)[:,0], 2.0, 3, n_tests)
     47        self._assert(interp.eval(2.6)[:,0], 2.5, 4, n_tests)
    4548       
    46         assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.0
    47         assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.0
    48         assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 2.5
    49         assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.0
     49        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 5, n_tests)
     50        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.0, 6, n_tests)
     51        self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 2.5, 7, n_tests)
     52        self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 8, n_tests)
    5053       
    5154        #Change mode
    5255        interp.set_mode("Backward")
    5356       
    54         assert interp.eval(0)[:,0] == 2.0
    55         assert interp.eval(6)[:,0] == 9.0
    56         assert interp.eval(1.5)[:,0] == 2.5
    57         assert interp.eval(2.6)[:,0] == 4.0
     57        self._assert(interp.eval(0)[:,0], 2.0, 9, n_tests)
     58        self._assert(interp.eval(6)[:,0], 9.0, 10, n_tests)
     59        self._assert(interp.eval(1.5)[:,0], 2.5, 11, n_tests)
     60        self._assert(interp.eval(2.6)[:,0], 4.0, 12, n_tests)
    5861       
    59         assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.0
    60         assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.5
    61         assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 4.0
    62         assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.0
     62        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 13, n_tests)
     63        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.5, 14, n_tests)
     64        self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 4.0, 15, n_tests)
     65        self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 16, n_tests)
    6366       
    6467    @testattr(stddist_full = True)
    6568    def test_two_dimensional(self):
    66         t = N.array([1,2,3,4,5])
    67         x = N.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose()
     69        t = np.array([1,2,3,4,5])
     70        x = np.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose()
    6871       
    6972        interp = TrajectoryConstantInterpolationExtrapolation(t,x)
    7073       
    71         assert interp.eval(0)[0,0] == 2.0
    72         assert interp.eval(6)[0,0] == 9.0
    73         assert interp.eval(1.5)[0,0] == 2.0
    74         assert interp.eval(2.6)[0,0] == 2.5
    75         assert interp.eval(0)[0,1] == 1.0
    76         assert interp.eval(6)[0,1] == 8.0
    77         assert interp.eval(1.5)[0,1] == 1.0
    78         assert interp.eval(2.6)[0,1] == 1.5
     74        n_tests = 31
     75        self._assert(interp.eval(0)[0,0], 2.0, 1, n_tests)
     76        self._assert(interp.eval(6)[0,0], 9.0, 2, n_tests)
     77        self._assert(interp.eval(1.5)[0,0], 2.0, 3, n_tests)
     78        self._assert(interp.eval(2.6)[0,0], 2.5, 3, n_tests)
     79        self._assert(interp.eval(0)[0,1], 1.0, 4, n_tests)
     80        self._assert(interp.eval(6)[0,1], 8.0, 5, n_tests)
     81        self._assert(interp.eval(1.5)[0,1], 1.0, 6, n_tests)
     82        self._assert(interp.eval(2.6)[0,1], 1.5, 7, n_tests)
    7983       
    80         assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.0
    81         assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.0
    82         assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 2.5
    83         assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.0
    84         assert interp.eval([0, 1.5, 2.6, 6])[0,1] == 1.0
    85         assert interp.eval([0, 1.5, 2.6, 6])[1,1] == 1.0
    86         assert interp.eval([0, 1.5, 2.6, 6])[2,1] == 1.5
    87         assert interp.eval([0, 1.5, 2.6, 6])[3,1] == 8.0
     84        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 8, n_tests)
     85        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.0, 9, n_tests)
     86        self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 2.5, 10, n_tests)
     87        self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 11, n_tests)
     88        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,1], 1.0, 12, n_tests)
     89        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,1], 1.0, 13, n_tests)
     90        self._assert(interp.eval([0, 1.5, 2.6, 6])[2,1], 1.5, 14, n_tests)
     91        self._assert(interp.eval([0, 1.5, 2.6, 6])[3,1], 8.0, 15, n_tests)
    8892       
    8993        #Change mode
    9094        interp.set_mode("Backward")
    9195       
    92         assert interp.eval(0)[0,0] == 2.0
    93         assert interp.eval(6)[0,0] == 9.0
    94         assert interp.eval(1.5)[0,0] == 2.5
    95         assert interp.eval(2.6)[0,0] == 4.0
    96         assert interp.eval(0)[0,1] == 1.0
    97         assert interp.eval(6)[0,1] == 8.0
    98         assert interp.eval(1.5)[0,1] == 1.5
    99         assert interp.eval(2.6)[0,1] == 3.0
     96        self._assert(interp.eval(0)[0,0], 2.0, 16, n_tests)
     97        self._assert(interp.eval(6)[0,0], 9.0, 17, n_tests)
     98        self._assert(interp.eval(1.5)[0,0], 2.5, 18, n_tests)
     99        self._assert(interp.eval(2.6)[0,0], 4.0, 19, n_tests)
     100        self._assert(interp.eval(0)[0,1], 1.0, 20, n_tests)
     101        self._assert(interp.eval(6)[0,1], 8.0, 21, n_tests)
     102        self._assert(interp.eval(1.5)[0,1], 1.5, 22, n_tests)
     103        self._assert(interp.eval(2.6)[0,1], 3.0, 23, n_tests)
    100104       
    101         assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 2.0
    102         assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.5
    103         assert interp.eval([0, 1.5, 2.6, 6])[2,0] == 4.0
    104         assert interp.eval([0, 1.5, 2.6, 6])[3,0] == 9.0
    105         assert interp.eval([0, 1.5, 2.6, 6])[0,1] == 1.0
    106         assert interp.eval([0, 1.5, 2.6, 6])[1,1] == 1.5
    107         assert interp.eval([0, 1.5, 2.6, 6])[2,1] == 3.0
    108         assert interp.eval([0, 1.5, 2.6, 6])[3,1] == 8.0
     105        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 2.0, 24, n_tests)
     106        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.5, 25, n_tests)
     107        self._assert(interp.eval([0, 1.5, 2.6, 6])[2,0], 4.0, 26, n_tests)
     108        self._assert(interp.eval([0, 1.5, 2.6, 6])[3,0], 9.0, 27, n_tests)
     109        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,1], 1.0, 28, n_tests)
     110        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,1], 1.5, 29, n_tests)
     111        self._assert(interp.eval([0, 1.5, 2.6, 6])[2,1], 3.0, 30, n_tests)
     112        self._assert(interp.eval([0, 1.5, 2.6, 6])[3,1], 8.0, 31, n_tests)
    109113
    110114
    111115class Test_TrajectoryLinearInterpolationExtrapolation:
    112116   
     117    def _assert(self, result, expected, assert_index, n_asserts):
     118        err_msg = "Failed in assert {} of {}, got {} instead of {}".format(result, expected, assert_index, n_asserts)
     119        assert result == expected, err_msg
     120   
    113121    @testattr(stddist_full = True)
    114122    def test_one_dimensional(self):
    115         t = N.array([1,2,3,4,5])
    116         x = N.array([[2,2.5,4,5,9]]).transpose()
     123       
     124        t = np.array([1,2,3,4,5])
     125        x = np.array([[2,2.5,4,5,9]]).transpose()
     126       
     127        interp = TrajectoryLinearInterpolationExtrapolation(t,x)
     128        n_tests = 8
     129        self._assert(interp.eval(0)[:,0], 1.5, 1, n_tests)
     130        self._assert(interp.eval(6)[:,0], 13.0, 2, n_tests)
     131        self._assert(interp.eval(1.5)[:,0], 2.25, 3, n_tests)
     132        np.testing.assert_almost_equal(interp.eval(2.6)[:,0], 3.4)
     133       
     134        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 1.5, 5, n_tests)
     135        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.25, 6, n_tests)
     136        np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0], 3.4)
     137        np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0)
     138       
     139    @testattr(stddist_full = True)
     140    def test_two_dimensional(self):
     141        t = np.array([1,2,3,4,5])
     142        x = np.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose()
    117143       
    118144        interp = TrajectoryLinearInterpolationExtrapolation(t,x)
    119145       
    120         assert interp.eval(0)[:,0] == 1.5
    121         assert interp.eval(6)[:,0] == 13.0
    122         assert interp.eval(1.5)[:,0] == 2.25
    123         nose.tools.assert_almost_equal(interp.eval(2.6)[:,0],3.4)
     146        n_tests = 16
     147        self._assert(interp.eval(0)[:,0], 1.5, 1, n_tests)
     148        self._assert(interp.eval(6)[:,0], 13.0, 1, n_tests)
     149        self._assert(interp.eval(1.5)[:,0], 2.25, 1, n_tests)
     150        np.testing.assert_almost_equal(interp.eval(2.6)[:,0],3.4)
     151        self._assert(interp.eval(0)[:,1], 0.5, 1, n_tests)
     152        self._assert(interp.eval(6)[:,1], 12.0, 1, n_tests)
     153        self._assert(interp.eval(1.5)[:,1], 1.25, 1, n_tests)
     154        np.testing.assert_almost_equal(interp.eval(2.6)[:,1],2.4)
    124155       
    125         assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 1.5
    126         assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.25
    127         nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0],3.4)
    128         nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0)
    129        
    130     @testattr(stddist_full = True)
    131     def test_two_dimensional(self):
    132         t = N.array([1,2,3,4,5])
    133         x = N.array([[2,2.5,4,5,9], [1,1.5,3,4,8]]).transpose()
    134        
    135         interp = TrajectoryLinearInterpolationExtrapolation(t,x)
    136        
    137         assert interp.eval(0)[:,0] == 1.5
    138         assert interp.eval(6)[:,0] == 13.0
    139         assert interp.eval(1.5)[:,0] == 2.25
    140         nose.tools.assert_almost_equal(interp.eval(2.6)[:,0],3.4)
    141         assert interp.eval(0)[:,1] == 0.5
    142         assert interp.eval(6)[:,1] == 12.0
    143         assert interp.eval(1.5)[:,1] == 1.25
    144         nose.tools.assert_almost_equal(interp.eval(2.6)[:,1],2.4)
    145        
    146         assert interp.eval([0, 1.5, 2.6, 6])[0,0] == 1.5
    147         assert interp.eval([0, 1.5, 2.6, 6])[1,0] == 2.25
    148         nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0],3.4)
    149         nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0)
    150         assert interp.eval([0, 1.5, 2.6, 6])[0,1] == 0.5
    151         assert interp.eval([0, 1.5, 2.6, 6])[1,1] == 1.25
    152         nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,1],2.4)
    153         nose.tools.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,1],12.0)
     156        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,0], 1.5, 1, n_tests)
     157        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,0], 2.25, 1, n_tests)
     158        np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,0],3.4)
     159        np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,0],13.0)
     160        self._assert(interp.eval([0, 1.5, 2.6, 6])[0,1], 0.5, 1, n_tests)
     161        self._assert(interp.eval([0, 1.5, 2.6, 6])[1,1], 1.25, 1, n_tests)
     162        np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[2,1],2.4)
     163        np.testing.assert_almost_equal(interp.eval([0, 1.5, 2.6, 6])[3,1],12.0)
Note: See TracChangeset for help on using the changeset viewer.