Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_expr_sqlite3ExprIsTableConstant.c169 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6/* Wrapper provided by the module (for static function) */7extern int test_sqlite3ExprIsTableConstant(Expr *p, int iCur, int bAllowSubq);8 9/* Helpers to build simple Expr trees directly */10 11/* Allocate a zeroed Expr and set its op */12static Expr* newExpr(int op){13  Expr *p = (Expr*)sqlite3MallocZero(sizeof(Expr));14  if( p ){15    p->op = (u8)op;16  }17  return p;18}19 20/* Create an integer literal expression */21static Expr* makeInt(int v){22  Expr *p = newExpr(TK_INTEGER);23  if( p ){24    p->u.iValue = v;25  }26  return p;27}28 29/* Create a column reference expression with given iTable and iColumn */30static Expr* makeColumn(int iTable, int iColumn){31  Expr *p = newExpr(TK_COLUMN);32  if( p ){33    p->iTable = iTable;34    p->iColumn = (ynVar)iColumn;35  }36  return p;37}38 39/* Create a binary operation expression with given operator and children */40static Expr* makeBinary(int op, Expr *pLeft, Expr *pRight){41  Expr *p = newExpr(op);42  if( p ){43    p->pLeft = pLeft;44    p->pRight = pRight;45  }46  return p;47}48 49/* Recursively free an Expr tree we allocated with sqlite3MallocZero */50static void freeExprTree(Expr *p){51  if( !p ) return;52  if( p->pLeft ) freeExprTree(p->pLeft);53  if( p->pRight ) freeExprTree(p->pRight);54  /* We did not allocate or set x.pList/x.pSelect etc., so just free self */55  sqlite3_free(p);56}57 58void setUp(void) {59  /* No global setup needed */60}61 62void tearDown(void) {63  /* No global teardown needed */64}65 66/* Tests */67 68void test_sqlite3ExprIsTableConstant_null_expr(void){69  /* NULL expression should be treated as constant (no table references). */70  int rc = test_sqlite3ExprIsTableConstant(NULL, /*iCur*/1, /*bAllowSubq*/0);71  TEST_ASSERT_NOT_EQUAL(0, rc);72 73  rc = test_sqlite3ExprIsTableConstant(NULL, /*iCur*/1, /*bAllowSubq*/1);74  TEST_ASSERT_NOT_EQUAL(0, rc);75}76 77void test_sqlite3ExprIsTableConstant_integer_literal(void){78  Expr *p = makeInt(123);79  TEST_ASSERT_NOT_NULL(p);80 81  int rc = test_sqlite3ExprIsTableConstant(p, /*iCur*/5, /*bAllowSubq*/0);82  TEST_ASSERT_NOT_EQUAL(0, rc);83 84  rc = test_sqlite3ExprIsTableConstant(p, /*iCur*/5, /*bAllowSubq*/1);85  TEST_ASSERT_NOT_EQUAL(0, rc);86 87  freeExprTree(p);88}89 90void test_sqlite3ExprIsTableConstant_column_same_table(void){91  /* Column from the same table cursor should be constant for any single row */92  int iCur = 3;93  Expr *p = makeColumn(iCur, 0);94  TEST_ASSERT_NOT_NULL(p);95 96  int rc = test_sqlite3ExprIsTableConstant(p, iCur, /*bAllowSubq*/0);97  TEST_ASSERT_NOT_EQUAL(0, rc);98 99  rc = test_sqlite3ExprIsTableConstant(p, iCur, /*bAllowSubq*/1);100  TEST_ASSERT_NOT_EQUAL(0, rc);101 102  freeExprTree(p);103}104 105void test_sqlite3ExprIsTableConstant_column_other_table(void){106  /* Column from a different table cursor should NOT be constant for iCur */107  int iCur = 2;108  Expr *p = makeColumn(/*iTable*/iCur+1, 1);109  TEST_ASSERT_NOT_NULL(p);110 111  int rc = test_sqlite3ExprIsTableConstant(p, iCur, /*bAllowSubq*/0);112  TEST_ASSERT_EQUAL_INT(0, rc);113 114  rc = test_sqlite3ExprIsTableConstant(p, iCur, /*bAllowSubq*/1);115  TEST_ASSERT_EQUAL_INT(0, rc);116 117  freeExprTree(p);118}119 120void test_sqlite3ExprIsTableConstant_binary_plus_same_table_and_literal(void){121  /* (col(iCur) + 5) should be constant per row of iCur */122  int iCur = 7;123  Expr *pCol = makeColumn(iCur, 0);124  Expr *pInt = makeInt(5);125  TEST_ASSERT_NOT_NULL(pCol);126  TEST_ASSERT_NOT_NULL(pInt);127 128  Expr *pAdd = makeBinary(TK_PLUS, pCol, pInt);129  TEST_ASSERT_NOT_NULL(pAdd);130 131  int rc = test_sqlite3ExprIsTableConstant(pAdd, iCur, /*bAllowSubq*/0);132  TEST_ASSERT_NOT_EQUAL(0, rc);133 134  rc = test_sqlite3ExprIsTableConstant(pAdd, iCur, /*bAllowSubq*/1);135  TEST_ASSERT_NOT_EQUAL(0, rc);136 137  freeExprTree(pAdd); /* Frees whole tree */138}139 140void test_sqlite3ExprIsTableConstant_binary_plus_mixed_tables(void){141  /* (col(iCur) + col(other)) should NOT be constant for iCur */142  int iCur = 4;143  Expr *pColSame = makeColumn(iCur, 0);144  Expr *pColOther = makeColumn(iCur+100, 1);145  TEST_ASSERT_NOT_NULL(pColSame);146  TEST_ASSERT_NOT_NULL(pColOther);147 148  Expr *pAdd = makeBinary(TK_PLUS, pColSame, pColOther);149  TEST_ASSERT_NOT_NULL(pAdd);150 151  int rc = test_sqlite3ExprIsTableConstant(pAdd, iCur, /*bAllowSubq*/0);152  TEST_ASSERT_EQUAL_INT(0, rc);153 154  rc = test_sqlite3ExprIsTableConstant(pAdd, iCur, /*bAllowSubq*/1);155  TEST_ASSERT_EQUAL_INT(0, rc);156 157  freeExprTree(pAdd);158}159 160int main(void){161  UNITY_BEGIN();162  RUN_TEST(test_sqlite3ExprIsTableConstant_null_expr);163  RUN_TEST(test_sqlite3ExprIsTableConstant_integer_literal);164  RUN_TEST(test_sqlite3ExprIsTableConstant_column_same_table);165  RUN_TEST(test_sqlite3ExprIsTableConstant_column_other_table);166  RUN_TEST(test_sqlite3ExprIsTableConstant_binary_plus_same_table_and_literal);167  RUN_TEST(test_sqlite3ExprIsTableConstant_binary_plus_mixed_tables);168  return UNITY_END();169}