Skip to content

Commit c22f243

Browse files
authored
Merge pull request #28 from anshuman23/dev
Added improved tests
2 parents 3c9b599 + 2661328 commit c22f243

File tree

1 file changed

+175
-36
lines changed

1 file changed

+175
-36
lines changed

Diff for: test/tensorflex_test.exs

+175-36
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,193 @@
11
defmodule TensorflexTest do
22
use ExUnit.Case
3-
doctest Tensorflex
43

5-
test "Matrix Functionalities Test" do
6-
mat = Tensorflex.create_matrix(2,3, [[2.2,1.3,44.5],[5.5,6.1,3.333]])
7-
assert 5.5 = Tensorflex.matrix_pos(mat,2,1)
8-
assert {2,3} = Tensorflex.size_of_matrix(mat)
9-
assert [[2.2,1.3,44.5],[5.5,6.1,3.333]] = Tensorflex.matrix_to_lists(mat)
4+
describe "matrix functionalities" do
5+
test "matrix creation check" do
6+
assert [[2.2,1.3,44.5],[5.5,6.1,3.333]] = Tensorflex.create_matrix(2,3, [[2.2,1.3,44.5],[5.5,6.1,3.333]]) |> Tensorflex.matrix_to_lists
7+
end
8+
9+
test "matrix to lists conversion check" do
10+
mat = Tensorflex.create_matrix(5,4,[[123,431,23,1],[1,2,3,4],[5,6,7,8],[768,564,44,5],[1,2,3,4]])
11+
assert [[123.0, 431.0, 23.0, 1.0],[1.0, 2.0, 3.0, 4.0],[5.0, 6.0, 7.0, 8.0],[768.0, 564.0, 44.0, 5.0],[1.0, 2.0, 3.0, 4.0]] = Tensorflex.matrix_to_lists mat
12+
end
13+
14+
test "matrix access function check" do
15+
mat = Tensorflex.create_matrix(2,3, [[2.2,1.3,44.5],[5.5,6.1,3.333]])
16+
assert 5.5 = Tensorflex.matrix_pos(mat,2,1)
17+
assert 3.333 = Tensorflex.matrix_pos(mat,2,3)
18+
end
19+
20+
test "get size of matrix" do
21+
assert {3,3} = Tensorflex.create_matrix(3,3, [[3.9,62,122],[2.2,1.3,44.5],[5.5,6.1,3.333]]) |> Tensorflex.size_of_matrix
22+
end
23+
24+
test "append new row to matrix function check" do
25+
mat = Tensorflex.create_matrix(4,4,[[123,431,23,1],[1,2,3,4],[5,6,7,8],[768,564,44,5]])
26+
mat = Tensorflex.append_to_matrix(mat, [[4.4,2,7,9.9]])
27+
assert {5,4} = Tensorflex.size_of_matrix mat
28+
assert 7.0 = Tensorflex.matrix_pos(mat,5,3)
29+
end
1030
end
1131

12-
test "Tensor Functionalities Test" do
13-
dims = Tensorflex.create_matrix(1,3,[[1,1,3]])
14-
vals = Tensorflex.create_matrix(1,3,[[245,202,9]])
15-
assert {:ok, :tf_float} = Tensorflex.tensor_datatype(elem(Tensorflex.float32_tensor(vals,dims),1))
16-
assert {:ok, :tf_float} = Tensorflex.tensor_datatype(elem(Tensorflex.float32_tensor_alloc(dims),1))
17-
assert {:ok, :tf_float} = Tensorflex.tensor_datatype(elem(Tensorflex.float32_tensor(123.123),1))
18-
assert {:ok, :tf_string} = Tensorflex.tensor_datatype(elem(Tensorflex.string_tensor("123.123"),1))
19-
assert {:ok, :tf_double} = Tensorflex.tensor_datatype(elem(Tensorflex.float64_tensor(123.123),1))
20-
assert_raise ArgumentError, fn ->
21-
Tensorflex.float32_tensor("123.123")
32+
describe "float32 tensor functionalities" do
33+
test "float32_tensor/2 tensor creation check" do
34+
dims = Tensorflex.create_matrix(1,2,[[1,3]])
35+
vals = Tensorflex.create_matrix(1,3,[[245,202,9]])
36+
{:ok, tensor} = Tensorflex.float32_tensor vals,dims
37+
{:ok, :tf_float} = Tensorflex.tensor_datatype(tensor)
38+
end
39+
40+
test "float32_tensor/1 tensor creation check" do
41+
{:ok, tensor} = Tensorflex.float32_tensor 1234.1234
42+
{:ok, :tf_float} = Tensorflex.tensor_datatype(tensor)
43+
end
44+
45+
test "float32_tensor_alloc/1 tensor creation check" do
46+
dims = Tensorflex.create_matrix(1,2,[[1,3]])
47+
{:ok, tensor} = Tensorflex.float32_tensor_alloc dims
48+
{:ok, :tf_float} = Tensorflex.tensor_datatype(tensor)
2249
end
23-
assert_raise ArgumentError, fn ->
24-
Tensorflex.string_tensor(123.123)
50+
51+
test "incorrect usage check" do
52+
assert_raise FunctionClauseError, fn ->
53+
Tensorflex.float32_tensor("123.123")
54+
end
55+
56+
assert_raise FunctionClauseError, fn ->
57+
Tensorflex.float32_tensor(123)
58+
end
2559
end
2660
end
2761

28-
test "Graph Loading Test" do
29-
assert ["biases", "biases/read", "weights", "weights/read", "input", "MatMul", "add", "output"] = Tensorflex.get_graph_ops(elem(Tensorflex.read_graph("./test/graphdef_toy.pb"),1))
30-
assert ["biases2", "biases2/read", "weights2", "weights2/read", "biases1", "biases1/read", "weights1", "weights1/read", "input", "MatMul", "Add", "Relu", "MatMul_1", "Add_1", "output"] = Tensorflex.get_graph_ops(elem(Tensorflex.read_graph("./test/graphdef_iris.pb"),1))
31-
assert_raise ArgumentError, fn ->
32-
Tensorflex.read_graph("Makefile")
62+
describe "float64 tensor functionalities" do
63+
test "float64_tensor/2 tensor creation check" do
64+
dims = Tensorflex.create_matrix(1,2,[[1,3]])
65+
vals = Tensorflex.create_matrix(1,3,[[245,202,9]])
66+
{:ok, tensor} = Tensorflex.float64_tensor vals,dims
67+
{:ok, :tf_double} = Tensorflex.tensor_datatype(tensor)
68+
end
69+
70+
test "float64_tensor/1 tensor creation check" do
71+
{:ok, tensor} = Tensorflex.float64_tensor 1234.1234
72+
{:ok, :tf_double} = Tensorflex.tensor_datatype(tensor)
3373
end
34-
assert_raise ArgumentError, fn ->
35-
Tensorflex.read_graph("graph.txt")
74+
75+
test "float64_tensor_alloc/1 tensor creation check" do
76+
dims = Tensorflex.create_matrix(1,2,[[1,3]])
77+
{:ok, tensor} = Tensorflex.float64_tensor_alloc dims
78+
{:ok, :tf_double} = Tensorflex.tensor_datatype(tensor)
79+
end
80+
81+
test "incorrect usage check" do
82+
assert_raise FunctionClauseError, fn ->
83+
Tensorflex.float64_tensor("123.123")
84+
end
85+
86+
assert_raise FunctionClauseError, fn ->
87+
Tensorflex.float64_tensor(123)
88+
end
3689
end
3790
end
3891

39-
test "Session Test" do
40-
{:ok, graph} = Tensorflex.read_graph("./test/graphdef_toy.pb")
41-
in_vals = Tensorflex.create_matrix(3,3,[[1.0,1.0,1.0],[2.0,2.0,2.0],[3.0,3.0,3.0]])
42-
in_dims = Tensorflex.create_matrix(1,2,[[3,3]])
43-
{:ok, input_tensor} = Tensorflex.float32_tensor(in_vals, in_dims)
44-
out_dims = Tensorflex.create_matrix(1,2,[[3,2]])
45-
{:ok, output_tensor} = Tensorflex.float32_tensor_alloc(out_dims)
46-
assert [[56.349998474121094, 39.26000213623047], [109.69999694824219, 75.52000427246094], [163.04998779296875, 111.77999877929688]] = Tensorflex.run_session(graph, input_tensor, output_tensor, "input", "output")
92+
describe "int32 tensor functionalities" do
93+
test "int32_tensor/2 tensor creation check" do
94+
dims = Tensorflex.create_matrix(1,2,[[1,3]])
95+
vals = Tensorflex.create_matrix(1,3,[[245,202,9]])
96+
{:ok, tensor} = Tensorflex.int32_tensor vals,dims
97+
{:ok, :tf_int32} = Tensorflex.tensor_datatype(tensor)
98+
end
99+
100+
test "int32_tensor/1 tensor creation check" do
101+
{:ok, tensor} = Tensorflex.int32_tensor 1234
102+
{:ok, :tf_int32} = Tensorflex.tensor_datatype(tensor)
103+
end
104+
105+
test "int32_tensor_alloc/1 tensor creation check" do
106+
dims = Tensorflex.create_matrix(1,2,[[1,3]])
107+
{:ok, tensor} = Tensorflex.int32_tensor_alloc dims
108+
{:ok, :tf_int32} = Tensorflex.tensor_datatype(tensor)
109+
end
110+
111+
test "incorrect usage check" do
112+
assert_raise FunctionClauseError, fn ->
113+
Tensorflex.int32_tensor("123.123")
114+
end
115+
116+
assert_raise FunctionClauseError, fn ->
117+
Tensorflex.int32_tensor(123.123)
118+
end
119+
end
47120
end
48-
49-
end
50121

122+
describe "string tensor functionality" do
123+
test "string tensor creation check" do
124+
{:ok, tensor} = Tensorflex.string_tensor "123.123"
125+
{:ok, :tf_string} = Tensorflex.tensor_datatype tensor
126+
end
51127

128+
test "incorrect usage check" do
129+
assert_raise FunctionClauseError, fn ->
130+
Tensorflex.string_tensor(123.123)
131+
end
132+
133+
assert_raise FunctionClauseError, fn ->
134+
Tensorflex.string_tensor(123)
135+
end
136+
end
137+
end
52138

139+
describe "graph loading and reading functionalities" do
140+
test "graph loading check" do
141+
{:ok, graph_toy} = Tensorflex.read_graph "./test/graphdef_toy.pb"
142+
{:ok, graph_iris} = Tensorflex.read_graph "./test/graphdef_iris.pb"
143+
end
144+
145+
test "get all graph ops" do
146+
{:ok, graph_toy} = Tensorflex.read_graph "./test/graphdef_toy.pb"
147+
{:ok, graph_iris} = Tensorflex.read_graph "./test/graphdef_iris.pb"
148+
assert ["input", "weights", "weights/read", "biases", "biases/read", "MatMul", "add", "output"] = Tensorflex.get_graph_ops graph_toy
149+
assert ["input", "weights1", "weights1/read", "biases1", "biases1/read", "weights2", "weights2/read", "biases2", "biases2/read", "MatMul", "Add", "Relu", "MatMul_1", "Add_1", "output"] = Tensorflex.get_graph_ops graph_iris
150+
end
151+
152+
test "incorrect usage check" do
153+
assert_raise ArgumentError, fn ->
154+
{:ok, graph} = Tensorflex.read_graph "Makefile"
155+
end
156+
157+
assert_raise ArgumentError, fn ->
158+
{:ok, graph} = Tensorflex.read_graph "Makefile.pb"
159+
end
160+
end
161+
end
162+
163+
describe "session functionality" do
164+
test "running session check" do
165+
{:ok, graph} = Tensorflex.read_graph("./test/graphdef_toy.pb")
166+
in_vals = Tensorflex.create_matrix(3,3,[[1.0,1.0,1.0],[2.0,2.0,2.0],[3.0,3.0,3.0]])
167+
in_dims = Tensorflex.create_matrix(1,2,[[3,3]])
168+
{:ok, input_tensor} = Tensorflex.float32_tensor(in_vals, in_dims)
169+
out_dims = Tensorflex.create_matrix(1,2,[[3,2]])
170+
{:ok, output_tensor} = Tensorflex.float32_tensor_alloc(out_dims)
171+
assert [[56.349998474121094, 39.26000213623047], [109.69999694824219, 75.52000427246094], [163.04998779296875, 111.77999877929688]] = Tensorflex.run_session(graph, input_tensor, output_tensor, "input", "output")
172+
end
173+
end
174+
175+
describe "miscellaneous functionalities" do
176+
test "CSV-with-header loading function check" do
177+
m = Tensorflex.load_csv_as_matrix("./test/sample2.csv", header: :true, delimiter: "-")
178+
assert [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]] = Tensorflex.matrix_to_lists m
179+
end
180+
181+
test "CSV-without-header loading function check" do
182+
m = Tensorflex.load_csv_as_matrix("./test/sample1.csv", header: :false)
183+
assert [[1.0, 2.0, 3.0, 4.0, 5.0],[6.0, 7.0, 8.0, 9.0, 10.0],[11.0, 12.0, 13.0, 14.0, 15.0]] = Tensorflex.matrix_to_lists m
184+
end
185+
186+
test "CSV-to-matrix function incorrect usage check" do
187+
assert_raise ArgumentError, fn ->
188+
m = Tensorflex.load_csv_as_matrix("./test/sample1.csv", header: :no_header, delimiter: ",")
189+
end
190+
end
191+
end
53192

54-
193+
end

0 commit comments

Comments
 (0)