Skip to content

Commit c06e0cd

Browse files
committed
conv fc tests
1 parent 3202dc3 commit c06e0cd

2 files changed

Lines changed: 124 additions & 0 deletions

File tree

test/single_layer/test_convlayer.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,4 +992,45 @@ TEST(ConvolutionalLayerTest, ConvImplInt2DKernelComplexPattern) {
992992
for (size_t i = 0; i < result.size(); ++i) {
993993
ASSERT_GT(result[i], 0);
994994
}
995+
}
996+
997+
TEST(ConvolutionalLayerTest, Float2DKernelPathCoverage) {
998+
std::vector<float> image = {1.0f, 2.0f, 3.0f, 4.0f};
999+
Shape input_shape({1, 1, 2, 2});
1000+
Tensor input = make_tensor(image, input_shape);
1001+
1002+
std::vector<float> kernelvec = {1.0f, 0.0f, 1.0f, 0.0f};
1003+
Shape kernel_shape({2, 2});
1004+
Tensor kernel = make_tensor(kernelvec, kernel_shape);
1005+
1006+
std::vector<float> output_vec(1, 0.0f);
1007+
Tensor output = make_tensor(output_vec, Shape({1, 1, 1, 1}));
1008+
1009+
ConvolutionalLayer layer(1, 0, 0, kernel);
1010+
std::vector<Tensor> in{input};
1011+
std::vector<Tensor> out{output};
1012+
1013+
EXPECT_THROW(layer.run(in, out), std::exception);
1014+
}
1015+
1016+
TEST(ConvolutionalLayerTest, Float4DKernelWorking) {
1017+
std::vector<float> image = {1.0f, 2.0f, 3.0f, 4.0f};
1018+
Shape input_shape({1, 1, 2, 2});
1019+
Tensor input = make_tensor(image, input_shape);
1020+
1021+
std::vector<float> kernelvec = {1.0f, 0.0f, 1.0f, 0.0f};
1022+
Shape kernel_shape({1, 1, 2, 2});
1023+
Tensor kernel = make_tensor(kernelvec, kernel_shape);
1024+
1025+
std::vector<float> output_vec(1, 0.0f);
1026+
Tensor output = make_tensor(output_vec, Shape({1, 1, 1, 1}));
1027+
1028+
ConvolutionalLayer layer(1, 0, 0, kernel);
1029+
std::vector<Tensor> in{input};
1030+
std::vector<Tensor> out{output};
1031+
1032+
EXPECT_NO_THROW(layer.run(in, out));
1033+
1034+
std::vector<float> result = *out[0].as<float>();
1035+
ASSERT_EQ(result.size(), 4);
9951036
}

test/single_layer/test_fclayer.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,86 @@ TEST(fclayer, new_fc_bias_and_weights_not_same) {
249249
std::vector<Tensor> out{output};
250250
EXPECT_THROW(layer.run(in, out), std::invalid_argument);
251251
}
252+
TEST(fclayer, VectorSizeNotDivisibleByMatrixRows) {
253+
std::vector<float> weightsvec = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
254+
Shape weights_shape({3, 2});
255+
Tensor weights = make_tensor(weightsvec, weights_shape);
256+
257+
std::vector<float> biasvec = {0.1f, 0.2f};
258+
Tensor bias = make_tensor(biasvec, Shape({2}));
259+
260+
std::vector<float> input_vec = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
261+
Tensor input = make_tensor(input_vec, Shape({5}));
262+
263+
std::vector<float> output_vec(4, 0.0f);
264+
Tensor output = make_tensor(output_vec, Shape({2, 2}));
265+
266+
FCLayer layer(weights, bias);
267+
std::vector<Tensor> in{input};
268+
std::vector<Tensor> out{output};
269+
270+
EXPECT_THROW(layer.run(in, out), std::invalid_argument);
271+
}
272+
273+
TEST(fclayer, VectorSizeNotDivisibleByMatrixRowsInt) {
274+
std::vector<int> weightsvec = {1, 2, 3, 4, 5, 6, 7, 8};
275+
Shape weights_shape({4, 2});
276+
Tensor weights = make_tensor(weightsvec, weights_shape);
277+
278+
std::vector<int> biasvec = {1, 2};
279+
Tensor bias = make_tensor(biasvec, Shape({2}));
280+
281+
std::vector<int> input_vec = {1, 2, 3, 4, 5, 6, 7};
282+
Tensor input = make_tensor(input_vec, Shape({7}));
283+
284+
std::vector<int> output_vec(4, 0);
285+
Tensor output = make_tensor(output_vec, Shape({2, 2}));
286+
287+
FCLayer layer(weights, bias);
288+
std::vector<Tensor> in{input};
289+
std::vector<Tensor> out{output};
290+
291+
EXPECT_THROW(layer.run(in, out), std::invalid_argument);
292+
}
293+
294+
TEST(fclayer, VectorSizeDivisibleByMatrixRows) {
295+
std::vector<float> weightsvec = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
296+
Shape weights_shape({3, 2});
297+
Tensor weights = make_tensor(weightsvec, weights_shape);
298+
299+
std::vector<float> biasvec = {0.1f, 0.2f};
300+
Tensor bias = make_tensor(biasvec, Shape({2}));
301+
302+
std::vector<float> input_vec = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
303+
Tensor input = make_tensor(input_vec, Shape({6}));
304+
305+
std::vector<float> output_vec(4, 0.0f);
306+
Tensor output = make_tensor(output_vec, Shape({2, 2}));
307+
308+
FCLayer layer(weights, bias);
309+
std::vector<Tensor> in{input};
310+
std::vector<Tensor> out{output};
311+
312+
EXPECT_NO_THROW(layer.run(in, out));
313+
}
314+
315+
TEST(fclayer, ZeroOutputNeuronsWithNonZeroInput) {
316+
std::vector<float> weightsvec = {};
317+
Shape weights_shape({5, 0});
318+
Tensor weights = make_tensor(weightsvec, weights_shape);
319+
320+
std::vector<float> biasvec = {};
321+
Tensor bias = make_tensor(biasvec, Shape({0}));
322+
323+
std::vector<float> input_vec = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
324+
Tensor input = make_tensor(input_vec, Shape({5}));
325+
326+
std::vector<float> output_vec = {};
327+
Tensor output = make_tensor(output_vec, Shape({0}));
328+
329+
FCLayer layer(weights, bias);
330+
std::vector<Tensor> in{input};
331+
std::vector<Tensor> out{output};
332+
333+
EXPECT_THROW(layer.run(in, out), std::invalid_argument);
334+
}

0 commit comments

Comments
 (0)