Skip to content

Commit

Permalink
Merge 20ea7ce into 0f15f30
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAFile authored Oct 10, 2018
2 parents 0f15f30 + 20ea7ce commit 5f204fa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 36 deletions.
8 changes: 5 additions & 3 deletions pyspades/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ cdef inline object allocate_memory(int size, char ** i):
return ob

cdef extern from "common_c.h":
struct Vector:
cdef cppclass Vector:
float x, y, z
Vector()
Vector(float x, float y, float z)
void set(float x, float y, float z)

struct LongVector:
int x, y, z

Vector * create_vector(float x, float y, float z)
void destroy_vector(Vector*)

cdef inline int check_default_int(int value, int default) except -1:
Expand Down Expand Up @@ -65,6 +67,6 @@ cdef inline Vertex3 create_proxy_vector(Vector * v):
cdef inline Vertex3 create_vertex3(float x, float y, float z):
# faster way of creating Vertex3 instances
cdef Vertex3 new_vertex = Vertex3(is_ref = True)
new_vertex.value = create_vector(x, y, z)
new_vertex.value = new Vector(x, y, z)
new_vertex.is_ref = False
return new_vertex
12 changes: 6 additions & 6 deletions pyspades/common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,19 @@ def escape_control_codes(untrusted_str):
cdef class Vertex3:
# NOTE: for the most part this behaves as a 2d vector, with z being tacked on
# so it's useful for orientation math

def __init__(self, *arg, is_ref = False):
cdef float x, y, z
if not is_ref:
if arg:
x, y, z = arg
else:
x = y = z = 0.0
self.value = create_vector(x, y, z)
self.value = new Vector(x, y, z)
self.is_ref = is_ref

def __dealloc__(self):
if not self.is_ref:
destroy_vector(self.value)
del self.value
self.value = NULL

def copy(self):
Expand Down Expand Up @@ -166,7 +165,7 @@ cdef class Vertex3:
cdef Vector * a = (<Vertex3>self).value
return create_vertex3(a.x * k, a.y * k, a.z * k)

def __div__(self, float k):
def __truediv__(self, float k):
cdef Vector * a = (<Vertex3>self).value
return create_vertex3(a.x / k, a.y / k, a.z / k)

Expand Down Expand Up @@ -298,9 +297,10 @@ cdef class Vertex3:
cdef Vector * a = self.value
return create_vertex3(+a.x, +a.y, +a.z)

def __str__(self):
def __repr__(self):
cdef Vector * a = self.value
return "(%s %s %s)" % (a.x, a.y, a.z)
# "5 decimal places ought to be enough for anyone (tm)"
return "Vertex3({:.5f}, {:.5f}, {:.5f})".format(a.x, a.y, a.z)

# properties for python wrapper
property x:
Expand Down
32 changes: 15 additions & 17 deletions pyspades/common_c.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
#ifndef COMMON_C_H
#define COMMON_C_H
// vi: set ft=cpp
#pragma once

struct Vector
class Vector
{
public:
float x, y, z;

Vector() {
x = 0.0, y =0.0, z = 0.0;
}
Vector(float x, float y, float z)
: x(x), y(y), z(z) {}
void set(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
};

struct LongVector
{
long x, y, z;
};

inline Vector * create_vector(float x, float y, float z)
{
Vector * v = new Vector;
v->x = x; v->y = y; v->z = z;
return v;
}

inline void destroy_vector(Vector * v)
{
delete v;
}

#endif /* COMMON_C_H */
91 changes: 81 additions & 10 deletions tests/pyspades/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,99 @@
test pyspades/common.pyx
"""

import math

from twisted.trial import unittest

from pyspades.common import escape_control_codes, get_color, to_coordinates, coordinates
from pyspades import common


def assert_math_isclose(first, second, rel_tol=1e-09, abs_tol=0.0, msg=None):
if math.isclose(first, second, rel_tol=rel_tol, abs_tol=abs_tol):
return
standardMsg = (
'{!r} != {!r} with relative tolerance of {:.3g}'
' and absolute tolerance of {:.3g}'
).format(first, second, rel_tol, abs_tol)
if msg:
raise AssertionError("{} : {}".format(standardMsg, msg))
else:
raise AssertionError(standardMsg)

class TestCommonThings(unittest.TestCase):

def test_get_color(self):
self.assertEqual(get_color(0xFFFFFF), (0xFF, 0xFF, 0xFF))
self.assertEqual(common.get_color(0xFFFFFF), (0xFF, 0xFF, 0xFF))

def test_to_coords(self):
self.assertEqual(to_coordinates(511, 511), "H8")
self.assertEqual(common.to_coordinates(511, 511), "H8")

def test_from_coords(self):
self.assertEqual(coordinates("H8"), (448, 448))
self.assertEqual(common.coordinates("H8"), (448, 448))

def test_escape_control_codes(self):
test_cases = [
("\x1b[6;30;42mGreen!\x1b[0m", "\\x1b[6;30;42mGreen!\\x1b[0m"), # ANSI
("\x1b[6;30;42mGreen世界!\x1b[0m", "\\x1b[6;30;42mGreen世界!\\x1b[0m"), # ANSI with utf-8
("hello\n\t","hello\\n\\t"), # acii controll codes
("hello ", "hello "), # normal
("世界", "世界") # normal utf-8
("\x1b[6;30;42mGreen!\x1b[0m",
"\\x1b[6;30;42mGreen!\\x1b[0m"), # ANSI
("\x1b[6;30;42mGreen世界!\x1b[0m",
"\\x1b[6;30;42mGreen世界!\\x1b[0m"), # ANSI with utf-8
("hello\n\t", "hello\\n\\t"), # acii controll codes
("hello ", "hello "), # normal
("世界", "世界") # normal utf-8
]
for unescaped, want in test_cases:
self.assertEqual(escape_control_codes(unescaped), want)
self.assertEqual(common.escape_control_codes(unescaped), want)


class TestVertex3(unittest.TestCase):
def test_basics(self):
v = common.Vertex3(10, 20, 30)
self.assertEqual(v.x, 10)
self.assertEqual(v.y, 20)
self.assertEqual(v.z, 30)
self.assertEqual(v.get(), (10, 20, 30))
v.set(40, 50, 60)
self.assertEqual(v.get(), (40, 50, 60))

def test_basic_arithmetic(self):
v_1 = common.Vertex3(10, 20, 30)
v_2 = common.Vertex3(10, 1, 5)

self.assertEqual((v_1 - v_2).get(), (0, 19, 25))
self.assertEqual((v_1 + v_2).get(), (20, 21, 35))
self.assertEqual((v_1 * 10).get(), (100, 200, 300))
self.assertEqual((v_1 / 10).get(), (1, 2, 3))

v_1 += v_2
self.assertEqual(v_1.get(), (20, 21, 35))

v_1 -= v_2
self.assertEqual(v_1.get(), (10, 20, 30))

v_1 *= 10
self.assertEqual(v_1.get(), (100, 200, 300))

v_1 /= 10
self.assertEqual(v_1.get(), (10, 20, 30))

v_1.translate(10, 20, 30)
self.assertEqual(v_1.get(), (20, 40, 60))

def test_vector_ops(self):
v_1 = common.Vertex3(10, 20, 30)
v_2 = common.Vertex3(10, 1, 5)

self.assertEqual(v_1.dot(v_2), 120)
self.assertEqual(v_1.perp_dot(v_2), -190)
v_1.rotate(v_2)
self.assertEqual(v_1.get(), (80, 210, 30))
v_1.unrotate(v_2)
self.assertEqual(v_1.get(), (1010, 2020, 30))

v_3 = common.Vertex3(0, -10, 0)
self.assertEqual(v_3.length(), 10)
v_4 = common.Vertex3(10, -10.5, 500)
assert_math_isclose(v_4.length(), 500.210205078125)
self.assertEqual(v_3.normal().get(), (0, -1, 0))
v_3.normalize()
self.assertEqual(v_3.get(), (0, -1, 0))

0 comments on commit 5f204fa

Please sign in to comment.