1#!/bin/bash
2# Copyright 2019 The TensorFlow Authors. 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# Bash unit tests for the TensorFlow Lite Micro project generator.
18
19set -e
20
21INPUT_FILE=${TEST_TMPDIR}/input.tflite
22printf "\x00\x01\x02\x03" > ${INPUT_FILE}
23
24OUTPUT_SOURCE_FILE=${TEST_TMPDIR}/output_source.cc
25OUTPUT_HEADER_FILE=${TEST_TMPDIR}/output_header.h
26
27# Needed for copybara compatibility.
28SCRIPT_BASE_DIR=/org_"tensor"flow
29${TEST_SRCDIR}${SCRIPT_BASE_DIR}/tensorflow/lite/python/convert_file_to_c_source \
30  --input_tflite_file="${INPUT_FILE}" \
31  --output_source_file="${OUTPUT_SOURCE_FILE}" \
32  --output_header_file="${OUTPUT_HEADER_FILE}" \
33  --array_variable_name="g_some_array" \
34  --line_width=80 \
35  --include_guard="SOME_GUARD_H_" \
36  --include_path="some/guard.h" \
37  --use_tensorflow_license=True
38
39if ! grep -q 'const unsigned char g_some_array' ${OUTPUT_SOURCE_FILE}; then
40  echo "ERROR: No array found in output '${OUTPUT_SOURCE_FILE}'"
41  exit 1
42fi
43
44if ! grep -q '0x00, 0x01, 0x02, 0x03' ${OUTPUT_SOURCE_FILE}; then
45  echo "ERROR: No array values found in output '${OUTPUT_SOURCE_FILE}'"
46  exit 1
47fi
48
49if ! grep -q 'const int g_some_array_len = 4;' ${OUTPUT_SOURCE_FILE}; then
50  echo "ERROR: No array length found in output '${OUTPUT_SOURCE_FILE}'"
51  exit 1
52fi
53
54if ! grep -q 'The TensorFlow Authors. All Rights Reserved' ${OUTPUT_SOURCE_FILE}; then
55  echo "ERROR: No license found in output '${OUTPUT_SOURCE_FILE}'"
56  exit 1
57fi
58
59if ! grep -q '\#include "some/guard\.h"' ${OUTPUT_SOURCE_FILE}; then
60  echo "ERROR: No include found in output '${OUTPUT_SOURCE_FILE}'"
61  exit 1
62fi
63
64
65if ! grep -q '#ifndef SOME_GUARD_H_' ${OUTPUT_HEADER_FILE}; then
66  echo "ERROR: No include guard found in output '${OUTPUT_HEADER_FILE}'"
67  exit 1
68fi
69
70if ! grep -q 'extern const unsigned char g_some_array' ${OUTPUT_HEADER_FILE}; then
71  echo "ERROR: No array found in output '${OUTPUT_HEADER_FILE}'"
72  exit 1
73fi
74
75if ! grep -q 'extern const int g_some_array_len;' ${OUTPUT_HEADER_FILE}; then
76  echo "ERROR: No array length found in output '${OUTPUT_HEADER_FILE}'"
77  exit 1
78fi
79
80if ! grep -q 'The TensorFlow Authors. All Rights Reserved' ${OUTPUT_HEADER_FILE}; then
81  echo "ERROR: No license found in output '${OUTPUT_HEADER_FILE}'"
82  exit 1
83fi
84
85
86echo
87echo "SUCCESS: convert_file_to_c_source test PASSED"
88