PACKET_SHADOW_ENTRY *packetShadowEntryFind(PACKET_SHADOW_ENTRY_QUEUE *head, SDDM_LPP_PACKET_KEY* pPacketKey) { PACKET_SHADOW_ENTRY *cur = NULL; PACKET_SHADOW_ENTRY *next = NULL; if((NULL == head) || (NULL == pPacketKey)) { return NULL; } STAILQ_FOREACH_SAFE(cur, head, link_next, next) { if(cur->packetKey.pkt_seq == pPacketKey->pkt_seq) { return cur; } } return NULL; } 用c语言写一个ft测试用例,覆盖所有分支
时间: 2024-03-15 11:45:47 浏览: 61
af_packet.rar_AF_PACKET_AFpacket_RAW
以下是一个可能的测试用例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "ft.h"
// Mock data types and functions
typedef struct {
int pkt_seq;
} SDDM_LPP_PACKET_KEY;
typedef struct {
SDDM_LPP_PACKET_KEY packetKey;
int payload;
STAILQ_ENTRY(PACKET_SHADOW_ENTRY) link_next;
} PACKET_SHADOW_ENTRY;
typedef STAILQ_HEAD(PACKET_SHADOW_ENTRY_QUEUE, PACKET_SHADOW_ENTRY) PACKET_SHADOW_ENTRY_QUEUE;
PACKET_SHADOW_ENTRY_QUEUE* mockPacketShadowEntryQueueCreate() {
PACKET_SHADOW_ENTRY_QUEUE* queue = malloc(sizeof(PACKET_SHADOW_ENTRY_QUEUE));
STAILQ_INIT(queue);
return queue;
}
PACKET_SHADOW_ENTRY* mockPacketShadowEntryCreate(int pkt_seq, int payload) {
PACKET_SHADOW_ENTRY* entry = malloc(sizeof(PACKET_SHADOW_ENTRY));
entry->packetKey.pkt_seq = pkt_seq;
entry->payload = payload;
return entry;
}
void mockPacketShadowEntryQueueDestroy(PACKET_SHADOW_ENTRY_QUEUE* queue) {
PACKET_SHADOW_ENTRY *cur, *next;
for (cur = STAILQ_FIRST(queue); cur != NULL; cur = next) {
next = STAILQ_NEXT(cur, link_next);
free(cur);
}
free(queue);
}
// Test cases
void test_packetShadowEntryFind_given_null_queue_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = NULL;
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_null_queue_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_null_packetKey_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY* packetKey = NULL;
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_null_packetKey_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_empty_queue_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_empty_queue_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_one_element_matching_packetKey_should_return_element() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element = mockPacketShadowEntryCreate(1, 10);
STAILQ_INSERT_TAIL(head, element, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != element) {
printf("test_packetShadowEntryFind_given_queue_with_one_element_matching_packetKey_should_return_element: Fail (result is not expected element)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_one_element_not_matching_packetKey_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element = mockPacketShadowEntryCreate(2, 10);
STAILQ_INSERT_TAIL(head, element, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_queue_with_one_element_not_matching_packetKey_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_multiple_elements_matching_packetKey_should_return_first_matching_element() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element1 = mockPacketShadowEntryCreate(1, 10);
PACKET_SHADOW_ENTRY* element2 = mockPacketShadowEntryCreate(1, 20);
PACKET_SHADOW_ENTRY* element3 = mockPacketShadowEntryCreate(2, 30);
STAILQ_INSERT_TAIL(head, element1, link_next);
STAILQ_INSERT_TAIL(head, element2, link_next);
STAILQ_INSERT_TAIL(head, element3, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != element1) {
printf("test_packetShadowEntryFind_given_queue_with_multiple_elements_matching_packetKey_should_return_first_matching_element: Fail (result is not expected element)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_multiple_elements_not_matching_packetKey_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element1 = mockPacketShadowEntryCreate(2, 10);
PACKET_SHADOW_ENTRY* element2 = mockPacketShadowEntryCreate(3, 20);
PACKET_SHADOW_ENTRY* element3 = mockPacketShadowEntryCreate(4, 30);
STAILQ_INSERT_TAIL(head, element1, link_next);
STAILQ_INSERT_TAIL(head, element2, link_next);
STAILQ_INSERT_TAIL(head, element3, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_queue_with_multiple_elements_not_matching_packetKey_should_return_null: Fail (result is not NULL)\n");
}
}
int main() {
test_packetShadowEntryFind_given_null_queue_should_return_null();
test_packetShadowEntryFind_given_null_packetKey_should_return_null();
test_packetShadowEntryFind_given_empty_queue_should_return_null();
test_packetShadowEntryFind_given_queue_with_one_element_matching_packetKey_should_return_element();
test_packetShadowEntryFind_given_queue_with_one_element_not_matching_packetKey_should_return_null();
test_packetShadowEntryFind_given_queue_with_multiple_elements_matching_packetKey_should_return_first_matching_element();
test_packetShadowEntryFind_given_queue_with_multiple_elements_not_matching_packetKey_should_return_null();
return 0;
}
```
阅读全文