aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/kernels/cast_test.cc
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-03-06 09:54:36 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-03-06 09:59:06 -0800
commitedbd683f42f999b8665a51c9312cdf9d05b335bb (patch)
tree918d8adabb8935bf73f077545ed3d84398fd0f50 /tensorflow/contrib/lite/kernels/cast_test.cc
parenta2ea23e91915fabd0e856f284d0af75a496a432a (diff)
Implementation of tf.cast in TfLite
PiperOrigin-RevId: 188036286
Diffstat (limited to 'tensorflow/contrib/lite/kernels/cast_test.cc')
-rw-r--r--tensorflow/contrib/lite/kernels/cast_test.cc66
1 files changed, 66 insertions, 0 deletions
diff --git a/tensorflow/contrib/lite/kernels/cast_test.cc b/tensorflow/contrib/lite/kernels/cast_test.cc
new file mode 100644
index 0000000000..4e56482a37
--- /dev/null
+++ b/tensorflow/contrib/lite/kernels/cast_test.cc
@@ -0,0 +1,66 @@
+/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+==============================================================================*/
+#include <gtest/gtest.h>
+#include "tensorflow/contrib/lite/interpreter.h"
+#include "tensorflow/contrib/lite/kernels/register.h"
+#include "tensorflow/contrib/lite/kernels/test_util.h"
+#include "tensorflow/contrib/lite/model.h"
+
+namespace tflite {
+namespace {
+
+using ::testing::ElementsAreArray;
+
+class CastOpModel : public SingleOpModel {
+ public:
+ CastOpModel(const TensorData& input, const TensorData& output) {
+ input_ = AddInput(input);
+ output_ = AddOutput(output);
+ SetBuiltinOp(BuiltinOperator_CAST, BuiltinOptions_CastOptions,
+ CreateCastOptions(builder_).Union());
+ BuildInterpreter({GetShape(input_)});
+ }
+
+ int input() const { return input_; }
+ int output() const { return output_; }
+
+ protected:
+ int input_;
+ int output_;
+};
+
+TEST(CastOpModel, CastIntToFloat) {
+ CastOpModel m({TensorType_INT64, {2, 3}}, {TensorType_FLOAT32, {2, 3}});
+ m.PopulateTensor<int64_t>(m.input(), {100, 200, 300, 400, 500, 600});
+ m.Invoke();
+ EXPECT_THAT(m.ExtractVector<float>(m.output()),
+ ElementsAreArray({100.f, 200.f, 300.f, 400.f, 500.f, 600.f}));
+}
+
+TEST(CastOpModel, CastFloatToInt) {
+ CastOpModel m({TensorType_FLOAT32, {3, 2}}, {TensorType_INT32, {3, 2}});
+ m.PopulateTensor<float>(m.input(), {100.f, 20.f, 3.f, 0.4f, 0.999f, 1.1f});
+ m.Invoke();
+ EXPECT_THAT(m.ExtractVector<int>(m.output()),
+ ElementsAreArray({100, 20, 3, 0, 0, 1}));
+}
+
+} // namespace
+} // namespace tflite
+int main(int argc, char** argv) {
+ ::tflite::LogToStderr();
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}