summaryrefslogtreecommitdiff
path: root/tests/nnapi/specs/generate_test.sh
blob: 2f1afbc66cceee03d437089e66a24486fe8d41a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash

#
# Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
# Copyright (C) 2017 The Android Open Source Project
#
# 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.

NNAPI_VERSION="
V1_0
V1_1
Ex
"

# Process one test spec, and optionally provide the log file argument
# for the slicing tool. The first argument is the test spec file; the
# second optional argument specifies the log file this test should dump
# results into. Only used by the test slicing tool to collect reference
# outputs from the CPU. Also, it outputs the right #includes in the
# test harness so the test would be invoked by TestGenerated.cpp
#
# This function shouldn't be directly called from other scripts. Use
# generate_wrapper below for generating models and examples and updating the
# test framework in one shot.

spec_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
export NNAPI_BASE=$(readlink -f "$spec_dir/../../..")
[ -z "$TEST_DIR" ] && TEST_DIR="tests/nnapi"

function generate_one_testcase {
  # Generate one testcase
  local LOGFILE=$2
  if [ -n "$2" ]; then
    local LOGFILE=", \"$2\""
  fi
  local BASENAME=`basename $1`
  BASENAME=${BASENAME%".mod.py"};
  local EXAMPLE="-e $NNAPI_BASE/$TEST_DIR/src/generated/examples/$BASENAME.example.cpp"

  $NNAPI_BASE/$TEST_DIR/nnapi_test_generator/android-p/test_generator.py ./`basename $1`\
    -m $NNAPI_BASE/$TEST_DIR/src/generated/models/$BASENAME.model.cpp $EXAMPLE
  ret=$?
  # Paste these lines into TestGenerated.cpp
  echo
  echo namespace $BASENAME {
  echo std::vector\<MixedTypedExample\> examples \= {
  echo // Generated $BASENAME test
  echo \#include \"generated/examples/$BASENAME.example.cpp\"
  echo }\;
  echo // Generated model constructor
  echo \#include \"generated/models/$BASENAME.model.cpp\"
  echo }  // namespace $BASENAME
  echo TEST_F\(GeneratedTests\, $BASENAME\) {
  echo '    execute'\($BASENAME\:\:CreateModel\,
  echo '            '$BASENAME\:\:is_ignored\,
  echo '            '$BASENAME\:\:examples${LOGFILE}\)\;
  echo }
  return $ret
}

# Driver for generate_one_testcase. Append the output of generate_one_testcase
# (which are C++ snippets that invokes the test harness) to the
# all_generated_tests.cpp
# Optionally, the "LOG" file ($2), only used by the slicing tool, would be
# passed to generate_one_testcase.
#
# This function should be called to process one test spec from other scripts.
function generate_wrapper {
  local LOGFILE=""
  if [ $1 = "log" ]; then
    local LOGFILE=$2
    shift
    shift
  fi

  mkdir -vp $NNAPI_BASE/$TEST_DIR/src/generated/models
  mkdir -vp $NNAPI_BASE/$TEST_DIR/src/generated/examples

  cd $NNAPI_BASE/$TEST_DIR/specs

  for ver in $NNAPI_VERSION;
  do
    OUTFILE=$NNAPI_BASE/$TEST_DIR/src/generated/all_generated_${ver}_cts_tests.cpp
    echo "// DO NOT EDIT;" > $OUTFILE
    echo "// Generated by ${TEST_DIR}/specs/generate_test.sh" >> $OUTFILE

    VER_DIR=$NNAPI_BASE/$TEST_DIR/specs/$ver
    [ ! -d $VER_DIR ] && continue
    cd $VER_DIR
    for f in $@;
    do
      generate_one_testcase $f $LOGFILE >> $OUTFILE
      if [ $? -ne 0 ]; then
        echo "Failed processing $f"
        return $?
      fi
    done
  done
  return $?
}

# Only run the following when not sourced by another script
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  if [ $# -eq 0 ]; then
    FILES=*.mod.py
  else
    FILES="$@"
  fi
  generate_wrapper $FILES
  if [ $? -ne 0 ]; then
    exit $?
  fi
  echo "Generated file in ${TEST_DIR}/src/generated/"`basename $OUTFILE`
fi # [[ "${BASH_SOURCE[0]}" == "${0}" ]]