Home | History | Annotate | Download | only in core
      1 # Copyright (C) 2012 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 #
     15 # Definitions for the Android NDK build system's internal unit tests.
     16 #
     17 
     18 #
     19 # A function which names begin with -test- (e.g. -test-foo) is assumed
     20 # to be an internal unit test. It will be run automatically by ndk-build
     21 # if NDK_UNIT_TESTS is defined in your environment.
     22 #
     23 # Each test should call one of the following functions that will
     24 # register a failure:
     25 #
     26 #   $(call test-expect,<expected-value>,<actual-value>)
     27 #
     28 #      This will check that <actual-value> is equal to <expected-value>.
     29 #      If not, this will print an error message and increment the failure
     30 #      counter.
     31 #
     32 #   $(call test-assert,<expected-value>,<actual-value>)
     33 #
     34 #      This is similar to test-expect, though it will abort the program
     35 #      immediately after displaying an error message.
     36 #
     37 # Here's an example that checks that the 'filter' function works correctly:
     38 #
     39 #   -test-filter = \
     40 #     $(call test-expect,foo,$(filter bar,foo bar))
     41 #
     42 #
     43 
     44 -ndk-test-start = \
     45   $(eval _test_name := $1)\
     46   $(eval _test_list += $1)\
     47   $(eval _test_failed :=)\
     48   $(info [$1  RUN])
     49 
     50 # End current unit test.
     51 #
     52 -ndk-test-end = \
     53   $(if $(_test_failed),\
     54     $(info [$(_test_name) FAIL])$(error Aborting)\
     55     $(eval _test_failures += $$(_test_name))\
     56   ,\
     57     $(info [$(_test_name)   OK])\
     58   )
     59 
     60 # Define NDK_UNIT_TESTS to 2 to dump each test-expect/assert check.
     61 #
     62 ifeq (2,$(NDK_UNIT_TESTS))
     63 -ndk-test-log = $(info .  $(_test_name): $1)
     64 else
     65 -ndk-test-log = $(empty)
     66 endif
     67 
     68 test-expect = \
     69   $(call -ndk-test-log,expect '$2' == '$1')\
     70   $(if $(call sne,$1,$2),\
     71     $(info ERROR <$(_test_name)>:$3)\
     72     $(info .  expected value:'$1')\
     73     $(info .  actual value:  '$2')\
     74     $(eval _test_failed := true)\
     75   )
     76 
     77 test-assert = \
     78   $(call -ndk-test-log,assert '$2' == '$1')\
     79   $(if $(call sne,$1,$2),\
     80     $(info ASSERT <$(_test_name)>:$3)\
     81     $(info .  expected value:'$1')\
     82     $(info .  actual value:  '$2')\
     83     $(eval _test_failed := true)\
     84     $(error Aborting.)\
     85   )
     86 
     87 # Run all the tests, i.e. all functions that are defined with a -test-
     88 # prefix will be called now in succession.
     89 ndk-run-all-tests = \
     90   $(info ================= STARTING NDK-BUILD UNIT TESTS =================)\
     91   $(eval _test_list :=)\
     92   $(eval _test_failures :=)\
     93   $(foreach _test,$(filter -test-%,$(.VARIABLES)),\
     94     $(call -ndk-test-start,$(_test))\
     95     $(call $(_test))\
     96     $(call -ndk-test-end)\
     97   )\
     98   $(eval _test_count := $$(words $$(_test_list)))\
     99   $(eval _test_fail_count := $$(words $$(_test_failures)))\
    100   $(if $(_test_failures),\
    101     $(info @@@@@@@@@@@ FAILED $(_test_fail_count) of $(_test_count) NDK-BUILD UNIT TESTS @@@@@@@)\
    102     $(foreach _test_name,$(_test_failures),\
    103       $(info .  $(_test_name)))\
    104   ,\
    105     $(info =================== PASSED $(_test_count) NDK-BUILD UNIT TESTS =================)\
    106   )
    107