Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_btree_anotherValidCursor.c173 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4 5/* Wrapper provided by the module for testing static function */6extern int test_anotherValidCursor(BtCursor *pCur);7 8void setUp(void) {9  /* No global setup needed */10}11void tearDown(void) {12  /* No global teardown needed */13}14 15/* Helper to zero-init a BtCursor and attach it to a BtShared and MemPage */16static void initCursor(BtCursor *pCur, BtShared *pBt, MemPage *pPage){17  memset(pCur, 0, sizeof(*pCur));18  pCur->pBt = pBt;19  pCur->pPage = pPage;20  pCur->eState = CURSOR_VALID; /* default valid; tests may override */21}22 23/* Test: With no cursors on the BtShared list, function returns SQLITE_OK */24void test_anotherValidCursor_no_list_returns_ok(void){25  BtShared bt;26  MemPage page;27  BtCursor cur;28 29  memset(&bt, 0, sizeof(bt));30  memset(&page, 0, sizeof(page));31  page.pgno = 5;32  page.pBt = &bt;33 34  initCursor(&cur, &bt, &page);35 36  /* No list head set */37  bt.pCursor = 0;38 39  int rc = test_anotherValidCursor(&cur);40  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);41}42 43/* Test: Only pCur is on the list; should return SQLITE_OK */44void test_anotherValidCursor_only_self_on_list_returns_ok(void){45  BtShared bt;46  MemPage page;47  BtCursor cur;48 49  memset(&bt, 0, sizeof(bt));50  memset(&page, 0, sizeof(page));51  page.pgno = 10;52  page.pBt = &bt;53 54  initCursor(&cur, &bt, &page);55  cur.pNext = NULL;56 57  bt.pCursor = &cur;58 59  int rc = test_anotherValidCursor(&cur);60  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);61}62 63/* Test: Another valid cursor on the same page; should return a corruption code (not SQLITE_OK) */64void test_anotherValidCursor_other_valid_same_page_returns_corrupt(void){65  BtShared bt;66  MemPage page;67  BtCursor cur, other;68 69  memset(&bt, 0, sizeof(bt));70  memset(&page, 0, sizeof(page));71  page.pgno = 42;72  page.pBt = &bt;73 74  initCursor(&cur, &bt, &page);75  initCursor(&other, &bt, &page);76 77  /* Build list: other -> cur */78  other.pNext = &cur;79  cur.pNext = NULL;80  bt.pCursor = &other;81 82  int rc = test_anotherValidCursor(&cur);83  TEST_ASSERT_NOT_EQUAL_INT(SQLITE_OK, rc);84}85 86/* Test: Another cursor present but not CURSOR_VALID; should return SQLITE_OK */87void test_anotherValidCursor_other_not_valid_returns_ok(void){88  BtShared bt;89  MemPage page;90  BtCursor cur, other;91 92  memset(&bt, 0, sizeof(bt));93  memset(&page, 0, sizeof(page));94  page.pgno = 77;95  page.pBt = &bt;96 97  initCursor(&cur, &bt, &page);98  initCursor(&other, &bt, &page);99  other.eState = CURSOR_INVALID; /* Not valid */100 101  /* Build list: other -> cur */102  other.pNext = &cur;103  cur.pNext = NULL;104  bt.pCursor = &other;105 106  int rc = test_anotherValidCursor(&cur);107  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);108}109 110/* Test: Another valid cursor but on a different page; should return SQLITE_OK */111void test_anotherValidCursor_other_valid_different_page_returns_ok(void){112  BtShared bt;113  MemPage page1, page2;114  BtCursor cur, other;115 116  memset(&bt, 0, sizeof(bt));117  memset(&page1, 0, sizeof(page1));118  memset(&page2, 0, sizeof(page2));119  page1.pgno = 100;120  page2.pgno = 101;121  page1.pBt = &bt;122  page2.pBt = &bt;123 124  initCursor(&cur, &bt, &page1);125  initCursor(&other, &bt, &page2);126 127  /* Build list: other -> cur */128  other.pNext = &cur;129  cur.pNext = NULL;130  bt.pCursor = &other;131 132  int rc = test_anotherValidCursor(&cur);133  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);134}135 136/* Test: Multiple others, one conflicting later in list: should still report corrupt */137void test_anotherValidCursor_multiple_others_one_conflict_returns_corrupt(void){138  BtShared bt;139  MemPage pageA, pageB;140  BtCursor cur, other1, other2;141 142  memset(&bt, 0, sizeof(bt));143  memset(&pageA, 0, sizeof(pageA));144  memset(&pageB, 0, sizeof(pageB));145  pageA.pgno = 200;146  pageB.pgno = 201;147  pageA.pBt = &bt;148  pageB.pBt = &bt;149 150  initCursor(&cur, &bt, &pageA);151  initCursor(&other1, &bt, &pageB); /* different page, valid */152  initCursor(&other2, &bt, &pageA); /* same page, valid -> conflict */153 154  /* Build list: other1 -> other2 -> cur */155  other1.pNext = &other2;156  other2.pNext = &cur;157  cur.pNext = NULL;158  bt.pCursor = &other1;159 160  int rc = test_anotherValidCursor(&cur);161  TEST_ASSERT_NOT_EQUAL_INT(SQLITE_OK, rc);162}163 164int main(void){165  UNITY_BEGIN();166  RUN_TEST(test_anotherValidCursor_no_list_returns_ok);167  RUN_TEST(test_anotherValidCursor_only_self_on_list_returns_ok);168  RUN_TEST(test_anotherValidCursor_other_valid_same_page_returns_corrupt);169  RUN_TEST(test_anotherValidCursor_other_not_valid_returns_ok);170  RUN_TEST(test_anotherValidCursor_other_valid_different_page_returns_ok);171  RUN_TEST(test_anotherValidCursor_multiple_others_one_conflict_returns_corrupt);172  return UNITY_END();173}