def test_distance(self): #Test that zeros work p1 = [1, 1, 1] p2 = [0, 0, 0] expected = 3**0.5 actual = Math.distance(p1, p2) self.assertAlmostEqual(expected, actual, 5) #Test that unequal size vectors fail p1 = [1, 1, 1] p2 = [0, 0] expected = 2**0.5 with self.assertRaises(IndexError): actual = Math.distance(p1, p2) #Test positive/negative mixing p1 = [0, 5.5] p2 = [0, -7.5] expected = 13.0 actual = Math.distance(p1, p2) self.assertAlmostEqual(expected, actual, 5) #Test orthogonal vectors (not that this should matter tbh) p1 = [0, 1] p2 = [1, 0] expected = 2**0.5 actual = Math.distance(p1, p2) self.assertAlmostEqual(expected, actual, 5)
def test_rotate(self): ox = oy = py = 0 px = 1 theta = Math.PI / 2 expected = (0, 1) actual = Math.rotate(ox, oy, px, py, theta) self.assertAlmostEqualSequence(expected, actual, 5)
def test_iwrap(self): #Test negative wrapping x = -1 M = 10 expected = 9 actual = Math.iwrap(x, M) self.assertEqual(expected, actual) #Test Positive wrapping x = 12 M = 10 expected = 2 actual = Math.iwrap(x, M) self.assertEqual(expected, actual) #Test in-range wrapping x = 3 M = 10 expected = 3 actual = Math.iwrap(x, M) self.assertEqual(expected, actual)
def test_fwrap(self): #Test negative wrapping (all floats) eps = 1E-3 x = -2.0 m = 4.0 M = 8.0 expected = 6.0 actual = Math.fwrap(x, m, M) self.assertAlmostEqual(expected, actual, 3) #Test pos val, below min wrapping x = 3.5 m = 5.0 M = 8.0 expected = 6.5 actual = Math.fwrap(x, m, M) self.assertAlmostEqual(expected, actual, 3) #Test neg val above max wrapping x = -8.5 m = -4.0 M = -7.0 expected = -5.5 actual = Math.fwrap(x, m, M) self.assertAlmostEqual(expected, actual, 3) #Test pos val above max wrapping (all floats) eps = 1E-3 x = 8.5 m = 4.0 M = 8.0 expected = 4.5 actual = Math.fwrap(x, m, M) self.assertAlmostEqual(expected, actual, 3) #Test in-range wrapping (all floats) eps = 1E-3 x = 3.75 m = 2.15 M = 8.35 expected = 3.75 actual = Math.fwrap(x, m, M) self.assertAlmostEqual(expected, actual, 3)
def test_unit(self): #Test quad 1 v = (1, 1) expected = (2**-0.5, 2**-0.5) actual = Math.unit(*v) self.assertAlmostEqualSequence(expected, actual, 5) #Test quad 2 v = (-1, 1) expected = (-(2**-0.5), 2**-0.5) actual = Math.unit(*v) self.assertAlmostEqualSequence(expected, actual, 5) #Test vertical v = (0, 5) expected = (0, 1) actual = Math.unit(*v) self.assertAlmostEqualSequence(expected, actual, 5) #Test horizontal v = (-4, 0) expected = (-1, 0) actual = Math.unit(*v) self.assertAlmostEqualSequence(expected, actual, 5)
def test_normalize(self): #Test on single value base = [4.5] expected = [1.0] actual = Math.normalize(base) self.assertAlmostEqualSequence(expected, actual, 5) #Test on roughly (isZero) equal values base = [4.5, 4.50000000001, 4.4999999999999] expected = [1.0 / len(base)] * len(base) actual = Math.normalize(base) self.assertAlmostEqualSequence(expected, actual, 5) #Test on negatives base = [-4.0, -4.0] expected = [1.0 / len(base)] * len(base) actual = Math.normalize(base) self.assertAlmostEqualSequence(expected, actual, 5) #Test on positives base = [2.0, 1.5, 1.0] expected = [1.0, 0.5, 0.0] actual = Math.normalize(base) self.assertAlmostEqualSequence(expected, actual, 5)
def test_limit_vector(self): #Check in-range values v = (-4, 0, 4) expected = (-4, 0) actual = Math.limit_vector(*v) self.assertAlmostEqualSequence(expected, actual, 5) #Check out-of range positives, and vertical vectors v = (10, 0, 3.5) expected = (3.5, 0) actual = Math.limit_vector(*v) self.assertAlmostEqualSequence(expected, actual, 5) #Check out-of-range negatives and multi-direction v = (-4, -4, 1) expected = (-(2**-0.5), -(2**-0.5)) actual = Math.limit_vector(*v) self.assertAlmostEqualSequence(expected, actual, 5) #Check negative mags raise Error v = (-4, 0, -3) expected = (-4, 0) with self.assertRaises(ArithmeticError): actual = Math.limit_vector(*v)
def test_clamp(self): #Test < min v = 1.0 vmin = 3.0 vmax = 5.0 expected = 3.0 actual = Math.clamp(v, vmin, vmax) self.assertAlmostEqual(expected, actual, 3) #Test > max v = 8.0 vmin = 3.0 vmax = 5.0 expected = 5.0 actual = Math.clamp(v, vmin, vmax) self.assertAlmostEqual(expected, actual, 3) #Test in range v = 4.0 vmin = 3.0 vmax = 5.0 expected = 4.0 actual = Math.clamp(v, vmin, vmax) self.assertAlmostEqual(expected, actual, 3)
def test_is_zero(self): #Test default precision self.assertTrue(Math.is_zero(0.000000009)) #Test fail default precision self.assertFalse(Math.is_zero(0.000000011)) #Test low-precision self.assertTrue(Math.is_zero(0.001, 2)) self.assertTrue(Math.is_zero(0.01, 1)) #Test negatives self.assertTrue(Math.is_zero(-1.5E-9)) #Test positives self.assertTrue(Math.is_zero(-1.5E-9))
#coding=utf-8 import sys reload(sys) sys.setdefaultencoding("utf8") from Util import Math u = Math() import db from Table import Role from Table import User from Table import Calc db = db.db db.drop_all() db.create_all() role_admin = Role() role_mod = Role() role_user = Role() user_john = User(username='******', role=role_admin) user_susan = User(username='******', role=role_user) user_david = User(username='******', role=role_user) db.session.add(role_admin) db.session.add(role_mod) db.session.add(role_user) db.session.add(user_john) db.session.add(user_susan) db.session.add(user_david)
from flask import redirect from flask import render_template from flask import request from flask import url_for from flask.ext.sqlalchemy import SQLAlchemy from Form import CalcForm from Util import Math # 这段代码是用于解决中文报错的问题 reload(sys) sys.setdefaultencoding("utf8") # 配置Flask app = Flask(__name__) u = Math() # 配置数据库 basedir = os.path.abspath(os.path.dirname(__file__)) SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'test.sqlite') app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True db = SQLAlchemy(app) #创建表 class Calc(db.Model): __tablename__ = 'calcs' id = db.Column(db.Integer, primary_key=True) w = db.Column(db.Float) h = db.Column(db.Float)
def check(a, b, t, e): self.assertAlmostEqual(Math.lerp(a, b, t), e, 3)
def check(a, b, r): self.assertEqual(Math.gcd(a, b), r)