Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_expr_exprRefToSrcList.c176 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* The tested function wrapper (provided in the module) */7extern int test_exprRefToSrcList(Walker *pWalker, Expr *pExpr);8 9/* Helpers */10static SrcList* makeSrcListWithCursors(sqlite3 *db, const int *cursors, int n){11  SrcList *p = 0;12  for(int i=0; i<n; i++){13    Token t;14    t.z = "t";15    t.n = 1;16    p = sqlite3SrcListAppend(db, p, &t, 0);17    TEST_ASSERT_NOT_NULL_MESSAGE(p, "sqlite3SrcListAppend returned NULL");18  }19  for(int i=0; i<n; i++){20    p->a[i].iCursor = cursors[i];21  }22  return p;23}24 25static void initWalkerRef(Walker *w, struct RefSrcList *r, SrcList *pRef, int *aiEx, int nEx){26  memset(w, 0, sizeof(*w));27  memset(r, 0, sizeof(*r));28  r->pRef = pRef;29  r->nExclude = nEx;30  r->aiExclude = aiEx;31  w->u.pRefSrcList = r;32  w->eCode = 0;33}34 35/* Unity fixtures */36void setUp(void) {37  /* No global setup needed */38}39void tearDown(void) {40  /* No global teardown needed */41}42 43/* Tests */44 45void test_exprRefToSrcList_non_column_op_no_effect(void){46  Walker w;47  struct RefSrcList r;48  Expr e;49  memset(&e, 0, sizeof(e));50  e.op = TK_INTEGER; /* Not TK_COLUMN or TK_AGG_COLUMN */51 52  initWalkerRef(&w, &r, NULL, NULL, 0);53 54  int rc = test_exprRefToSrcList(&w, &e);55  TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);56  TEST_ASSERT_EQUAL_INT(0, w.eCode);57}58 59void test_exprRefToSrcList_column_matches_pRef_sets_bit1(void){60  sqlite3 *db = 0;61  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));62 63  int cursors[] = {111};64  SrcList *pRef = makeSrcListWithCursors(db, cursors, 1);65 66  Walker w;67  struct RefSrcList r;68  initWalkerRef(&w, &r, pRef, NULL, 0);69 70  Expr e;71  memset(&e, 0, sizeof(e));72  e.op = TK_COLUMN;73  e.iTable = 111;74 75  int rc = test_exprRefToSrcList(&w, &e);76  TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);77  TEST_ASSERT_TRUE_MESSAGE((w.eCode & 0x01)!=0, "Bit 0x01 should be set when iTable matches pRef");78  TEST_ASSERT_TRUE_MESSAGE((w.eCode & 0x02)==0, "Bit 0x02 should not be set when iTable matches pRef");79 80  sqlite3SrcListDelete(db, pRef);81  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_close(db));82}83 84void test_exprRefToSrcList_not_in_pRef_but_in_exclude_sets_no_bits(void){85  sqlite3 *db = 0;86  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));87 88  int cursors[] = {10};89  SrcList *pRef = makeSrcListWithCursors(db, cursors, 1);90 91  int excludeList[] = {99, 77};92  Walker w;93  struct RefSrcList r;94  initWalkerRef(&w, &r, pRef, excludeList, 2);95 96  Expr e;97  memset(&e, 0, sizeof(e));98  e.op = TK_COLUMN;99  e.iTable = 99; /* Not in pRef, but in aiExclude */100 101  int rc = test_exprRefToSrcList(&w, &e);102  TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);103  TEST_ASSERT_EQUAL_INT_MESSAGE(0, w.eCode, "No bits should be set when iTable is excluded");104 105  sqlite3SrcListDelete(db, pRef);106  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_close(db));107}108 109void test_exprRefToSrcList_agg_not_in_pRef_nor_exclude_sets_bit2(void){110  sqlite3 *db = 0;111  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));112 113  int cursors[] = {5};114  SrcList *pRef = makeSrcListWithCursors(db, cursors, 1);115 116  int excludeList[] = {9};117  Walker w;118  struct RefSrcList r;119  initWalkerRef(&w, &r, pRef, excludeList, 1);120 121  Expr e;122  memset(&e, 0, sizeof(e));123  e.op = TK_AGG_COLUMN;124  e.iTable = 77; /* Not in pRef and not in exclude list */125 126  int rc = test_exprRefToSrcList(&w, &e);127  TEST_ASSERT_EQUAL_INT(WRC_Continue, rc);128  TEST_ASSERT_TRUE_MESSAGE((w.eCode & 0x02)!=0, "Bit 0x02 should be set when not in pRef and not excluded");129  TEST_ASSERT_TRUE_MESSAGE((w.eCode & 0x01)==0, "Bit 0x01 should not be set when not in pRef");130 131  sqlite3SrcListDelete(db, pRef);132  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_close(db));133}134 135void test_exprRefToSrcList_accumulates_bits_across_calls(void){136  sqlite3 *db = 0;137  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_open(":memory:", &db));138 139  int cursors[] = {1};140  SrcList *pRef = makeSrcListWithCursors(db, cursors, 1);141 142  Walker w;143  struct RefSrcList r;144  initWalkerRef(&w, &r, pRef, NULL, 0);145 146  Expr e;147  memset(&e, 0, sizeof(e));148 149  /* First: set bit 0x02 (not in pRef, not excluded) */150  e.op = TK_COLUMN;151  e.iTable = 2; /* Not in pRef (1) and no exclude list */152  int rc1 = test_exprRefToSrcList(&w, &e);153  TEST_ASSERT_EQUAL_INT(WRC_Continue, rc1);154  TEST_ASSERT_EQUAL_INT(0x02, w.eCode);155 156  /* Second: set bit 0x01 (match pRef) */157  e.op = TK_COLUMN;158  e.iTable = 1; /* Matches pRef cursor */159  int rc2 = test_exprRefToSrcList(&w, &e);160  TEST_ASSERT_EQUAL_INT(WRC_Continue, rc2);161  TEST_ASSERT_EQUAL_INT(0x03, w.eCode); /* Both bits should now be set */162 163  sqlite3SrcListDelete(db, pRef);164  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_close(db));165}166 167/* Unity main */168int main(void){169  UNITY_BEGIN();170  RUN_TEST(test_exprRefToSrcList_non_column_op_no_effect);171  RUN_TEST(test_exprRefToSrcList_column_matches_pRef_sets_bit1);172  RUN_TEST(test_exprRefToSrcList_not_in_pRef_but_in_exclude_sets_no_bits);173  RUN_TEST(test_exprRefToSrcList_agg_not_in_pRef_nor_exclude_sets_bit2);174  RUN_TEST(test_exprRefToSrcList_accumulates_bits_across_calls);175  return UNITY_END();176}