Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tests / scripts / command / nnpkg-test
1 #!/bin/bash
2
3 set -u
4
5 command_exists() {
6         command -v "$@" > /dev/null 2>&1
7 }
8
9 progname=$(basename "${BASH_SOURCE[0]}")
10 indir="."
11 outdir="."
12 nnpkg_run=${nnpkg_run:-"nnpackage_run"}
13 difftool=${difftool:-"h5diff"}
14 delete_dumped_on_failure=0
15
16 usage() {
17   echo "Usage: $0 $progname [options] nnpackage_test"
18   echo "Run an nnpackage testcase"
19   echo ""
20   echo "Returns"
21   echo "     0       success"
22   echo "  non-zero   failure"
23   echo ""
24   echo "Options:"
25   echo "    -h   show this help"
26   echo "    -i   set input directory (default=$indir)"
27   echo "    -o   set output directory (default=$outdir)"
28   echo "    -d   delete dumped file on failure."
29   echo "         (dumped file are always deleted on success) (default=$delete_dumped_on_failure)"
30   echo ""
31   echo "Environment variables:"
32   echo "   nnpackage_run    path to nnpackage_run (default=nnpackage_run)"
33   echo "   difftool         path to i5diff or h5diff (default=h5diff)"
34   echo ""
35   echo "Examples:"
36   echo "    $0 $progname Add_000                => run $indir/Add_000 and check output"
37   echo "    $0 $progname -i nnpkg-tcs Add_000   => run nnpkg-tcs/Add_000 and check output"
38   exit 1
39 }
40
41 if [ $# -eq 0 ]; then
42   echo "For help, type $progname -h"
43   exit 1
44 fi
45
46 while getopts "hdi:o:" OPTION; do
47 case "${OPTION}" in
48     h) usage;;
49     d) delete_dumped_on_failure=1;;
50     i) indir=$OPTARG;;
51     o) outdir=$OPTARG;;
52     ?) exit 1;;
53 esac
54 done
55
56 shift $((OPTIND-1))
57
58 if [ $# -ne 1 ]; then
59   echo "error: wrong argument (no argument or too many arguments)."
60   echo "For help, type $progname -h"
61   exit 1
62 fi
63
64 tcname=$(basename "$1")
65 nnpkg="$indir/$tcname"
66
67 # run
68
69 if [ ! -e $nnpkg ]; then
70   echo "error: nnpackage "$nnpkg" does not exist."
71   exit 1
72 fi
73
74 if ! command_exists $nnpkg_run; then
75   echo "error: runner "$nnpkg_run" does not exist."
76   echo "       if $nnpkg_run exists, please set PATH to $nnpkg_run"
77   exit 1
78 fi
79
80 dumped="$outdir/$tcname".out.h5
81
82 echo -n "[  Run  ] $nnpkg "
83
84 if $nnpkg_run \
85 --nnpackage "$nnpkg" \
86 --load "$nnpkg/metadata/tc/input.h5" \
87 --dump "$dumped" >& /dev/null > "$dumped.log" 2>&1 ; then
88   echo -e "\tPass"
89   rm "$dumped.log"
90 else
91   echo -e "\tFail"
92   echo ""
93   cat "$dumped.log"
94   echo ""
95   rm "$dumped.log"
96   exit 2
97 fi
98
99 # diff
100
101 if ! command_exists $difftool; then
102   echo "error: difftool "$difftool" does not exist."
103   exit 1
104 fi
105
106 expected="$nnpkg/metadata/tc/expected.h5"
107
108 echo -n "[Compare] $nnpkg "
109
110 test_fail()
111 {
112   echo -e "\tFail"
113   [ $delete_dumped_on_failure ] && rm "$dumped"
114   cat "$dumped.log"
115   rm "$dumped.log"
116   exit 3
117 }
118
119 test_pass()
120 {
121   echo -e "\tPass"
122   cat "$dumped.log"
123   rm "$dumped" "$dumped.log"
124 }
125
126 if ! $difftool -d 0.001 -v "$dumped" "$expected" /value >& "$dumped.log"; then
127   test_fail
128 elif grep "not comparable" "$dumped.log" > /dev/null; then
129   test_fail
130 else
131   test_pass
132 fi