summaryrefslogtreecommitdiffstats
path: root/ml/dlib/tools/python/test
diff options
context:
space:
mode:
Diffstat (limited to 'ml/dlib/tools/python/test')
-rw-r--r--ml/dlib/tools/python/test/.gitignore1
-rw-r--r--ml/dlib/tools/python/test/test_array.py107
-rw-r--r--ml/dlib/tools/python/test/test_global_optimization.py69
-rw-r--r--ml/dlib/tools/python/test/test_matrix.py100
-rw-r--r--ml/dlib/tools/python/test/test_point.py48
-rw-r--r--ml/dlib/tools/python/test/test_range.py97
-rw-r--r--ml/dlib/tools/python/test/test_rgb_pixel.py26
-rw-r--r--ml/dlib/tools/python/test/test_sparse_vector.py101
-rw-r--r--ml/dlib/tools/python/test/test_svm_c_trainer.py65
-rw-r--r--ml/dlib/tools/python/test/test_vector.py170
10 files changed, 784 insertions, 0 deletions
diff --git a/ml/dlib/tools/python/test/.gitignore b/ml/dlib/tools/python/test/.gitignore
new file mode 100644
index 000000000..bee8a64b7
--- /dev/null
+++ b/ml/dlib/tools/python/test/.gitignore
@@ -0,0 +1 @@
+__pycache__
diff --git a/ml/dlib/tools/python/test/test_array.py b/ml/dlib/tools/python/test/test_array.py
new file mode 100644
index 000000000..479997ac3
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_array.py
@@ -0,0 +1,107 @@
+from dlib import array
+try:
+ import cPickle as pickle # Use cPickle on Python 2.7
+except ImportError:
+ import pickle
+
+try:
+ from types import FloatType
+except ImportError:
+ FloatType = float
+
+from pytest import raises
+
+
+def test_array_init_with_number():
+ a = array(5)
+ assert len(a) == 5
+ for i in range(5):
+ assert a[i] == 0
+ assert type(a[i]) == FloatType
+
+
+def test_array_init_with_negative_number():
+ with raises(Exception):
+ array(-5)
+
+
+def test_array_init_with_zero():
+ a = array(0)
+ assert len(a) == 0
+
+
+def test_array_init_with_list():
+ a = array([0, 1, 2, 3, 4])
+ assert len(a) == 5
+ for idx, val in enumerate(a):
+ assert idx == val
+ assert type(val) == FloatType
+
+
+def test_array_init_with_empty_list():
+ a = array([])
+ assert len(a) == 0
+
+
+def test_array_init_without_argument():
+ a = array()
+ assert len(a) == 0
+
+
+def test_array_init_with_tuple():
+ a = array((0, 1, 2, 3, 4))
+ for idx, val in enumerate(a):
+ assert idx == val
+ assert type(val) == FloatType
+
+
+def test_array_serialization_empty():
+ a = array()
+ # cPickle with protocol 2 required for Python 2.7
+ # see http://pybind11.readthedocs.io/en/stable/advanced/classes.html#custom-constructors
+ ser = pickle.dumps(a, 2)
+ deser = pickle.loads(ser)
+ assert a == deser
+
+
+def test_array_serialization():
+ a = array([0, 1, 2, 3, 4])
+ ser = pickle.dumps(a, 2)
+ deser = pickle.loads(ser)
+ assert a == deser
+
+
+def test_array_extend():
+ a = array()
+ a.extend([0, 1, 2, 3, 4])
+ assert len(a) == 5
+ for idx, val in enumerate(a):
+ assert idx == val
+ assert type(val) == FloatType
+
+
+def test_array_string_representations_empty():
+ a = array()
+ assert str(a) == ""
+ assert repr(a) == "array[]"
+
+
+def test_array_string_representations():
+ a = array([1, 2, 3])
+ assert str(a) == "1\n2\n3"
+ assert repr(a) == "array[1, 2, 3]"
+
+
+def test_array_clear():
+ a = array(10)
+ a.clear()
+ assert len(a) == 0
+
+
+def test_array_resize():
+ a = array(10)
+ a.resize(100)
+ assert len(a) == 100
+
+ for i in range(100):
+ assert a[i] == 0
diff --git a/ml/dlib/tools/python/test/test_global_optimization.py b/ml/dlib/tools/python/test/test_global_optimization.py
new file mode 100644
index 000000000..ec320909f
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_global_optimization.py
@@ -0,0 +1,69 @@
+from dlib import find_max_global, find_min_global
+import dlib
+from pytest import raises
+from math import sin,cos,pi,exp,sqrt,pow
+
+
+def test_global_optimization_nargs():
+ w0 = find_max_global(lambda *args: sum(args), [0, 0, 0], [1, 1, 1], 10)
+ w1 = find_min_global(lambda *args: sum(args), [0, 0, 0], [1, 1, 1], 10)
+ assert w0 == ([1, 1, 1], 3)
+ assert w1 == ([0, 0, 0], 0)
+
+ w2 = find_max_global(lambda a, b, c, *args: a + b + c - sum(args), [0, 0, 0], [1, 1, 1], 10)
+ w3 = find_min_global(lambda a, b, c, *args: a + b + c - sum(args), [0, 0, 0], [1, 1, 1], 10)
+ assert w2 == ([1, 1, 1], 3)
+ assert w3 == ([0, 0, 0], 0)
+
+ with raises(Exception):
+ find_max_global(lambda a, b: 0, [0, 0, 0], [1, 1, 1], 10)
+ with raises(Exception):
+ find_min_global(lambda a, b: 0, [0, 0, 0], [1, 1, 1], 10)
+ with raises(Exception):
+ find_max_global(lambda a, b, c, d, *args: 0, [0, 0, 0], [1, 1, 1], 10)
+ with raises(Exception):
+ find_min_global(lambda a, b, c, d, *args: 0, [0, 0, 0], [1, 1, 1], 10)
+
+
+def F(a,b):
+ return -pow(a-2,2.0) - pow(b-4,2.0);
+def G(x):
+ return 2-pow(x-5,2.0);
+
+def test_global_function_search():
+ spec_F = dlib.function_spec([-10,-10], [10,10])
+ spec_G = dlib.function_spec([-2], [6])
+
+ opt = dlib.global_function_search([spec_F, spec_G])
+
+ for i in range(15):
+ next = opt.get_next_x()
+ #print("next x is for function {} and has coordinates {}".format(next.function_idx, next.x))
+
+ if (next.function_idx == 0):
+ a = next.x[0]
+ b = next.x[1]
+ next.set(F(a,b))
+ else:
+ x = next.x[0]
+ next.set(G(x))
+
+ [x,y,function_idx] = opt.get_best_function_eval()
+
+ #print("\nbest function was {}, with y of {}, and x of {}".format(function_idx,y,x))
+
+ assert(abs(y-2) < 1e-7)
+ assert(abs(x[0]-5) < 1e-7)
+ assert(function_idx==1)
+
+
+
+def holder_table(x0,x1):
+ return -abs(sin(x0)*cos(x1)*exp(abs(1-sqrt(x0*x0+x1*x1)/pi)))
+
+def test_on_holder_table():
+ x,y = find_min_global(holder_table,
+ [-10,-10],
+ [10,10],
+ 200)
+ assert (y - -19.2085025679) < 1e-7
diff --git a/ml/dlib/tools/python/test/test_matrix.py b/ml/dlib/tools/python/test/test_matrix.py
new file mode 100644
index 000000000..cdd9bed13
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_matrix.py
@@ -0,0 +1,100 @@
+from dlib import matrix
+try:
+ import cPickle as pickle # Use cPickle on Python 2.7
+except ImportError:
+ import pickle
+from pytest import raises
+
+try:
+ import numpy
+ have_numpy = True
+except ImportError:
+ have_numpy = False
+
+
+def test_matrix_empty_init():
+ m = matrix()
+ assert m.nr() == 0
+ assert m.nc() == 0
+ assert m.shape == (0, 0)
+ assert len(m) == 0
+ assert repr(m) == "< dlib.matrix containing: >"
+ assert str(m) == ""
+
+
+def test_matrix_from_list():
+ m = matrix([[0, 1, 2],
+ [3, 4, 5],
+ [6, 7, 8]])
+ assert m.nr() == 3
+ assert m.nc() == 3
+ assert m.shape == (3, 3)
+ assert len(m) == 3
+ assert repr(m) == "< dlib.matrix containing: \n0 1 2 \n3 4 5 \n6 7 8 >"
+ assert str(m) == "0 1 2 \n3 4 5 \n6 7 8"
+
+ deser = pickle.loads(pickle.dumps(m, 2))
+
+ for row in range(3):
+ for col in range(3):
+ assert m[row][col] == deser[row][col]
+
+
+def test_matrix_from_list_with_invalid_rows():
+ with raises(ValueError):
+ matrix([[0, 1, 2],
+ [3, 4],
+ [5, 6, 7]])
+
+
+def test_matrix_from_list_as_column_vector():
+ m = matrix([0, 1, 2])
+ assert m.nr() == 3
+ assert m.nc() == 1
+ assert m.shape == (3, 1)
+ assert len(m) == 3
+ assert repr(m) == "< dlib.matrix containing: \n0 \n1 \n2 >"
+ assert str(m) == "0 \n1 \n2"
+
+
+if have_numpy:
+ def test_matrix_from_object_with_2d_shape():
+ m1 = numpy.array([[0, 1, 2],
+ [3, 4, 5],
+ [6, 7, 8]])
+ m = matrix(m1)
+ assert m.nr() == 3
+ assert m.nc() == 3
+ assert m.shape == (3, 3)
+ assert len(m) == 3
+ assert repr(m) == "< dlib.matrix containing: \n0 1 2 \n3 4 5 \n6 7 8 >"
+ assert str(m) == "0 1 2 \n3 4 5 \n6 7 8"
+
+
+ def test_matrix_from_object_without_2d_shape():
+ with raises(IndexError):
+ m1 = numpy.array([0, 1, 2])
+ matrix(m1)
+
+
+def test_matrix_from_object_without_shape():
+ with raises(AttributeError):
+ matrix("invalid")
+
+
+def test_matrix_set_size():
+ m = matrix()
+ m.set_size(5, 5)
+
+ assert m.nr() == 5
+ assert m.nc() == 5
+ assert m.shape == (5, 5)
+ assert len(m) == 5
+ assert repr(m) == "< dlib.matrix containing: \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 >"
+ assert str(m) == "0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0 \n0 0 0 0 0"
+
+ deser = pickle.loads(pickle.dumps(m, 2))
+
+ for row in range(5):
+ for col in range(5):
+ assert m[row][col] == deser[row][col]
diff --git a/ml/dlib/tools/python/test/test_point.py b/ml/dlib/tools/python/test/test_point.py
new file mode 100644
index 000000000..75b8c191f
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_point.py
@@ -0,0 +1,48 @@
+from dlib import point, points
+try:
+ import cPickle as pickle # Use cPickle on Python 2.7
+except ImportError:
+ import pickle
+
+
+def test_point():
+ p = point(27, 42)
+ assert repr(p) == "point(27, 42)"
+ assert str(p) == "(27, 42)"
+ assert p.x == 27
+ assert p.y == 42
+ ser = pickle.dumps(p, 2)
+ deser = pickle.loads(ser)
+ assert deser.x == p.x
+ assert deser.y == p.y
+
+
+def test_point_init_kwargs():
+ p = point(y=27, x=42)
+ assert repr(p) == "point(42, 27)"
+ assert str(p) == "(42, 27)"
+ assert p.x == 42
+ assert p.y == 27
+
+
+def test_points():
+ ps = points()
+
+ ps.resize(5)
+ assert len(ps) == 5
+ for i in range(5):
+ assert ps[i].x == 0
+ assert ps[i].y == 0
+
+ ps.clear()
+ assert len(ps) == 0
+
+ ps.extend([point(1, 2), point(3, 4)])
+ assert len(ps) == 2
+
+ ser = pickle.dumps(ps, 2)
+ deser = pickle.loads(ser)
+ assert deser[0].x == 1
+ assert deser[0].y == 2
+ assert deser[1].x == 3
+ assert deser[1].y == 4
diff --git a/ml/dlib/tools/python/test/test_range.py b/ml/dlib/tools/python/test/test_range.py
new file mode 100644
index 000000000..c881da369
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_range.py
@@ -0,0 +1,97 @@
+from dlib import range, ranges, rangess
+try:
+ import cPickle as pickle # Use cPickle on Python 2.7
+except ImportError:
+ import pickle
+from pytest import raises
+
+
+def test_range():
+ r = range(0, 10)
+ assert r.begin == 0
+ assert r.end == 10
+ assert str(r) == "0, 10"
+ assert repr(r) == "dlib.range(0, 10)"
+ assert len(r) == 10
+
+ ser = pickle.dumps(r, 2)
+ deser = pickle.loads(ser)
+
+ for a, b in zip(r, deser):
+ assert a == b
+
+
+# TODO: make this init parameterization an exception?
+def test_range_wrong_order():
+ r = range(5, 0)
+ assert r.begin == 5
+ assert r.end == 0
+ assert str(r) == "5, 0"
+ assert repr(r) == "dlib.range(5, 0)"
+ assert len(r) == 0
+
+
+def test_range_with_negative_elements():
+ with raises(TypeError):
+ range(-1, 1)
+ with raises(TypeError):
+ range(1, -1)
+
+
+def test_ranges():
+ rs = ranges()
+ assert len(rs) == 0
+
+ rs.resize(5)
+ assert len(rs) == 5
+ for r in rs:
+ assert r.begin == 0
+ assert r.end == 0
+
+ rs.clear()
+ assert len(rs) == 0
+
+ rs.extend([range(1, 2), range(3, 4)])
+ assert rs[0].begin == 1
+ assert rs[0].end == 2
+ assert rs[1].begin == 3
+ assert rs[1].end == 4
+
+ ser = pickle.dumps(rs, 2)
+ deser = pickle.loads(ser)
+ assert rs == deser
+
+
+def test_rangess():
+ rss = rangess()
+ assert len(rss) == 0
+
+ rss.resize(5)
+ assert len(rss) == 5
+ for rs in rss:
+ assert len(rs) == 0
+
+ rss.clear()
+ assert len(rss) == 0
+
+ rs1 = ranges()
+ rs1.append(range(1, 2))
+ rs1.append(range(3, 4))
+
+ rs2 = ranges()
+ rs2.append(range(5, 6))
+ rs2.append(range(7, 8))
+
+ rss.extend([rs1, rs2])
+ assert rss[0][0].begin == 1
+ assert rss[0][1].begin == 3
+ assert rss[1][0].begin == 5
+ assert rss[1][1].begin == 7
+ assert rss[0][0].end == 2
+ assert rss[0][1].end == 4
+ assert rss[1][0].end == 6
+ assert rss[1][1].end == 8
+
+ ser = pickle.dumps(rss, 2)
+ deser = pickle.loads(ser)
+ assert rss == deser
diff --git a/ml/dlib/tools/python/test/test_rgb_pixel.py b/ml/dlib/tools/python/test/test_rgb_pixel.py
new file mode 100644
index 000000000..0b3aaf5e9
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_rgb_pixel.py
@@ -0,0 +1,26 @@
+from dlib import rgb_pixel
+
+
+def test_rgb_pixel():
+ p = rgb_pixel(0, 50, 100)
+ assert p.red == 0
+ assert p.green == 50
+ assert p.blue == 100
+ assert str(p) == "red: 0, green: 50, blue: 100"
+ assert repr(p) == "rgb_pixel(0,50,100)"
+
+ p = rgb_pixel(blue=0, red=50, green=100)
+ assert p.red == 50
+ assert p.green == 100
+ assert p.blue == 0
+ assert str(p) == "red: 50, green: 100, blue: 0"
+ assert repr(p) == "rgb_pixel(50,100,0)"
+
+ p.red = 100
+ p.green = 0
+ p.blue = 50
+ assert p.red == 100
+ assert p.green == 0
+ assert p.blue == 50
+ assert str(p) == "red: 100, green: 0, blue: 50"
+ assert repr(p) == "rgb_pixel(100,0,50)"
diff --git a/ml/dlib/tools/python/test/test_sparse_vector.py b/ml/dlib/tools/python/test/test_sparse_vector.py
new file mode 100644
index 000000000..124e68d5d
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_sparse_vector.py
@@ -0,0 +1,101 @@
+from dlib import pair, make_sparse_vector, sparse_vector, sparse_vectors, sparse_vectorss
+try:
+ import cPickle as pickle # Use cPickle on Python 2.7
+except ImportError:
+ import pickle
+from pytest import approx
+
+
+def test_pair():
+ p = pair(4, .9)
+ assert p.first == 4
+ assert p.second == .9
+
+ p.first = 3
+ p.second = .4
+
+ assert p.first == 3
+ assert p.second == .4
+
+ assert str(p) == "3: 0.4"
+ assert repr(p) == "dlib.pair(3, 0.4)"
+
+ deser = pickle.loads(pickle.dumps(p, 2))
+ assert deser.first == p.first
+ assert deser.second == p.second
+
+
+def test_sparse_vector():
+ sv = sparse_vector()
+ sv.append(pair(3, .1))
+ sv.append(pair(3, .2))
+ sv.append(pair(2, .3))
+ sv.append(pair(1, .4))
+
+ assert len(sv) == 4
+ make_sparse_vector(sv)
+
+ assert len(sv) == 3
+ assert sv[0].first == 1
+ assert sv[0].second == .4
+ assert sv[1].first == 2
+ assert sv[1].second == .3
+ assert sv[2].first == 3
+ assert sv[2].second == approx(.3)
+
+ assert str(sv) == "1: 0.4\n2: 0.3\n3: 0.3"
+ assert repr(sv) == "< dlib.sparse_vector containing: \n1: 0.4\n2: 0.3\n3: 0.3 >"
+
+
+def test_sparse_vectors():
+ svs = sparse_vectors()
+ assert len(svs) == 0
+
+ svs.resize(5)
+ for sv in svs:
+ assert len(sv) == 0
+
+ svs.clear()
+ assert len(svs) == 0
+
+ svs.extend([sparse_vector([pair(1, 2), pair(3, 4)]), sparse_vector([pair(5, 6), pair(7, 8)])])
+
+ assert len(svs) == 2
+ assert svs[0][0].first == 1
+ assert svs[0][0].second == 2
+ assert svs[0][1].first == 3
+ assert svs[0][1].second == 4
+ assert svs[1][0].first == 5
+ assert svs[1][0].second == 6
+ assert svs[1][1].first == 7
+ assert svs[1][1].second == 8
+
+ deser = pickle.loads(pickle.dumps(svs, 2))
+ assert deser == svs
+
+
+def test_sparse_vectorss():
+ svss = sparse_vectorss()
+ assert len(svss) == 0
+
+ svss.resize(5)
+ for svs in svss:
+ assert len(svs) == 0
+
+ svss.clear()
+ assert len(svss) == 0
+
+ svss.extend([sparse_vectors([sparse_vector([pair(1, 2), pair(3, 4)]), sparse_vector([pair(5, 6), pair(7, 8)])])])
+
+ assert len(svss) == 1
+ assert svss[0][0][0].first == 1
+ assert svss[0][0][0].second == 2
+ assert svss[0][0][1].first == 3
+ assert svss[0][0][1].second == 4
+ assert svss[0][1][0].first == 5
+ assert svss[0][1][0].second == 6
+ assert svss[0][1][1].first == 7
+ assert svss[0][1][1].second == 8
+
+ deser = pickle.loads(pickle.dumps(svss, 2))
+ assert deser == svss
diff --git a/ml/dlib/tools/python/test/test_svm_c_trainer.py b/ml/dlib/tools/python/test/test_svm_c_trainer.py
new file mode 100644
index 000000000..ba9392e08
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_svm_c_trainer.py
@@ -0,0 +1,65 @@
+from __future__ import division
+
+import pytest
+from random import Random
+from dlib import (vectors, vector, sparse_vectors, sparse_vector, pair, array,
+ cross_validate_trainer,
+ svm_c_trainer_radial_basis,
+ svm_c_trainer_sparse_radial_basis,
+ svm_c_trainer_histogram_intersection,
+ svm_c_trainer_sparse_histogram_intersection,
+ svm_c_trainer_linear,
+ svm_c_trainer_sparse_linear,
+ rvm_trainer_radial_basis,
+ rvm_trainer_sparse_radial_basis,
+ rvm_trainer_histogram_intersection,
+ rvm_trainer_sparse_histogram_intersection,
+ rvm_trainer_linear,
+ rvm_trainer_sparse_linear)
+
+
+@pytest.fixture
+def training_data():
+ r = Random(0)
+ predictors = vectors()
+ sparse_predictors = sparse_vectors()
+ response = array()
+ for i in range(30):
+ for c in [-1, 1]:
+ response.append(c)
+ values = [r.random() + c * 0.5 for _ in range(3)]
+ predictors.append(vector(values))
+ sp = sparse_vector()
+ for i, v in enumerate(values):
+ sp.append(pair(i, v))
+ sparse_predictors.append(sp)
+ return predictors, sparse_predictors, response
+
+
+@pytest.mark.parametrize('trainer, class1_accuracy, class2_accuracy', [
+ (svm_c_trainer_radial_basis, 1.0, 1.0),
+ (svm_c_trainer_sparse_radial_basis, 1.0, 1.0),
+ (svm_c_trainer_histogram_intersection, 1.0, 1.0),
+ (svm_c_trainer_sparse_histogram_intersection, 1.0, 1.0),
+ (svm_c_trainer_linear, 1.0, 23 / 30),
+ (svm_c_trainer_sparse_linear, 1.0, 23 / 30),
+ (rvm_trainer_radial_basis, 1.0, 1.0),
+ (rvm_trainer_sparse_radial_basis, 1.0, 1.0),
+ (rvm_trainer_histogram_intersection, 1.0, 1.0),
+ (rvm_trainer_sparse_histogram_intersection, 1.0, 1.0),
+ (rvm_trainer_linear, 1.0, 0.6),
+ (rvm_trainer_sparse_linear, 1.0, 0.6)
+])
+def test_trainers(training_data, trainer, class1_accuracy, class2_accuracy):
+ predictors, sparse_predictors, response = training_data
+ if 'sparse' in trainer.__name__:
+ predictors = sparse_predictors
+ cv = cross_validate_trainer(trainer(), predictors, response, folds=10)
+ assert cv.class1_accuracy == pytest.approx(class1_accuracy)
+ assert cv.class2_accuracy == pytest.approx(class2_accuracy)
+
+ decision_function = trainer().train(predictors, response)
+ assert decision_function(predictors[2]) < 0
+ assert decision_function(predictors[3]) > 0
+ if 'linear' in trainer.__name__:
+ assert len(decision_function.weights) == 3
diff --git a/ml/dlib/tools/python/test/test_vector.py b/ml/dlib/tools/python/test/test_vector.py
new file mode 100644
index 000000000..ff79ab339
--- /dev/null
+++ b/ml/dlib/tools/python/test/test_vector.py
@@ -0,0 +1,170 @@
+from dlib import vector, vectors, vectorss, dot
+try:
+ import cPickle as pickle # Use cPickle on Python 2.7
+except ImportError:
+ import pickle
+from pytest import raises
+
+
+def test_vector_empty_init():
+ v = vector()
+ assert len(v) == 0
+ assert v.shape == (0, 1)
+ assert str(v) == ""
+ assert repr(v) == "dlib.vector([])"
+
+
+def test_vector_init_with_number():
+ v = vector(3)
+ assert len(v) == 3
+ assert v.shape == (3, 1)
+ assert str(v) == "0\n0\n0"
+ assert repr(v) == "dlib.vector([0, 0, 0])"
+
+
+def test_vector_set_size():
+ v = vector(3)
+
+ v.set_size(0)
+ assert len(v) == 0
+ assert v.shape == (0, 1)
+
+ v.resize(10)
+ assert len(v) == 10
+ assert v.shape == (10, 1)
+ for i in range(10):
+ assert v[i] == 0
+
+
+def test_vector_init_with_list():
+ v = vector([1, 2, 3])
+ assert len(v) == 3
+ assert v.shape == (3, 1)
+ assert str(v) == "1\n2\n3"
+ assert repr(v) == "dlib.vector([1, 2, 3])"
+
+
+def test_vector_getitem():
+ v = vector([1, 2, 3])
+ assert v[0] == 1
+ assert v[-1] == 3
+ assert v[1] == v[-2]
+
+
+def test_vector_slice():
+ v = vector([1, 2, 3, 4, 5])
+ v_slice = v[1:4]
+ assert len(v_slice) == 3
+ for idx, val in enumerate([2, 3, 4]):
+ assert v_slice[idx] == val
+
+ v_slice = v[-3:-1]
+ assert len(v_slice) == 2
+ for idx, val in enumerate([3, 4]):
+ assert v_slice[idx] == val
+
+ v_slice = v[1:-2]
+ assert len(v_slice) == 2
+ for idx, val in enumerate([2, 3]):
+ assert v_slice[idx] == val
+
+
+def test_vector_invalid_getitem():
+ v = vector([1, 2, 3])
+ with raises(IndexError):
+ v[-4]
+ with raises(IndexError):
+ v[3]
+
+
+def test_vector_init_with_negative_number():
+ with raises(Exception):
+ vector(-3)
+
+
+def test_dot():
+ v1 = vector([1, 0])
+ v2 = vector([0, 1])
+ v3 = vector([-1, 0])
+ assert dot(v1, v1) == 1
+ assert dot(v1, v2) == 0
+ assert dot(v1, v3) == -1
+
+
+def test_vector_serialization():
+ v = vector([1, 2, 3])
+ ser = pickle.dumps(v, 2)
+ deser = pickle.loads(ser)
+ assert str(v) == str(deser)
+
+
+def generate_test_vectors():
+ vs = vectors()
+ vs.append(vector([0, 1, 2]))
+ vs.append(vector([3, 4, 5]))
+ vs.append(vector([6, 7, 8]))
+ assert len(vs) == 3
+ return vs
+
+
+def generate_test_vectorss():
+ vss = vectorss()
+ vss.append(generate_test_vectors())
+ vss.append(generate_test_vectors())
+ vss.append(generate_test_vectors())
+ assert len(vss) == 3
+ return vss
+
+
+def test_vectors_serialization():
+ vs = generate_test_vectors()
+ ser = pickle.dumps(vs, 2)
+ deser = pickle.loads(ser)
+ assert vs == deser
+
+
+def test_vectors_clear():
+ vs = generate_test_vectors()
+ vs.clear()
+ assert len(vs) == 0
+
+
+def test_vectors_resize():
+ vs = vectors()
+ vs.resize(100)
+ assert len(vs) == 100
+ for i in range(100):
+ assert len(vs[i]) == 0
+
+
+def test_vectors_extend():
+ vs = vectors()
+ vs.extend([vector([1, 2, 3]), vector([4, 5, 6])])
+ assert len(vs) == 2
+
+
+def test_vectorss_serialization():
+ vss = generate_test_vectorss()
+ ser = pickle.dumps(vss, 2)
+ deser = pickle.loads(ser)
+ assert vss == deser
+
+
+def test_vectorss_clear():
+ vss = generate_test_vectorss()
+ vss.clear()
+ assert len(vss) == 0
+
+
+def test_vectorss_resize():
+ vss = vectorss()
+ vss.resize(100)
+ assert len(vss) == 100
+ for i in range(100):
+ assert len(vss[i]) == 0
+
+
+def test_vectorss_extend():
+ vss = vectorss()
+ vss.extend([generate_test_vectors(), generate_test_vectors()])
+ assert len(vss) == 2