-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_test.cpp
85 lines (73 loc) · 2.15 KB
/
slice_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <math.h>
#include "gtest/gtest.h"
extern "C" {
#include "src/basic/factories.h"
#include "src/basic/slice.h"
#include "src/core/tensor.h"
}
void slice_assign_int32(Tensor t) {
int64_t size = aitisa_tensor_size(t);
int32_t* data = (int32_t*)aitisa_tensor_data(t);
int32_t value = 0;
for (int i = 0; i < size; ++i) {
value = i;
data[i] = value;
}
}
void slice_assign_float(Tensor t) {
int64_t size = aitisa_tensor_size(t);
float* data = (float*)aitisa_tensor_data(t);
float value = 0;
for (int i = 0; i < size; ++i) {
value = i * 0.1;
data[i] = value;
}
}
namespace aitisa_api {
namespace {
TEST(Slice, StepIsOne) {
Tensor input;
DataType dtype = kFloat;
Device device = {DEVICE_CPU, 0};
int64_t dims[3] = {2, 4, 5};
aitisa_create(dtype, device, dims, 3, NULL, 0, &input);
slice_assign_float(input);
Tensor output;
int begin[3] = {1, 1, 2};
int size[3] = {1, 2, 3};
int step[3] = {1, 1, 1};
aitisa_slice(input, begin, size, step, &output);
float* out_data = (float*)aitisa_tensor_data(output);
int64_t out_size = aitisa_tensor_size(output);
float test_data[] = {2.7, 2.8, 2.9, 3.2, 3.3, 3.4};
for (int64_t i = 0; i < out_size; i++) {
/* Due to the problem of precision, consider the two numbers
are equal when their difference is less than 0.00001*/
EXPECT_TRUE(abs(out_data[i] - test_data[i]) < 0.00001);
}
aitisa_destroy(&input);
aitisa_destroy(&output);
}
TEST(Slice, StepNotOne) {
Tensor input;
DataType dtype = kInt32;
Device device = {DEVICE_CPU, 0};
int64_t dims[3] = {2, 5, 10};
aitisa_create(dtype, device, dims, 3, NULL, 0, &input);
slice_assign_int32(input);
Tensor output;
int begin[3] = {0, 1, 2};
int size[3] = {2, 4, 6};
int step[3] = {1, 2, 3};
aitisa_slice(input, begin, size, step, &output);
int32_t* out_data = (int32_t*)aitisa_tensor_data(output);
int64_t out_size = aitisa_tensor_size(output);
int32_t test_data[] = {12, 15, 32, 35, 62, 65, 82, 85};
for (int64_t i = 0; i < out_size; i++) {
EXPECT_EQ(out_data[i], test_data[i]);
}
aitisa_destroy(&input);
aitisa_destroy(&output);
}
} // namespace
} // namespace aitisa_api