TFLite: 怎样使用googletest 测试代码流程

tflite下的每个源文件都有一个测试文件,测试代码的流程,可以使用printf添加打印log,对理解代码很有帮助。

编译运行测试文件

以tensorflow/lite/c/c_api_internal.c为例, 该目录下对应的测试文件 c_api_internal_test.cc

bazel test //tensorflow/lite/c:c_api_internal_test

注意 bazel 后面是test, 后面target的写法类似 build的写法(从workspace开始写//xxx)

test     Builds and runs the specified test targets

 

bazel test //tensorflow/lite/c:c_api_internal_test                                                                                      
WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
/home/ws/code/tensorflow/tools/bazel.rc
INFO: Invocation ID: 3dcab59a-1119-43d0-88f4-215a37f214d1
INFO: Analysed target //tensorflow/lite/c:c_api_internal_test (0 packages loaded, 0 targets configured).
INFO: Found 1 test target...
INFO: From Compiling tensorflow/lite/c/c_api_internal.c:
In file included from tensorflow/lite/c/c_api_internal.c:16:0:
./tensorflow/lite/c/c_api_internal.h:60:34: warning: 'struct TfLiteContext' declared inside parameter list will not be visible outside of this definition or declaration
   TfLiteStatus (*Refresh)(struct TfLiteContext* context);
                                  ^~~~~~~~~~~~~
Target //tensorflow/lite/c:c_api_internal_test up-to-date:
  bazel-bin/tensorflow/lite/c/c_api_internal_test
INFO: Elapsed time: 0.541s, Critical Path: 0.41s
INFO: 4 processes: 4 linux-sandbox.
INFO: Build completed successfully, 6 total actions
//tensorflow/lite/c:c_api_internal_test                                  PASSED in 0.1s

INFO: Build completed successfully, 6 total actions
 

运行结果

[==========] Running 4 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from IntArray
[ RUN      ] IntArray.TestIntArrayCreate
[       OK ] IntArray.TestIntArrayCreate (0 ms)
[ RUN      ] IntArray.TestIntArrayCopy
[       OK ] IntArray.TestIntArrayCopy (0 ms)
[ RUN      ] IntArray.TestIntArrayEqual
[       OK ] IntArray.TestIntArrayEqual (0 ms)
[----------] 3 tests from IntArray (0 ms total)

[----------] 1 test from Types
[ RUN      ] Types.TestTypeNames
[       OK ] Types.TestTypeNames (0 ms)
[----------] 1 test from Types (0 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 4 tests.
 

添加log

为理解代码添加日志如下

  int TfLiteIntArrayGetSizeInBytes(int size) {
    static TfLiteIntArray dummy;
>>  printf("input size %d, SizeInBytes %d\n", size, (sizeof(dummy) + sizeof(dummy.data[0]) * size));                                                                                                        
    return sizeof(dummy) + sizeof(dummy.data[0]) * size;
  }


再次运行编译、运行过程

可以看到添加的log输出

[==========] Running 4 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from IntArray
[ RUN      ] IntArray.TestIntArrayCreate
input size 0, SizeInBytes 4
input size 3, SizeInBytes 16

[       OK ] IntArray.TestIntArrayCreate (0 ms)
[ RUN      ] IntArray.TestIntArrayCopy
input size 2, SizeInBytes 12
input size 2, SizeInBytes 12
[       OK ] IntArray.TestIntArrayCopy (0 ms)
[ RUN      ] IntArray.TestIntArrayEqual
input size 1, SizeInBytes 8
input size 2, SizeInBytes 12
input size 2, SizeInBytes 12
input size 2, SizeInBytes 12

[       OK ] IntArray.TestIntArrayEqual (0 ms)
[----------] 3 tests from IntArray (0 ms total)

[----------] 1 test from Types
[ RUN      ] Types.TestTypeNames
[       OK ] Types.TestTypeNames (0 ms)
[----------] 1 test from Types (0 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 4 tests.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值