AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* Wrapper provided in the module for the static function */7int test_exprListIsNoAffinity(Parse *pParse, ExprList *pRow);8 9static sqlite3 *gDb = NULL;10 11/* Unity setup/teardown */12void setUp(void) {13 int rc = sqlite3_open(":memory:", &gDb);14 TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);15 TEST_ASSERT_NOT_NULL(gDb);16}17void tearDown(void) {18 if (gDb) {19 sqlite3_close(gDb);20 gDb = NULL;21 }22}23 24/* Helper: initialize a Parse object with the global db */25static void initParse(Parse *p){26 memset(p, 0, sizeof(*p));27 p->db = gDb;28}29 30/* Helper: create a minimal Expr with a given opcode, affExpr=0 */31static Expr* makeExpr(int op){32 Expr *p = (Expr*)sqlite3DbMallocZero(gDb, sizeof(Expr));33 TEST_ASSERT_NOT_NULL(p);34 p->op = op;35 p->affExpr = 0; /* Ensure assert(pExpr->affExpr==0) would hold if reached */36 return p;37}38 39/* Test: Empty list (nExpr=0) should return true (1) */40void test_exprListIsNoAffinity_empty_list_returns_true(void){41 Parse parse;42 initParse(&parse);43 44 ExprList *pList = (ExprList*)sqlite3DbMallocZero(gDb, sizeof(ExprList));45 TEST_ASSERT_NOT_NULL(pList);46 pList->nExpr = 0; /* Leave pList->a == NULL */47 48 int rc = test_exprListIsNoAffinity(&parse, pList);49 TEST_ASSERT_EQUAL_INT(1, rc);50 51 sqlite3ExprListDelete(gDb, pList);52}53 54/* Test: Single integer constant -> true */55void test_exprListIsNoAffinity_single_integer_constant_returns_true(void){56 Parse parse;57 initParse(&parse);58 59 ExprList *pList = NULL;60 Expr *e1 = makeExpr(TK_INTEGER);61 pList = sqlite3ExprListAppend(&parse, pList, e1);62 TEST_ASSERT_NOT_NULL(pList);63 64 int rc = test_exprListIsNoAffinity(&parse, pList);65 TEST_ASSERT_EQUAL_INT(1, rc);66 67 sqlite3ExprListDelete(gDb, pList);68}69 70/* Test: Multiple constant literals -> true */71void test_exprListIsNoAffinity_multiple_constant_literals_return_true(void){72 Parse parse;73 initParse(&parse);74 75 ExprList *pList = NULL;76 Expr *e1 = makeExpr(TK_INTEGER);77 Expr *e2 = makeExpr(TK_STRING);78 Expr *e3 = makeExpr(TK_NULL);79 80 pList = sqlite3ExprListAppend(&parse, pList, e1);81 TEST_ASSERT_NOT_NULL(pList);82 pList = sqlite3ExprListAppend(&parse, pList, e2);83 TEST_ASSERT_NOT_NULL(pList);84 pList = sqlite3ExprListAppend(&parse, pList, e3);85 TEST_ASSERT_NOT_NULL(pList);86 87 int rc = test_exprListIsNoAffinity(&parse, pList);88 TEST_ASSERT_EQUAL_INT(1, rc);89 90 sqlite3ExprListDelete(gDb, pList);91}92 93/* Test: List including a non-constant (TK_COLUMN) -> false due to non-constant */94void test_exprListIsNoAffinity_list_with_column_expr_returns_false(void){95 Parse parse;96 initParse(&parse);97 98 ExprList *pList = NULL;99 Expr *e1 = makeExpr(TK_INTEGER); /* constant */100 Expr *e2 = makeExpr(TK_COLUMN); /* non-constant */101 102 pList = sqlite3ExprListAppend(&parse, pList, e1);103 TEST_ASSERT_NOT_NULL(pList);104 pList = sqlite3ExprListAppend(&parse, pList, e2);105 TEST_ASSERT_NOT_NULL(pList);106 107 int rc = test_exprListIsNoAffinity(&parse, pList);108 TEST_ASSERT_EQUAL_INT(0, rc);109 110 sqlite3ExprListDelete(gDb, pList);111}112 113/* Test: Single function expression (TK_FUNCTION) -> false due to non-constant */114void test_exprListIsNoAffinity_function_expr_returns_false(void){115 Parse parse;116 initParse(&parse);117 118 ExprList *pList = NULL;119 Expr *e1 = makeExpr(TK_FUNCTION); /* treated as non-constant */120 121 pList = sqlite3ExprListAppend(&parse, pList, e1);122 TEST_ASSERT_NOT_NULL(pList);123 124 int rc = test_exprListIsNoAffinity(&parse, pList);125 TEST_ASSERT_EQUAL_INT(0, rc);126 127 sqlite3ExprListDelete(gDb, pList);128}129 130int main(void){131 UNITY_BEGIN();132 RUN_TEST(test_exprListIsNoAffinity_empty_list_returns_true);133 RUN_TEST(test_exprListIsNoAffinity_single_integer_constant_returns_true);134 RUN_TEST(test_exprListIsNoAffinity_multiple_constant_literals_return_true);135 RUN_TEST(test_exprListIsNoAffinity_list_with_column_expr_returns_false);136 RUN_TEST(test_exprListIsNoAffinity_function_expr_returns_false);137 return UNITY_END();138}