Release 9.0.0.16564
[platform/core/csapi/tizenfx.git] / test / Tizen.MachineLearning.Inference.Test / TensorsInfoTest.cs
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.IO;
19 using Tizen.MachineLearning.Inference;
20
21 namespace Tizen.MachineLearning.Inference.Test
22 {
23     public static class TensorsInfoTest
24     {
25         const string TAG = "Nnstreamer";
26
27         public static bool BasicTensorTest_Success00()
28         {
29             int[] in_dim = new int[4] { 3, 224, 224, 1 };
30
31             TensorsInfo tensorsInfo = new TensorsInfo();
32             tensorsInfo.AddTensorInfo(TensorType.UInt8, in_dim);
33
34             /* Check */
35             if (tensorsInfo.GetTensorType(0) != TensorType.UInt8)
36                 return false;
37
38             int[] in_res = tensorsInfo.GetDimension(0);
39             for (int i = 0; i < 4; ++i)
40             {
41                 if (in_dim[i] != in_res[i])
42                     return false;
43             }
44             return true;
45         }
46         public static bool BasicTensorTest_Success01()
47         {
48             TensorsInfo tensorsInfo;
49             TensorsData tensorsData;
50             int[] in_dim = new int[4] { 10, 1, 1, 1 };
51             byte[] buffer_in = new byte[] { 17, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
52             byte[] buffer_out;
53
54             tensorsInfo = new TensorsInfo();
55             tensorsInfo.AddTensorInfo(TensorType.UInt8, in_dim);
56             Log.Info(TAG, "Current Count: " + tensorsInfo.Count);
57
58             tensorsData = tensorsInfo.GetTensorsData();
59             tensorsData.SetTensorData(0, buffer_in);
60
61             buffer_out = tensorsData.GetTensorData(0);
62
63             if (buffer_in.Length != buffer_out.Length)
64             {
65                 Log.Error(TAG, "The size of buffers is different");
66                 return false;
67             }
68
69             for (int i = 0; i < buffer_in.Length; ++i)
70             {
71                 if (buffer_in[i] != buffer_out[i])
72                 {
73                     Log.Error(TAG, "The value of " + i.ToString() + " th element is different");
74                     return false;
75                 }
76             }
77
78             return true;
79         }
80
81         public static bool BasicTensorTest_Success02()
82         {
83             TensorsInfo tensorsInfo;
84             TensorsData tensorsData;
85             int[] in_dim = new int[4] { 10, 1, 1, 1 };
86             byte[] buffer_in = new byte[] { 17, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
87             byte[] buffer_out;
88
89             tensorsInfo = new TensorsInfo();
90             tensorsInfo.AddTensorInfo(TensorType.UInt8, in_dim);
91
92             tensorsData = tensorsInfo.GetTensorsData();
93             tensorsData.SetTensorData(0, buffer_in);
94             buffer_out = tensorsData.GetTensorData(0);
95
96             if (buffer_in.Length != buffer_out.Length)
97             {
98                 Log.Error(TAG, "The size of buffers is different");
99                 return false;
100             }
101
102             for (int i = 0; i < buffer_in.Length; ++i)
103             {
104                 if (buffer_in[i] != buffer_out[i])
105                 {
106                     Log.Error(TAG, "The value of " + i.ToString() + " th element is different");
107                     return false;
108                 }
109             }
110             tensorsData.Dispose();
111
112             /* Add new tensor */
113             int[] in2_dim = new int[4] { 5, 1, 1, 1 };
114             byte[] buffer_in2 = new byte[] { 10, 20, 30, 40, 50 };
115             byte[] buffer_out2;
116
117
118             tensorsInfo.AddTensorInfo(TensorType.UInt8, in2_dim);
119
120             tensorsData = tensorsInfo.GetTensorsData();
121             tensorsData.SetTensorData(0, buffer_in);
122             buffer_out = tensorsData.GetTensorData(0);
123             tensorsData.SetTensorData(1, buffer_in2);
124             buffer_out2 = tensorsData.GetTensorData(1);
125
126             if (buffer_in2.Length != buffer_out2.Length)
127             {
128                 Log.Error(TAG, "The size of buffers is different");
129                 return false;
130             }
131
132             for (int i = 0; i < buffer_in2.Length; ++i)
133             {
134                 if (buffer_in2[i] != buffer_out2[i])
135                 {
136                     Log.Error(TAG, "The value of " + i.ToString() + " th element is different");
137                     return false;
138                 }
139             }
140
141             return true;
142         }
143     }
144 }