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_REGULAR_FILE=${TEST_TMPDIR}/input_regular.cc
22cat << EOF > ${INPUT_REGULAR_FILE}
23#include <stdio.h>
24#include "foo.h"
25#include "bar/fish.h"
26#include "baz.h"
27#ifndef __ANDROID__
28  #include "something.h"
29#endif  // __ANDROID__
30
31int main(int argc, char** argv) {
32  fprintf(stderr, "Hello World!\n");
33  return 0;
34}
35EOF
36
37OUTPUT_REGULAR_FILE=${TEST_TMPDIR}/output_regular.cc
38THIRD_PARTY_HEADERS="subdir/foo.h subdir_2/include/bar/fish.h subdir_3/something.h"
39
40${TEST_SRCDIR}/tensorflow/lite/micro/tools/make/transform_source \
41  --platform=arduino \
42  --third_party_headers="${THIRD_PARTY_HEADERS}" \
43  < ${INPUT_REGULAR_FILE} \
44  > ${OUTPUT_REGULAR_FILE}
45
46if ! grep -q '#include <stdio.h>' ${OUTPUT_REGULAR_FILE}; then
47  echo "ERROR: No stdio.h include found in output '${OUTPUT_REGULAR_FILE}'"
48  exit 1
49fi
50
51if ! grep -q '#include "subdir/foo.h"' ${OUTPUT_REGULAR_FILE}; then
52  echo "ERROR: No subdir/foo.h include found in output '${OUTPUT_REGULAR_FILE}'"
53  exit 1
54fi
55
56if ! grep -q '#include "subdir_2/include/bar/fish.h"' ${OUTPUT_REGULAR_FILE}; then
57  echo "ERROR: No subdir_2/include/bar/fish.h include found in output '${OUTPUT_REGULAR_FILE}'"
58  exit 1
59fi
60
61if ! grep -q '#include "baz.h"' ${OUTPUT_REGULAR_FILE}; then
62  echo "ERROR: No baz.h include found in output '${OUTPUT_REGULAR_FILE}'"
63  exit 1
64fi
65
66if ! grep -q '#include "subdir_3/something.h"' ${OUTPUT_REGULAR_FILE}; then
67  echo "ERROR: No subdir_3/something.h include found in output '${OUTPUT_REGULAR_FILE}'"
68  exit 1
69fi
70
71if ! grep -q 'int tflite_micro_main(' ${OUTPUT_REGULAR_FILE}; then
72  echo "ERROR: No int tflite_micro_main() definition found in output '${OUTPUT_REGULAR_FILE}'"
73  exit 1
74fi
75
76
77INPUT_EXAMPLE_INO_FILE=${TEST_TMPDIR}/input_example_ino.cc
78cat << EOF > ${INPUT_EXAMPLE_INO_FILE}
79#include <stdio.h>
80#include "foo.h"
81#include "tensorflow/lite/micro/examples/something/foo/fish.h"
82#include "baz.h"
83
84void setup() {
85}
86
87void loop() {
88}
89EOF
90
91OUTPUT_EXAMPLE_INO_FILE=${TEST_TMPDIR}/output_regular.cc
92
93${TEST_SRCDIR}/tensorflow/lite/micro/tools/make/transform_source \
94  --platform=arduino \
95  --third_party_headers="${THIRD_PARTY_HEADERS}" \
96  --is_example_ino \
97  < ${INPUT_EXAMPLE_INO_FILE} \
98  > ${OUTPUT_EXAMPLE_INO_FILE}
99
100if ! grep -q '#include <TensorFlowLite.h>' ${OUTPUT_EXAMPLE_INO_FILE}; then
101  echo "ERROR: No TensorFlowLite.h include found in output '${OUTPUT_EXAMPLE_INO_FILE}'"
102  exit 1
103fi
104
105if ! grep -q '#include "foo_fish.h"' ${OUTPUT_EXAMPLE_INO_FILE}; then
106  echo "ERROR: No foo/fish.h include found in output '${OUTPUT_EXAMPLE_INO_FILE}'"
107  exit 1
108fi
109
110INPUT_EXAMPLE_SOURCE_FILE=${TEST_TMPDIR}/input_example_source.h
111cat << EOF > ${INPUT_EXAMPLE_SOURCE_FILE}
112#include <stdio.h>
113#include "foo.h"
114#include "foo/fish.h"
115#include "baz.h"
116#include "tensorflow/lite/micro/examples/something/cube/tri.h"
117
118void setup() {
119}
120
121void loop() {
122}
123
124int main(int argc, char* argv[]) {
125  setup();
126  while (true) {
127    loop();
128  }
129}
130EOF
131
132OUTPUT_EXAMPLE_SOURCE_FILE=${TEST_TMPDIR}/output_example_source.h
133
134${TEST_SRCDIR}/tensorflow/lite/micro/tools/make/transform_source \
135  --platform=arduino \
136  --third_party_headers="${THIRD_PARTY_HEADERS}" \
137  --is_example_source \
138  --source_path="foo/input_example_source.h" \
139  < ${INPUT_EXAMPLE_SOURCE_FILE} \
140  > ${OUTPUT_EXAMPLE_SOURCE_FILE}
141
142if ! grep -q '#include "foo/fish.h"' ${OUTPUT_EXAMPLE_SOURCE_FILE}; then
143  echo "ERROR: No foo/fish.h include found in output '${OUTPUT_EXAMPLE_SOURCE_FILE}'"
144  exit 1
145fi
146
147if ! grep -q '#include "cube_tri.h"' ${OUTPUT_EXAMPLE_SOURCE_FILE}; then
148  echo "ERROR: No cube_tri.h include found in output '${OUTPUT_EXAMPLE_SOURCE_FILE}'"
149  exit 1
150fi
151
152
153echo
154echo "SUCCESS: transform_arduino_source test PASSED"
155