1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/lite/experimental/writer/writer_lib.h"
17 #include <gtest/gtest.h>
18 #include "tensorflow/lite/interpreter.h"
19 #include "tensorflow/lite/kernels/register.h"
20 #include "tensorflow/lite/model.h"
21 #include "tensorflow/lite/testing/util.h"
22 
23 namespace tflite {
24 // Make an interpreter that has no tensors and no nodes
25 // TODO(b/113731921): add more tests.
TEST(Writer,BasicTest)26 TEST(Writer, BasicTest) {
27   Interpreter interpreter;
28   interpreter.AddTensors(3);
29   float foo[] = {1, 2, 3};
30   interpreter.SetTensorParametersReadWrite(0, kTfLiteFloat32, "a", {3},
31                                            TfLiteQuantizationParams());
32   interpreter.SetTensorParametersReadOnly(
33       1, kTfLiteFloat32, "b", {3}, TfLiteQuantizationParams(),
34       reinterpret_cast<char*>(foo), sizeof(foo));
35   interpreter.SetTensorParametersReadWrite(2, kTfLiteFloat32, "c", {3},
36                                            TfLiteQuantizationParams());
37   interpreter.SetInputs({0, 1});
38   interpreter.SetOutputs({2});
39   const char* initial_data = "";
40   tflite::ops::builtin::BuiltinOpResolver resolver;
41   TfLiteAddParams* builtin_data =
42       reinterpret_cast<TfLiteAddParams*>(malloc(sizeof(TfLiteAddParams)));
43   builtin_data->activation = kTfLiteActNone;
44   const TfLiteRegistration* reg = resolver.FindOp(BuiltinOperator_ADD, 1);
45   interpreter.AddNodeWithParameters({0, 1}, {2}, initial_data, 0,
46                                     reinterpret_cast<void*>(builtin_data), reg);
47 
48   InterpreterWriter writer(&interpreter);
49   writer.Write("/tmp/test.tflite");
50   std::unique_ptr<FlatBufferModel> model =
51       FlatBufferModel::BuildFromFile("/tmp/test.tflite");
52   InterpreterBuilder builder(*model, resolver);
53   std::unique_ptr<Interpreter> new_interpreter;
54   builder(&new_interpreter);
55 }
56 
57 }  // namespace tflite
58 
main(int argc,char ** argv)59 int main(int argc, char** argv) {
60   ::testing::InitGoogleTest(&argc, argv);
61   return RUN_ALL_TESTS();
62 }
63