AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* The wrapper for the static function under test */7extern int test_dupedExprStructSize(const Expr *p, int flags);8 9void setUp(void) {10 /* no-op */11}12 13void tearDown(void) {14 /* no-op */15}16 17/* Helper to init an Expr to a known-zero state */18static void initExpr(Expr *p){19 memset(p, 0, sizeof(*p));20 /* p->flags left 0 unless test needs to set flags explicitly */21 /* Other fields default to NULL */22}23 24/* 1) flags == 0 -> always EXPR_FULLSIZE */25void test_dupedExprStructSize_flags_zero_returns_fullsize(void){26 Expr e;27 initExpr(&e);28 29 int rc = test_dupedExprStructSize(&e, 0);30 TEST_ASSERT_EQUAL_INT(EXPR_FULLSIZE, rc);31 32 /* Even with children present, flags==0 must still force FULLSIZE */33 Expr child;34 initExpr(&child);35 e.pLeft = &child;36 e.pRight = &child; /* doesn't affect the branch when flags==0 */37 rc = test_dupedExprStructSize(&e, 0);38 TEST_ASSERT_EQUAL_INT(EXPR_FULLSIZE, rc);39}40 41/* 2) flags == EXPRDUP_REDUCE and a left child -> REDUCEDSIZE | EP_Reduced */42void test_dupedExprStructSize_reduce_with_left_child_returns_reduced(void){43 Expr e, left;44 initExpr(&e);45 initExpr(&left);46 47 e.pLeft = &left; /* Having a left child should select reduced form */48 /* Ensure no disallowed flags are set for reduce mode */49 e.flags = 0;50 51 int rc = test_dupedExprStructSize(&e, EXPRDUP_REDUCE);52 TEST_ASSERT_EQUAL_INT((EXPR_REDUCEDSIZE | EP_Reduced), rc);53}54 55/* 3) flags == EXPRDUP_REDUCE and non-NULL x.pList (no left) -> REDUCEDSIZE | EP_Reduced */56void test_dupedExprStructSize_reduce_with_pList_returns_reduced(void){57 Expr e;58 initExpr(&e);59 60 e.pLeft = NULL;61 /* Use a non-NULL sentinel for pList. No dereference occurs in the target function. */62 e.x.pList = (ExprList*)(uintptr_t)1;63 e.flags = 0;64 65 int rc = test_dupedExprStructSize(&e, EXPRDUP_REDUCE);66 TEST_ASSERT_EQUAL_INT((EXPR_REDUCEDSIZE | EP_Reduced), rc);67 68 /* Cleanup: reset x.pList to avoid accidental reuse */69 e.x.pList = NULL;70}71 72/* 4) flags == EXPRDUP_REDUCE and a leaf (no left, no pList, and pRight==NULL)73 -> TOKENONLYSIZE | EP_TokenOnly */74void test_dupedExprStructSize_reduce_leaf_returns_tokenonly(void){75 Expr e;76 initExpr(&e);77 78 e.pLeft = NULL;79 e.pRight = NULL; /* Required by assert in token-only branch */80 e.x.pList = NULL;81 e.flags = 0;82 83 int rc = test_dupedExprStructSize(&e, EXPRDUP_REDUCE);84 TEST_ASSERT_EQUAL_INT((EXPR_TOKENONLYSIZE | EP_TokenOnly), rc);85}86 87/* 5) EP_FullSize flag forces FULLSIZE even when EXPRDUP_REDUCE is requested */88void test_dupedExprStructSize_ep_fullsize_forces_fullsize(void){89 Expr e, left;90 initExpr(&e);91 initExpr(&left);92 93 e.pLeft = &left; /* Would normally imply reduced when reducing */94 e.flags = EP_FullSize; /* Force full-size path */95 96 int rc = test_dupedExprStructSize(&e, EXPRDUP_REDUCE);97 TEST_ASSERT_EQUAL_INT(EXPR_FULLSIZE, rc);98}99 100int main(void){101 UNITY_BEGIN();102 RUN_TEST(test_dupedExprStructSize_flags_zero_returns_fullsize);103 RUN_TEST(test_dupedExprStructSize_reduce_with_left_child_returns_reduced);104 RUN_TEST(test_dupedExprStructSize_reduce_with_pList_returns_reduced);105 RUN_TEST(test_dupedExprStructSize_reduce_leaf_returns_tokenonly);106 RUN_TEST(test_dupedExprStructSize_ep_fullsize_forces_fullsize);107 return UNITY_END();108}