TFLM: Fix mysterious doule execution issue in TF_LITE_MICRO_EXPECT_EQ.

If x has side effect, and the if condition is false, then x will be executed twice.

e.g.

TF_LITE_MICRO_EXPECT_EQ(Inc(), 3); will do Inc() twice if the first return value
is not 3.

PiperOrigin-RevId: 303896402
Change-Id: I8cade5509e8b6cf64f843c33895d034d7baeeeeb
This commit is contained in:
Tiezhen WANG 2020-03-30 22:39:01 -07:00 committed by TensorFlower Gardener
parent 79f69d1221
commit 23eb56ce6d
3 changed files with 60 additions and 11 deletions

View File

@ -1,3 +1,8 @@
load(
"//tensorflow/lite/micro/testing:micro_test.bzl",
"tflite_micro_cc_test",
)
package(
default_visibility = ["//visibility:public"],
licenses = ["notice"], # Apache 2.0
@ -23,6 +28,16 @@ cc_library(
],
)
tflite_micro_cc_test(
name = "util_test",
srcs = [
"util_test.cc",
],
deps = [
":micro_test",
],
)
cc_library(
name = "micro_benchmark",
hdrs = [

View File

@ -109,9 +109,11 @@ extern tflite::ErrorReporter* reporter;
#define TF_LITE_MICRO_EXPECT_EQ(x, y) \
do { \
if ((x) != (y)) { \
auto vx = x; \
auto vy = y; \
if ((vx) != (vy)) { \
micro_test::reporter->Report(#x " == " #y " failed at %s:%d (%d vs %d)", \
__FILE__, __LINE__, (x), (y)); \
__FILE__, __LINE__, (vx), (vy)); \
micro_test::did_test_fail = true; \
} \
} while (false)
@ -142,15 +144,17 @@ extern tflite::ErrorReporter* reporter;
} \
} while (false)
#define TF_LITE_MICRO_EXPECT_NEAR(x, y, epsilon) \
do { \
auto delta = ((x) > (y)) ? ((x) - (y)) : ((y) - (x)); \
if (delta > epsilon) { \
micro_test::reporter->Report( \
#x " (%f) near " #y " (%f) failed at %s:%d", static_cast<float>(x), \
static_cast<float>(y), __FILE__, __LINE__); \
micro_test::did_test_fail = true; \
} \
#define TF_LITE_MICRO_EXPECT_NEAR(x, y, epsilon) \
do { \
auto vx = (x); \
auto vy = (y); \
auto delta = ((vx) > (vy)) ? ((vx) - (vy)) : ((vy) - (vx)); \
if (delta > epsilon) { \
micro_test::reporter->Report( \
#x " (%f) near " #y " (%f) failed at %s:%d", static_cast<float>(vx), \
static_cast<float>(vy), __FILE__, __LINE__); \
micro_test::did_test_fail = true; \
} \
} while (false)
#define TF_LITE_MICRO_EXPECT_GT(x, y) \

View File

@ -0,0 +1,30 @@
/* Copyright 2020 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 "tensorflow/lite/micro/testing/micro_test.h"
#include "tensorflow/lite/micro/testing/test_utils.h"
TF_LITE_MICRO_TESTS_BEGIN
TF_LITE_MICRO_TEST(ArgumentsExecutedOnlyOnce) {
float count = 0.;
// Make sure either argument is executed once after macro expansion.
TF_LITE_MICRO_EXPECT_NEAR(0, count++, 0.1);
TF_LITE_MICRO_EXPECT_NEAR(1, count++, 0.1);
TF_LITE_MICRO_EXPECT_NEAR(count++, 2, 0.1);
TF_LITE_MICRO_EXPECT_NEAR(count++, 3, 0.1);
}
TF_LITE_MICRO_TESTS_END