rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2017 MediaTek Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <err.h> |
| 24 | #include <mtk_wrapper.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unittest.h> |
| 28 | #include <platform.h> |
| 29 | |
| 30 | /** |
| 31 | * lib/unittest defines several macros for unittest, |
| 32 | * EXPECT_EQ, EXPECT_NEQ, EXPECT_LE, EXPECT_LT, EXPECT_GE, EXPECT_GT, |
| 33 | * EXPECT_TRUE, EXPECT_FALSE, EXPECT_BYTES_EQ, EXPECT_BYTES_NE, EXPECT_EQ_LL, |
| 34 | * ASSERT_NOT_NULL. |
| 35 | * For detail, please refer to lib/unittest/include/unittest.h |
| 36 | */ |
| 37 | //irq function unittest |
| 38 | bool timerEnable = false; |
| 39 | #define ARM_GENERIC_TIMER_PHYSICAL_INT 30 |
| 40 | |
| 41 | static enum wrap_handler_return wrap_plat_platform_timer_handle(void *arg) |
| 42 | { |
| 43 | timerEnable = true; |
| 44 | plat_wrap_mask_interrupt(ARM_GENERIC_TIMER_PHYSICAL_INT); |
| 45 | return WRAP_INT_NO_RESCHEDULE; |
| 46 | } |
| 47 | |
| 48 | static bool test_plat_wrap_irq_function(void) |
| 49 | { |
| 50 | #define WRAP_LEVEL_SENSITIVE 1 |
| 51 | #define WRAP_POLARITY_LOW 0 |
| 52 | int count = 5; |
| 53 | |
| 54 | plat_wrap_irq_set_polarity(ARM_GENERIC_TIMER_PHYSICAL_INT, WRAP_LEVEL_SENSITIVE); |
| 55 | plat_wrap_irq_set_sensitive(ARM_GENERIC_TIMER_PHYSICAL_INT, WRAP_POLARITY_LOW); |
| 56 | plat_wrap_register_int_handler(ARM_GENERIC_TIMER_PHYSICAL_INT, &wrap_plat_platform_timer_handle, NULL); |
| 57 | plat_wrap_unmask_interrupt(ARM_GENERIC_TIMER_PHYSICAL_INT); |
| 58 | while (1) { |
| 59 | if (count > 0) { |
| 60 | plat_wrap_delay(1000000); |
| 61 | if (timerEnable) { |
| 62 | break; |
| 63 | } |
| 64 | count --; |
| 65 | } else |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | //delay function unittest |
| 73 | static bool test_plat_wrap_delay_function(void) |
| 74 | { |
| 75 | BEGIN_TEST; |
| 76 | |
| 77 | unsigned long long startTime = 0; |
| 78 | unsigned long long endTime = 0; |
| 79 | int delayTime = 1000000; //one second |
| 80 | int delayThreshold = 500; |
| 81 | |
| 82 | startTime = current_time_hires(); |
| 83 | plat_wrap_delay(1000000); |
| 84 | endTime = current_time_hires(); |
| 85 | EXPECT_LE((delayTime - delayThreshold), endTime - startTime, "delay time is less than expected value"); |
| 86 | EXPECT_GE((delayTime + delayThreshold), endTime - startTime, "delay time is greater than expected value"); |
| 87 | |
| 88 | END_TEST; |
| 89 | } |
| 90 | |
| 91 | //address translate function unittest |
| 92 | static bool test_plat_wrap_addr_trans_function(void) |
| 93 | { |
| 94 | #define EMI_PHY_BASE (0x10203000) |
| 95 | |
| 96 | BEGIN_TEST; |
| 97 | |
| 98 | unsigned long paddr; |
| 99 | void *vaddr = plat_wrap_paddr_to_kvaddr(EMI_PHY_BASE); |
| 100 | paddr = plat_wrap_kvaddr_to_paddr(vaddr); |
| 101 | EXPECT_EQ(EMI_PHY_BASE, paddr, "fail to virtual/phyical address translate"); |
| 102 | |
| 103 | END_TEST; |
| 104 | } |
| 105 | |
| 106 | //mutex function unittest |
| 107 | wrap_mutex m; |
| 108 | |
| 109 | static bool test_plat_wrap_mutex_function(void) |
| 110 | { |
| 111 | BEGIN_TEST; |
| 112 | |
| 113 | plat_wrap_mutex_init(&m); |
| 114 | EXPECT_EQ(NO_ERROR, plat_wrap_mutex_acquire(&m), "fail to mutex acquire"); |
| 115 | EXPECT_EQ(NO_ERROR, plat_wrap_mutex_release(&m), "fail to mutex release"); |
| 116 | plat_wrap_mutex_destroy(&m); |
| 117 | |
| 118 | END_TEST; |
| 119 | } |
| 120 | |
| 121 | //semphore function unittest |
| 122 | wrap_sem sem; |
| 123 | static bool test_plat_wrap_sem_function(void) |
| 124 | { |
| 125 | BEGIN_TEST; |
| 126 | |
| 127 | unsigned int value = 1; |
| 128 | plat_wrap_sem_init(&sem, value); |
| 129 | EXPECT_EQ(NO_ERROR, plat_wrap_sem_wait(&sem), "fail to semaphore wait function"); |
| 130 | EXPECT_EQ(NO_ERROR, plat_wrap_sem_post(&sem), "fail to semaphore post function"); |
| 131 | plat_wrap_sem_destroy(&sem); |
| 132 | |
| 133 | END_TEST; |
| 134 | } |
| 135 | |
| 136 | static bool test_plat_wrap_sem_trywait_function(void) |
| 137 | { |
| 138 | BEGIN_TEST; |
| 139 | |
| 140 | unsigned int value = 1; |
| 141 | plat_wrap_sem_init(&sem, value); |
| 142 | EXPECT_EQ(NO_ERROR, plat_wrap_sem_trywait(&sem), "fail to semaphore trywait function"); |
| 143 | EXPECT_EQ(NO_ERROR, plat_wrap_sem_post(&sem), "fail to semaphore post function"); |
| 144 | plat_wrap_sem_destroy(&sem); |
| 145 | |
| 146 | END_TEST; |
| 147 | } |
| 148 | |
| 149 | static bool test_plat_wrap_sem_timedwait_function(void) |
| 150 | { |
| 151 | BEGIN_TEST; |
| 152 | |
| 153 | unsigned int value = 1; |
| 154 | uint32_t timeout = 1; |
| 155 | plat_wrap_sem_init(&sem, value); |
| 156 | EXPECT_EQ(NO_ERROR, plat_wrap_sem_timedwait(&sem, timeout), "fail to semaphore timedwait function"); |
| 157 | EXPECT_EQ(NO_ERROR, plat_wrap_sem_post(&sem), "fail to semaphore post function"); |
| 158 | plat_wrap_sem_destroy(&sem); |
| 159 | |
| 160 | END_TEST; |
| 161 | } |
| 162 | |
| 163 | BEGIN_TEST_CASE(wrapper_tests); |
| 164 | RUN_TEST(test_plat_wrap_irq_function); |
| 165 | RUN_TEST(test_plat_wrap_delay_function); |
| 166 | RUN_TEST(test_plat_wrap_addr_trans_function); |
| 167 | RUN_TEST(test_plat_wrap_mutex_function); |
| 168 | RUN_TEST(test_plat_wrap_sem_function); |
| 169 | RUN_TEST(test_plat_wrap_sem_trywait_function); |
| 170 | RUN_TEST(test_plat_wrap_sem_timedwait_function); |
| 171 | END_TEST_CASE(wrapper_tests); |