Skip to content

Commit 23b225c

Browse files
authored
Merge pull request #554 from jphickey/fix-550-utassert-groups
Fix #550, add function groups to ut assert
2 parents 34e0978 + 9a9171c commit 23b225c

File tree

6 files changed

+216
-136
lines changed

6 files changed

+216
-136
lines changed

ut_assert/inc/utlist.h

+18-34
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ typedef struct UtListNodeTag {
6363
} UtListNode_t;
6464

6565
typedef struct {
66-
UtListNode_t *First;
67-
UtListNode_t *Last;
68-
uint32 NumberOfEntries;
66+
UtListNode_t *Tags;
67+
uint32 NumberOfTags;
6968
} UtListHead_t;
7069

7170
/*
@@ -75,54 +74,39 @@ typedef struct UtListNodeTag {
7574
/* Dynamically allocates a new list head. A list head could also just be declared, this function is useful
7675
* if you need to dynamically allocate memory for a new list head. Note always free list heads allocated by
7776
* this function by calling UtList_Destroy. */
78-
UtListHead_t *UtList_Create(void);
77+
UtListHead_t *UtList_Create(uint32 NumTags);
7978

8079
/* Frees a list head created by UtList_Create. */
8180
void UtList_Destroy(UtListHead_t *ListHead);
8281

8382
/* Deletes all nodes on the list. */
84-
void UtList_Reset(UtListHead_t *ListHead);
83+
void UtList_Reset(UtListNode_t *TagHead);
84+
85+
/* Merge two lists heads together */
86+
void UtList_Merge(UtListNode_t *TagHead1, UtListNode_t *TagHead2);
8587

8688
/* Dynamically adds a new node to the list. Nodes are always added to the end of the list. Memory is dynamically
8789
* allocated for the new node and to hold the data pointed to by Data. A Tag field is also provided to be used to
8890
* store user defined information with the node. */
8991
void UtList_Add(UtListHead_t *ListHead, void *Data, uint32 DataSize, uint32 Tag);
9092

91-
/* Deletes the first node from the list. */
92-
void UtList_DeleteFirst(UtListHead_t *ListHead);
93-
94-
/* Deletes the last node from the list. */
95-
void UtList_DeleteLast(UtListHead_t *ListHead);
96-
9793
/* Deletes the specified node from the list, this will screw up if you do not pass in a valid DeleteNode. I do not
9894
* verify that DeleteNode is a member of the list. */
99-
void UtList_DeleteNode(UtListHead_t *ListHead, UtListNode_t *DeleteNode);
100-
101-
/* Removes the first node from the list by first copying the data from the node to the memory buffer pointed to by the
102-
* specified Data pointer and then the node is deleted from the list. Make sure the destination pointer points to a
103-
* memory buffer large enough to hold the data. The size of the data on the node is available by referencing UtListNode->DataSize. */
104-
void UtList_RemoveFirst(UtListHead_t *ListHead, void *Data);
95+
void UtList_DeleteNode(UtListNode_t *DeleteNode);
10596

106-
/* Removes the last node from the list by first copying the data from the node to the memory buffer pointed to by the
107-
* specified Data pointer and then the node is deleted from the list. Make sure the destination pointer points to a
108-
* memory buffer large enough to hold the data. The size of the data on the node is available by referencing UtListNode->DataSize. */
109-
void UtList_RemoveLast(UtListHead_t *ListHead, void *Data);
110-
111-
/* Removes the speciified RemoveNode from the list by first copying the data from the node to the memory buffer pointed to by the
112-
* specified Data pointer and then the node is deleted from the list. Make sure the destination pointer points to a
113-
* memory buffer large enough to hold the data. The size of the data on the node is available by referencing UtListNode->DataSize. */
114-
void UtList_RemoveNode(UtListHead_t *ListHead, void *Data, UtListNode_t *RemoveNode);
97+
/* Returns true if the list is empty. This is the same as (UtListHead->NumberOfEntries == 0). */
98+
bool UtList_IsEmpty(UtListNode_t *TagHead);
11599

116-
/* Returns a pointer to the first node on the list. This is the same as (UtListHead->First). */
117-
UtListNode_t *UtList_First(UtListHead_t *ListHead);
100+
/* Returns the head node of a list for the given tag */
101+
UtListNode_t *UtList_GetHead(UtListHead_t *ListHead, uint32 Tag);
118102

119-
/* Returns a pointer to the last node on the list. This is the same as (UtListHead->Last). */
120-
UtListNode_t *UtList_Last(UtListHead_t *ListHead);
103+
/* Returns the next node in the list, given the current node */
104+
UtListNode_t *UtList_GetNext(UtListNode_t *ListNode);
121105

122-
/* Returns true if the list is empty. This is the same as (UtListHead->NumberOfEntries == 0). */
123-
bool UtList_IsEmpty(UtListHead_t *ListHead);
106+
/* Returns the data object associated with the current node */
107+
void *UtList_GetObject(UtListNode_t *ListNode);
124108

125-
/* Returns the number of nodes on the list. This is the same as (UtListHead->NumberOfEntries). */
126-
uint32 UtList_Depth(UtListHead_t *ListHead);
109+
/* Check if the current node marks the end of the list */
110+
bool UtList_IsEnd(UtListNode_t *TagHead, UtListNode_t *ListNode);
127111

128112
#endif

ut_assert/inc/uttest.h

+23
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,29 @@
5050
*/
5151
void UtTest_Add(void (*Test)(void), void (*Setup)(void), void (*Teardown)(void), const char *TestName);
5252

53+
/**
54+
* \brief Registers a setup function
55+
*
56+
* This group of functions are invoked BEFORE normal test routines added with UtTest_Add.
57+
* Within the group, functions are executed in the order registered.
58+
*
59+
* \param Setup Setup function, called before the test function
60+
* \param TestName Name of function for logging purposes
61+
*/
62+
void UtTest_AddSetup(void (*Setup)(void), const char *SequenceName);
63+
64+
/**
65+
* \brief Registers a teardown function
66+
*
67+
* This group of functions is invoked AFTER normal test routines added with UtTest_Add.
68+
* Within the group, functions are executed in the order registered.
69+
*
70+
* \param Teardown Teardown function, called before the test function
71+
* \param TestName Name of function for logging purposes
72+
*/
73+
void UtTest_AddTeardown(void (*Teardown)(void), const char *SequenceName);
74+
75+
5376
/**
5477
* \brief Early initialization function
5578
*

ut_assert/src/utbsp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void OS_Application_Run(void)
240240
*/
241241
void OS_Application_Startup(void)
242242
{
243-
243+
UtTest_EarlyInit();
244244
UT_BSP_Setup();
245245

246246
/*

ut_assert/src/utglobal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef struct
5555

5656
typedef struct
5757
{
58-
UtListHead_t DataBase;
58+
UtListHead_t *DataBasePtr;
5959
uint32 ExecutedCount;
6060
} UtAssert_Global_t;
6161

ut_assert/src/utlist.c

+114-87
Original file line numberDiff line numberDiff line change
@@ -37,138 +37,165 @@
3737
* Function Definitions
3838
*/
3939

40-
UtListHead_t *UtList_Create(void)
40+
UtListHead_t *UtList_Create(uint32 NumTags)
4141
{
42-
UtListHead_t *NewList;
42+
struct ListAllocator
43+
{
44+
UtListHead_t Head;
45+
UtListNode_t Tags[];
46+
};
47+
struct ListAllocator *NewList;
48+
UtListNode_t *TagHead;
49+
size_t ActualSize;
50+
uint32 i;
51+
52+
ActualSize = sizeof(struct ListAllocator) + (sizeof(UtListNode_t) * NumTags);
53+
NewList = (struct ListAllocator *)malloc(ActualSize);
54+
55+
memset(NewList, 0, ActualSize);
56+
57+
NewList->Head.Tags = NewList->Tags;
58+
NewList->Head.NumberOfTags = NumTags;
59+
60+
for (i=0; i < NumTags; ++i)
61+
{
62+
TagHead = &NewList->Head.Tags[i];
63+
TagHead->Tag = i;
64+
TagHead->Next = TagHead;
65+
TagHead->Prev = TagHead;
66+
}
4367

44-
NewList = malloc(sizeof(UtListHead_t));
45-
NewList->First = NULL;
46-
NewList->Last = NULL;
47-
NewList->NumberOfEntries = 0;
48-
return (NewList);
68+
return (&NewList->Head);
4969
}
5070

5171
void UtList_Destroy(UtListHead_t *ListHead)
5272
{
53-
UtList_Reset(ListHead);
73+
uint32 i;
74+
75+
for (i=0; i < ListHead->NumberOfTags; ++i)
76+
{
77+
UtList_Reset(&ListHead->Tags[i]);
78+
}
5479
free(ListHead);
5580
}
5681

57-
void UtList_Reset(UtListHead_t *ListHead)
82+
void UtList_Reset(UtListNode_t *TagHead)
5883
{
59-
while (!UtList_IsEmpty(ListHead)) {
60-
UtList_DeleteFirst(ListHead);
84+
while (!UtList_IsEmpty(TagHead))
85+
{
86+
UtList_DeleteNode(TagHead->Next);
6187
}
6288
}
6389

64-
void UtList_Add(UtListHead_t *ListHead, void *Data, uint32 DataSize, uint32 Tag)
90+
void UtList_Merge(UtListNode_t *TagHead1, UtListNode_t *TagHead2)
6591
{
66-
UtListNode_t *NewNode = NULL;
67-
68-
NewNode = malloc(sizeof(UtListNode_t));
69-
if (ListHead->NumberOfEntries == 0) {
70-
71-
ListHead->First = NewNode;
72-
ListHead->Last = NewNode;
73-
ListHead->NumberOfEntries++;
92+
UtListNode_t *Tail1 = TagHead1->Prev;
93+
UtListNode_t *Tail2 = TagHead2->Prev;
7494

75-
NewNode->Next = NULL;
76-
NewNode->Prev = NULL;
77-
NewNode->Tag = Tag;
78-
NewNode->DataSize = DataSize;
79-
NewNode->Data = malloc(DataSize);
80-
memcpy(NewNode->Data, Data, DataSize);
81-
}
82-
else {
95+
Tail1->Next = TagHead2;
96+
Tail2->Next = TagHead1;
97+
TagHead1->Prev = Tail2;
98+
TagHead2->Prev = Tail1;
99+
}
83100

84-
NewNode->Next = NULL;
85-
NewNode->Prev = ListHead->Last;
86-
NewNode->Tag = Tag;
87-
NewNode->DataSize = DataSize;
88-
NewNode->Data = malloc(DataSize);
89-
memcpy(NewNode->Data, Data, DataSize);
101+
void UtList_Insert_After(UtListNode_t *ExistingNode, UtListNode_t *NewNode)
102+
{
103+
NewNode->Next = ExistingNode->Next;
104+
NewNode->Prev = ExistingNode;
105+
NewNode->Prev->Next = NewNode;
106+
NewNode->Next->Prev = NewNode;
107+
}
90108

91-
ListHead->Last->Next = NewNode;
92-
ListHead->Last = NewNode;
93-
ListHead->NumberOfEntries++;
94-
}
109+
void UtList_Insert_Before(UtListNode_t *ExistingNode, UtListNode_t *NewNode)
110+
{
111+
NewNode->Next = ExistingNode;
112+
NewNode->Prev = ExistingNode->Prev;
113+
NewNode->Prev->Next = NewNode;
114+
NewNode->Next->Prev = NewNode;
95115
}
96116

97-
void UtList_DeleteFirst(UtListHead_t *ListHead)
117+
void UtList_Extract(UtListNode_t *ExistingNode)
98118
{
99-
UtList_DeleteNode(ListHead, ListHead->First);
119+
ExistingNode->Next->Prev = ExistingNode->Prev;
120+
ExistingNode->Prev->Next = ExistingNode->Next;
121+
ExistingNode->Next = ExistingNode;
122+
ExistingNode->Prev = ExistingNode;
100123
}
101124

102-
void UtList_DeleteLast(UtListHead_t *ListHead)
125+
UtListNode_t *UtList_NewNode(void *Data, uint32 DataSize)
103126
{
104-
UtList_DeleteNode(ListHead, ListHead->Last);
127+
union NodeAllocator
128+
{
129+
UtListNode_t Node;
130+
double AlignDbl;
131+
void* AlignPtr;
132+
long AlignLong;
133+
} *AllocNode;
134+
135+
AllocNode = malloc(sizeof(union NodeAllocator) + DataSize);
136+
memset(AllocNode, 0, sizeof(union NodeAllocator));
137+
AllocNode->Node.Data = &AllocNode[1];
138+
AllocNode->Node.DataSize = DataSize;
139+
memcpy(AllocNode->Node.Data, Data, DataSize);
140+
141+
AllocNode->Node.Next = &AllocNode->Node;
142+
AllocNode->Node.Prev = &AllocNode->Node;
143+
144+
return &AllocNode->Node;
105145
}
106146

107-
void UtList_DeleteNode(UtListHead_t *ListHead, UtListNode_t *DeleteNode)
147+
148+
void UtList_Add(UtListHead_t *ListHead, void *Data, uint32 DataSize, uint32 Tag)
108149
{
109-
110-
if (!UtList_IsEmpty(ListHead)) {
111-
112-
if (ListHead->NumberOfEntries == 1) {
113-
ListHead->First = NULL;
114-
ListHead->Last = NULL;
115-
ListHead->NumberOfEntries = 0;
116-
}
117-
else if (DeleteNode == ListHead->First) {
118-
ListHead->First = DeleteNode->Next;
119-
ListHead->First->Prev = NULL;
120-
ListHead->NumberOfEntries--;
121-
}
122-
else if (DeleteNode == ListHead->Last) {
123-
ListHead->Last = DeleteNode->Prev;
124-
ListHead->Last->Next = NULL;
125-
ListHead->NumberOfEntries--;
126-
}
127-
else {
128-
DeleteNode->Prev->Next = DeleteNode->Next;
129-
DeleteNode->Next->Prev = DeleteNode->Prev;
130-
ListHead->NumberOfEntries--;
131-
}
132-
133-
free(DeleteNode->Data);
134-
free(DeleteNode);
150+
UtListNode_t *TagHead;
151+
UtListNode_t *NewNode;
152+
153+
TagHead = UtList_GetHead(ListHead, Tag);
154+
if (TagHead != NULL)
155+
{
156+
NewNode = UtList_NewNode(Data, DataSize);
157+
NewNode->Tag = Tag;
158+
UtList_Insert_Before(TagHead, NewNode);
135159
}
136160
}
137161

138-
void UtList_RemoveFirst(UtListHead_t *ListHead, void *Data)
162+
void UtList_DeleteNode(UtListNode_t *DeleteNode)
139163
{
140-
UtList_RemoveNode(ListHead, Data, ListHead->First);
164+
UtList_Extract(DeleteNode);
165+
166+
/* non-data/header nodes shouldn't be free()'ed */
167+
if (DeleteNode->Data != NULL)
168+
{
169+
free(DeleteNode);
170+
}
141171
}
142172

143-
void UtList_RemoveLast(UtListHead_t *ListHead, void *Data)
173+
bool UtList_IsEmpty(UtListNode_t *TagHead)
144174
{
145-
UtList_RemoveNode(ListHead, Data, ListHead->Last);
175+
return(TagHead->Next == TagHead);
146176
}
147177

148-
void UtList_RemoveNode(UtListHead_t *ListHead, void *Data, UtListNode_t *CurrentNode)
178+
UtListNode_t *UtList_GetHead(UtListHead_t *ListHead, uint32 Tag)
149179
{
150-
if (!UtList_IsEmpty(ListHead)) {
151-
memcpy(Data, CurrentNode->Data, CurrentNode->DataSize);
152-
UtList_DeleteNode(ListHead, CurrentNode);
180+
if (Tag >= ListHead->NumberOfTags)
181+
{
182+
return NULL;
153183
}
184+
return &ListHead->Tags[Tag];
154185
}
155186

156-
UtListNode_t *UtList_First(UtListHead_t *ListHead)
187+
UtListNode_t *UtList_GetNext(UtListNode_t *ListNode)
157188
{
158-
return(ListHead->First);
189+
return ListNode->Next;
159190
}
160191

161-
UtListNode_t *UtList_Last(UtListHead_t *ListHead)
192+
void *UtList_GetObject(UtListNode_t *ListNode)
162193
{
163-
return(ListHead->Last);
194+
return ListNode->Data;
164195
}
165196

166-
bool UtList_IsEmpty(UtListHead_t *ListHead)
197+
bool UtList_IsEnd(UtListNode_t *TagHead, UtListNode_t *ListNode)
167198
{
168-
return(ListHead->NumberOfEntries == 0);
199+
return(TagHead == ListNode);
169200
}
170201

171-
uint32 UtList_Depth(UtListHead_t *ListHead)
172-
{
173-
return(ListHead->NumberOfEntries);
174-
}

0 commit comments

Comments
 (0)