AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5 6/* External wrapper provided by the harness for the static function */7void test_assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild);8 9static void init_mempage_basic(MemPage *pPg, u8 *aData, u8 *aCellIdx){10 memset(pPg, 0, sizeof(MemPage));11 pPg->aData = aData;12 pPg->aCellIdx = aCellIdx;13 pPg->hdrOffset = 0; /* Put the header at the start of aData */14 pPg->maskPage = 0xFFFF; /* Allow findCell to use offsets verbatim */15}16 17/* Helpers to big-endian write 16-bit index into the cell pointer array */18static void set_cell_pointer(u8 *aCellIdx, int iIdx, int ofst){19 put2byte(&aCellIdx[2*iIdx], (u16)ofst);20}21 22/* Helpers to write the first 4 bytes of a cell (the child pointer)23 and the right-child pointer in the header at aData[8] */24static void set_cell_child(u8 *aData, int cellOfst, Pgno child){25 put4byte(&aData[cellOfst], child);26}27static void set_right_child(u8 *aData, int hdrOffset, Pgno child){28 put4byte(&aData[hdrOffset+8], child);29}30 31void setUp(void) {32 /* Nothing to set up globally */33}34 35void tearDown(void) {36 /* Nothing to tear down globally */37}38 39/* Test: iIdx < nCell, child pointer stored in the cell content */40void test_assertParentIndex_left_child_matches(void) {41 u8 page[512];42 u8 cellIdx[4]; /* Enough for at least one cell index entry */43 MemPage mp;44 memset(page, 0, sizeof(page));45 memset(cellIdx, 0, sizeof(cellIdx));46 init_mempage_basic(&mp, page, cellIdx);47 48 mp.nCell = 1;49 50 /* Place cell 0 at offset 100 in aData and set its first 4 bytes to child */51 const int cellOfst = 100;52 const Pgno child = 12345;53 set_cell_pointer(cellIdx, 0, cellOfst);54 set_cell_child(page, cellOfst, child);55 56 /* Call wrapper - should not assert */57 test_assertParentIndex(&mp, 0, child);58 59 /* If we reach here, the assertion passed */60 TEST_ASSERT_TRUE(1);61}62 63/* Test: iIdx == nCell, right-child pointer in the header (offset hdr+8) */64void test_assertParentIndex_right_child_matches(void) {65 u8 page[512];66 u8 cellIdx[4];67 MemPage mp;68 memset(page, 0, sizeof(page));69 memset(cellIdx, 0, sizeof(cellIdx));70 init_mempage_basic(&mp, page, cellIdx);71 72 mp.nCell = 3; /* Three cells means right-child is used for iIdx==3 */73 const Pgno rightChild = 54321;74 set_right_child(page, mp.hdrOffset, rightChild);75 76 /* Call wrapper - should not assert */77 test_assertParentIndex(&mp, 3, rightChild);78 TEST_ASSERT_TRUE(1);79}80 81/* Test: nCell == 0 (no cells). iIdx==0 => check right-child pointer */82void test_assertParentIndex_right_child_with_zero_cells(void) {83 u8 page[512];84 u8 cellIdx[2];85 MemPage mp;86 memset(page, 0, sizeof(page));87 memset(cellIdx, 0, sizeof(cellIdx));88 init_mempage_basic(&mp, page, cellIdx);89 90 mp.nCell = 0;91 const Pgno rightChild = 24680;92 set_right_child(page, mp.hdrOffset, rightChild);93 94 test_assertParentIndex(&mp, 0, rightChild);95 TEST_ASSERT_TRUE(1);96}97 98/* Test: Multiple cells, use iIdx==1 cell’s child pointer */99void test_assertParentIndex_left_child_second_cell(void) {100 u8 page[512];101 u8 cellIdx[4]; /* Space for two cell-pointer entries */102 MemPage mp;103 memset(page, 0, sizeof(page));104 memset(cellIdx, 0, sizeof(cellIdx));105 init_mempage_basic(&mp, page, cellIdx);106 107 mp.nCell = 2;108 109 /* Put cell 0 at offset 80 with child 1111, cell 1 at offset 200 with child 2222 */110 set_cell_pointer(cellIdx, 0, 80);111 set_cell_child(page, 80, 1111);112 set_cell_pointer(cellIdx, 1, 200);113 set_cell_child(page, 200, 2222);114 115 test_assertParentIndex(&mp, 1, 2222);116 TEST_ASSERT_TRUE(1);117}118 119int main(void) {120 UNITY_BEGIN();121 RUN_TEST(test_assertParentIndex_left_child_matches);122 RUN_TEST(test_assertParentIndex_right_child_matches);123 RUN_TEST(test_assertParentIndex_right_child_with_zero_cells);124 RUN_TEST(test_assertParentIndex_left_child_second_cell);125 return UNITY_END();126}