|
| 1 | +/************************************************************************* |
| 2 | +** |
| 3 | +** GSC-18128-1, "Core Flight Executive Version 6.7" |
| 4 | +** |
| 5 | +** Copyright (c) 2006-2019 United States Government as represented by |
| 6 | +** the Administrator of the National Aeronautics and Space Administration. |
| 7 | +** All Rights Reserved. |
| 8 | +** |
| 9 | +** Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +** you may not use this file except in compliance with the License. |
| 11 | +** You may obtain a copy of the License at |
| 12 | +** |
| 13 | +** http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +** |
| 15 | +** Unless required by applicable law or agreed to in writing, software |
| 16 | +** distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +** See the License for the specific language governing permissions and |
| 19 | +** limitations under the License. |
| 20 | +** |
| 21 | +*************************************************************************/ |
| 22 | + |
| 23 | +/** |
| 24 | + * @file |
| 25 | + * |
| 26 | + * Functional test of ES Generic Counter APIs |
| 27 | + */ |
| 28 | + |
| 29 | +#include "cfe_test.h" |
| 30 | + |
| 31 | +void TestCounterCreateDelete(void) |
| 32 | +{ |
| 33 | + CFE_ES_CounterId_t Ids[CFE_PLATFORM_ES_MAX_GEN_COUNTERS]; |
| 34 | + CFE_ES_CounterId_t TestId; |
| 35 | + CFE_ES_CounterId_t CheckId; |
| 36 | + char CounterName[CFE_MISSION_MAX_API_LEN]; |
| 37 | + char CheckName[CFE_MISSION_MAX_API_LEN]; |
| 38 | + CFE_Status_t Status; |
| 39 | + uint32 NumCounters; |
| 40 | + uint32 Idx; |
| 41 | + |
| 42 | + snprintf(CounterName, sizeof(CounterName), "ut"); |
| 43 | + |
| 44 | + /* Confirm proper bad argument rejection */ |
| 45 | + UtAssert_INT32_EQ(CFE_ES_RegisterGenCounter(&Ids[0], NULL), CFE_ES_BAD_ARGUMENT); |
| 46 | + UtAssert_INT32_EQ(CFE_ES_RegisterGenCounter(NULL, CounterName), CFE_ES_BAD_ARGUMENT); |
| 47 | + |
| 48 | + /* Create up to CFE_PLATFORM_ES_MAX_GEN_COUNTERS and confirm success */ |
| 49 | + for (NumCounters = 0; NumCounters < CFE_PLATFORM_ES_MAX_GEN_COUNTERS; ++NumCounters) |
| 50 | + { |
| 51 | + snprintf(CounterName, sizeof(CounterName), "C%u", (unsigned int)NumCounters); |
| 52 | + Status = CFE_ES_RegisterGenCounter(&Ids[NumCounters], CounterName); |
| 53 | + if (Status != CFE_SUCCESS) |
| 54 | + { |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /* Confirm that the expected number of counters were created */ |
| 60 | + UtAssert_UINT32_EQ(NumCounters, CFE_PLATFORM_ES_MAX_GEN_COUNTERS); |
| 61 | + |
| 62 | + /* Attempt to create one too many */ |
| 63 | + snprintf(CounterName, sizeof(CounterName), "extra"); |
| 64 | + UtAssert_INT32_EQ(CFE_ES_RegisterGenCounter(&TestId, CounterName), CFE_ES_NO_RESOURCE_IDS_AVAILABLE); |
| 65 | + |
| 66 | + /* pick a single counter ID from the middle of the set for more detail testing of support APIs */ |
| 67 | + TestId = Ids[NumCounters / 2]; |
| 68 | + snprintf(CounterName, sizeof(CounterName), "C%u", (unsigned int)NumCounters / 2); |
| 69 | + |
| 70 | + /* Confirm CFE_ES_CounterID_ToIndex works (nominal) */ |
| 71 | + Idx = UINT32_MAX; |
| 72 | + UtAssert_INT32_EQ(CFE_ES_CounterID_ToIndex(TestId, &Idx), CFE_SUCCESS); |
| 73 | + UtAssert_UINT32_LT(Idx, CFE_PLATFORM_ES_MAX_GEN_COUNTERS); |
| 74 | + |
| 75 | + /* Confirm proper rejection of bad args in CFE_ES_CounterID_ToIndex */ |
| 76 | + UtAssert_INT32_EQ(CFE_ES_CounterID_ToIndex(CFE_ES_COUNTERID_UNDEFINED, &Idx), CFE_ES_ERR_RESOURCEID_NOT_VALID); |
| 77 | + UtAssert_INT32_EQ(CFE_ES_CounterID_ToIndex(TestId, NULL), CFE_ES_BAD_ARGUMENT); |
| 78 | + |
| 79 | + /* Confirm conversion To/From Name */ |
| 80 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterIDByName(&CheckId, CounterName), CFE_SUCCESS); |
| 81 | + cFE_FTAssert_ResourceID_EQ(CheckId, TestId); |
| 82 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterName(CheckName, TestId, sizeof(CheckName)), CFE_SUCCESS); |
| 83 | + UtAssert_STRINGBUF_EQ(CheckName, sizeof(CheckName), CounterName, sizeof(CounterName)); |
| 84 | + |
| 85 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterIDByName(&CheckId, "na"), CFE_ES_ERR_NAME_NOT_FOUND); |
| 86 | + |
| 87 | + /* Confirm proper rejection of bad args in conversion To/From Name */ |
| 88 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterIDByName(NULL, CounterName), CFE_ES_BAD_ARGUMENT); |
| 89 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterIDByName(&CheckId, NULL), CFE_ES_BAD_ARGUMENT); |
| 90 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterName(CheckName, CFE_ES_COUNTERID_UNDEFINED, sizeof(CounterName)), |
| 91 | + CFE_ES_ERR_RESOURCEID_NOT_VALID); |
| 92 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterName(CheckName, TestId, 0), CFE_ES_BAD_ARGUMENT); |
| 93 | + UtAssert_INT32_EQ(CFE_ES_GetGenCounterName(NULL, TestId, sizeof(CounterName)), CFE_ES_BAD_ARGUMENT); |
| 94 | + |
| 95 | + /* Confirm proper rejection of bad args in CFE_ES_DeleteGenCounter (this returns CFE_ES_BAD_ARGUMENT instead) */ |
| 96 | + UtAssert_INT32_EQ(CFE_ES_DeleteGenCounter(CFE_ES_COUNTERID_UNDEFINED), CFE_ES_BAD_ARGUMENT); |
| 97 | + |
| 98 | + /* Delete last counter to test duplicate name rejection (needs a free slot) */ |
| 99 | + --NumCounters; |
| 100 | + UtAssert_INT32_EQ(CFE_ES_DeleteGenCounter(Ids[NumCounters]), CFE_SUCCESS); |
| 101 | + snprintf(CounterName, sizeof(CounterName), "C%u", (unsigned int)0); |
| 102 | + UtAssert_INT32_EQ(CFE_ES_RegisterGenCounter(&TestId, CounterName), CFE_ES_ERR_DUPLICATE_NAME); |
| 103 | + |
| 104 | + /* Delete remainder of counters */ |
| 105 | + while (NumCounters > 0) |
| 106 | + { |
| 107 | + Status = CFE_ES_DeleteGenCounter(Ids[NumCounters - 1]); |
| 108 | + if (Status != CFE_SUCCESS) |
| 109 | + { |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + --NumCounters; |
| 114 | + } |
| 115 | + |
| 116 | + UtAssert_ZERO(NumCounters); |
| 117 | +} |
| 118 | + |
| 119 | +void TestCounterGetSet(void) |
| 120 | +{ |
| 121 | + CFE_ES_CounterId_t TestId; |
| 122 | + uint32 CountVal; |
| 123 | + |
| 124 | + /* Setup - create a single counter */ |
| 125 | + UtAssert_INT32_EQ(CFE_ES_RegisterGenCounter(&TestId, "ut"), CFE_SUCCESS); |
| 126 | + |
| 127 | + /* Get and set its count - should be initially 0 */ |
| 128 | + CountVal = UINT32_MAX; |
| 129 | + UtAssert_INT32_EQ(CFE_ES_GetGenCount(TestId, &CountVal), CFE_SUCCESS); |
| 130 | + UtAssert_ZERO(CountVal); |
| 131 | + UtAssert_INT32_EQ(CFE_ES_SetGenCount(TestId, 5), CFE_SUCCESS); |
| 132 | + UtAssert_INT32_EQ(CFE_ES_GetGenCount(TestId, &CountVal), CFE_SUCCESS); |
| 133 | + UtAssert_UINT32_EQ(CountVal, 5); |
| 134 | + UtAssert_INT32_EQ(CFE_ES_IncrementGenCounter(TestId), CFE_SUCCESS); |
| 135 | + UtAssert_INT32_EQ(CFE_ES_GetGenCount(TestId, &CountVal), CFE_SUCCESS); |
| 136 | + UtAssert_UINT32_EQ(CountVal, 6); |
| 137 | + |
| 138 | + /* |
| 139 | + * Confirm bad arg rejection in Get/Set/Increment |
| 140 | + * Note these APIs return CFE_ES_BAD_ARGUMENT rather than |
| 141 | + * CFE_ES_ERR_RESOURCEID_NOT_VALID on a bad ID (historical) |
| 142 | + */ |
| 143 | + UtAssert_INT32_EQ(CFE_ES_GetGenCount(TestId, NULL), CFE_ES_BAD_ARGUMENT); |
| 144 | + UtAssert_INT32_EQ(CFE_ES_GetGenCount(CFE_ES_COUNTERID_UNDEFINED, &CountVal), CFE_ES_BAD_ARGUMENT); |
| 145 | + UtAssert_INT32_EQ(CFE_ES_IncrementGenCounter(CFE_ES_COUNTERID_UNDEFINED), CFE_ES_BAD_ARGUMENT); |
| 146 | + UtAssert_INT32_EQ(CFE_ES_SetGenCount(CFE_ES_COUNTERID_UNDEFINED, 0), CFE_ES_BAD_ARGUMENT); |
| 147 | + |
| 148 | + /* Teardown - delete the counter */ |
| 149 | + UtAssert_INT32_EQ(CFE_ES_DeleteGenCounter(TestId), CFE_SUCCESS); |
| 150 | +} |
| 151 | + |
| 152 | +void ESCounterTestSetup(void) |
| 153 | +{ |
| 154 | + UtTest_Add(TestCounterCreateDelete, NULL, NULL, "Test Counter Create/Delete"); |
| 155 | + UtTest_Add(TestCounterGetSet, NULL, NULL, "Test Counter Get/Set"); |
| 156 | +} |
0 commit comments