|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Intel Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the Intel Corporation nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + * Author: Bartosz Kokoszko <bartoszx.kokoszko@linux.intel.com> |
| 29 | + */ |
| 30 | + |
| 31 | +#include <sof/audio/component.h> |
| 32 | +#include <errno.h> |
| 33 | + |
| 34 | +#include <stdarg.h> |
| 35 | +#include <stddef.h> |
| 36 | +#include <setjmp.h> |
| 37 | +#include <cmocka.h> |
| 38 | +#include <sof/sof.h> |
| 39 | + |
| 40 | +struct test_case { |
| 41 | + uint16_t in_state; |
| 42 | + int cmd; |
| 43 | + uint16_t out_state; |
| 44 | +}; |
| 45 | + |
| 46 | +static struct test_case tests_succeed[] = { |
| 47 | + {COMP_STATE_PREPARE, COMP_TRIGGER_START, |
| 48 | + COMP_STATE_ACTIVE}, |
| 49 | + {COMP_STATE_PAUSED, COMP_TRIGGER_START, |
| 50 | + COMP_STATE_ACTIVE}, |
| 51 | + {COMP_STATE_PAUSED, COMP_TRIGGER_RELEASE, |
| 52 | + COMP_STATE_ACTIVE}, |
| 53 | + {COMP_STATE_ACTIVE, COMP_TRIGGER_STOP, |
| 54 | + COMP_STATE_PREPARE}, |
| 55 | + {COMP_STATE_ACTIVE, COMP_TRIGGER_XRUN, |
| 56 | + COMP_STATE_PREPARE}, |
| 57 | + {COMP_STATE_ACTIVE, COMP_TRIGGER_PAUSE, |
| 58 | + COMP_STATE_PAUSED}, |
| 59 | + {COMP_STATE_INIT, COMP_TRIGGER_RESET, |
| 60 | + COMP_STATE_READY}, |
| 61 | + {COMP_STATE_READY, COMP_TRIGGER_RESET, |
| 62 | + COMP_STATE_READY}, |
| 63 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_RESET, |
| 64 | + COMP_STATE_READY}, |
| 65 | + {COMP_STATE_PREPARE, COMP_TRIGGER_RESET, |
| 66 | + COMP_STATE_READY}, |
| 67 | + {COMP_STATE_PAUSED, COMP_TRIGGER_RESET, |
| 68 | + COMP_STATE_READY}, |
| 69 | + {COMP_STATE_ACTIVE, COMP_TRIGGER_RESET, |
| 70 | + COMP_STATE_READY}, |
| 71 | + {COMP_STATE_PREPARE, COMP_TRIGGER_PREPARE, |
| 72 | + COMP_STATE_PREPARE}, |
| 73 | + {COMP_STATE_READY, COMP_TRIGGER_PREPARE, |
| 74 | + COMP_STATE_PREPARE} |
| 75 | +}; |
| 76 | + |
| 77 | +static struct test_case tests_fail[] = { |
| 78 | + {COMP_STATE_INIT, COMP_TRIGGER_START}, |
| 79 | + {COMP_STATE_READY, COMP_TRIGGER_START}, |
| 80 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_START}, |
| 81 | + {COMP_STATE_ACTIVE, COMP_TRIGGER_START}, |
| 82 | + {COMP_STATE_INIT, COMP_TRIGGER_RELEASE}, |
| 83 | + {COMP_STATE_READY, COMP_TRIGGER_RELEASE}, |
| 84 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_RELEASE}, |
| 85 | + {COMP_STATE_PREPARE, COMP_TRIGGER_RELEASE}, |
| 86 | + {COMP_STATE_INIT, COMP_TRIGGER_STOP}, |
| 87 | + {COMP_STATE_READY, COMP_TRIGGER_STOP}, |
| 88 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_STOP}, |
| 89 | + {COMP_STATE_PREPARE, COMP_TRIGGER_STOP}, |
| 90 | + {COMP_STATE_PAUSED, COMP_TRIGGER_STOP}, |
| 91 | + {COMP_STATE_INIT, COMP_TRIGGER_XRUN}, |
| 92 | + {COMP_STATE_READY, COMP_TRIGGER_XRUN}, |
| 93 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_XRUN}, |
| 94 | + {COMP_STATE_PREPARE, COMP_TRIGGER_XRUN}, |
| 95 | + {COMP_STATE_PAUSED, COMP_TRIGGER_XRUN}, |
| 96 | + {COMP_STATE_INIT, COMP_TRIGGER_PAUSE}, |
| 97 | + {COMP_STATE_READY, COMP_TRIGGER_PAUSE}, |
| 98 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_PAUSE}, |
| 99 | + {COMP_STATE_PREPARE, COMP_TRIGGER_PAUSE}, |
| 100 | + {COMP_STATE_PAUSED, COMP_TRIGGER_PAUSE}, |
| 101 | + {COMP_STATE_INIT, COMP_TRIGGER_PREPARE}, |
| 102 | + {COMP_STATE_SUSPEND, COMP_TRIGGER_PREPARE}, |
| 103 | + {COMP_STATE_PAUSED, COMP_TRIGGER_PREPARE}, |
| 104 | + {COMP_STATE_ACTIVE, COMP_TRIGGER_PREPARE} |
| 105 | +}; |
| 106 | + |
| 107 | +static void test_audio_component_comp_set_state_succeed(void **state) |
| 108 | +{ |
| 109 | + (void)state; /* unused */ |
| 110 | + |
| 111 | + int i; |
| 112 | + struct comp_dev test_dev; |
| 113 | + |
| 114 | + for (i = 0; i < ARRAY_SIZE(tests_succeed); i++) { |
| 115 | + test_dev.state = tests_succeed[i].in_state; |
| 116 | + assert_int_equal(comp_set_state(&test_dev, |
| 117 | + tests_succeed[i].cmd), 0); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +static void test_audio_component_comp_set_state_correct_output_state(void **state) |
| 122 | +{ |
| 123 | + (void)state; /* unused */ |
| 124 | + |
| 125 | + int i; |
| 126 | + struct comp_dev test_dev; |
| 127 | + |
| 128 | + for (i = 0; i < ARRAY_SIZE(tests_succeed); i++) { |
| 129 | + test_dev.state = tests_succeed[i].in_state; |
| 130 | + comp_set_state(&test_dev, tests_succeed[i].cmd); |
| 131 | + assert_int_equal(test_dev.state, tests_succeed[i].out_state); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +static void test_audio_component_comp_set_state_fail(void **state) |
| 136 | +{ |
| 137 | + (void)state; /* unused */ |
| 138 | + |
| 139 | + int i; |
| 140 | + struct comp_dev test_drv; |
| 141 | + |
| 142 | + for (i = 0; i < ARRAY_SIZE(tests_fail); i++) { |
| 143 | + test_drv.state = tests_fail[i].in_state; |
| 144 | + assert_int_equal(comp_set_state(&test_drv, |
| 145 | + tests_fail[i].cmd), -EINVAL); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +int main(void) |
| 150 | +{ |
| 151 | + const struct CMUnitTest tests[] = { |
| 152 | + cmocka_unit_test(test_audio_component_comp_set_state_succeed), |
| 153 | + cmocka_unit_test(test_audio_component_comp_set_state_correct_output_state), |
| 154 | + cmocka_unit_test(test_audio_component_comp_set_state_fail) |
| 155 | + }; |
| 156 | + |
| 157 | + cmocka_set_message_output(CM_OUTPUT_TAP); |
| 158 | + |
| 159 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 160 | +} |
0 commit comments