Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_btree_btreeParseCellAdjustSizeForOverflow.c160 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5#include <stdint.h>6 7/* Wrapper for the static function under test (provided in source) */8extern void test_btreeParseCellAdjustSizeForOverflow(9  MemPage *pPage,10  u8 *pCell,11  CellInfo *pInfo12);13 14void setUp(void) {15  /* Nothing to set up */16}17void tearDown(void) {18  /* Nothing to tear down */19}20 21/* Helper to initialize a MemPage/BtShared with only the fields needed */22static void initPage(BtShared *pBt, MemPage *pPage, u16 minLocal, u16 maxLocal, u32 usableSize){23  memset(pBt, 0, sizeof(*pBt));24  memset(pPage, 0, sizeof(*pPage));25  pBt->usableSize = usableSize;26  pPage->pBt = pBt;27  pPage->minLocal = minLocal;28  pPage->maxLocal = maxLocal;29}30 31/* Case 1: surplus == maxLocal, expect nLocal == maxLocal and correct nSize */32void test_btreeParseCellAdjustSizeForOverflow_surplus_equals_max(void){33  BtShared bt;34  MemPage page;35  /* min=10, max=20, usable=100. Choose nPayload so:36     (n - 10) % 96 = 10 -> surplus = 20 (== maxLocal). nPayload=116 */37  initPage(&bt, &page, 10, 20, 100);38 39  uint8_t cell[256];40  memset(cell, 0xAA, sizeof(cell));41  u8 *pCell = cell;42  size_t headerOffset = 12; /* simulate header before payload */43 44  CellInfo info;45  memset(&info, 0, sizeof(info));46  info.nPayload = 116;47  info.pPayload = pCell + headerOffset;48 49  test_btreeParseCellAdjustSizeForOverflow(&page, pCell, &info);50 51  TEST_ASSERT_EQUAL_UINT16(20, info.nLocal);52  /* nSize = headerOffset + nLocal + 4 */53  TEST_ASSERT_EQUAL_UINT16((u16)(headerOffset + 20 + 4), info.nSize);54}55 56/* Case 2: surplus < maxLocal, expect nLocal == surplus */57void test_btreeParseCellAdjustSizeForOverflow_surplus_less_than_max(void){58  BtShared bt;59  MemPage page;60  /* min=10, max=20, usable=100. nPayload=106:61     (106-10)=96, %96=0 => surplus=10 (< max) => nLocal=10 */62  initPage(&bt, &page, 10, 20, 100);63 64  uint8_t cell[256];65  memset(cell, 0, sizeof(cell));66  u8 *pCell = cell;67  size_t headerOffset = 12;68 69  CellInfo info;70  memset(&info, 0, sizeof(info));71  info.nPayload = 106;72  info.pPayload = pCell + headerOffset;73 74  test_btreeParseCellAdjustSizeForOverflow(&page, pCell, &info);75 76  TEST_ASSERT_EQUAL_UINT16(10, info.nLocal);77  TEST_ASSERT_EQUAL_UINT16((u16)(headerOffset + 10 + 4), info.nSize);78}79 80/* Case 3: surplus > maxLocal, expect nLocal == minLocal */81void test_btreeParseCellAdjustSizeForOverflow_surplus_greater_than_max(void){82  BtShared bt;83  MemPage page;84  /* min=10, max=20, usable=100. nPayload=125:85     (125-10)=115, %96=19, surplus=29 (> max) => nLocal=10 */86  initPage(&bt, &page, 10, 20, 100);87 88  uint8_t cell[256];89  memset(cell, 0, sizeof(cell));90  u8 *pCell = cell;91  size_t headerOffset = 8;92 93  CellInfo info;94  memset(&info, 0, sizeof(info));95  info.nPayload = 125;96  info.pPayload = pCell + headerOffset;97 98  test_btreeParseCellAdjustSizeForOverflow(&page, pCell, &info);99 100  TEST_ASSERT_EQUAL_UINT16(10, info.nLocal);101  TEST_ASSERT_EQUAL_UINT16((u16)(headerOffset + 10 + 4), info.nSize);102}103 104/* Case 4: Larger usableSize, general modulo behavior */105void test_btreeParseCellAdjustSizeForOverflow_large_usable_modulo(void){106  BtShared bt;107  MemPage page;108  /* min=30, max=50, usable=500. nPayload=536:109     (536-30)=506, %496 = 10, surplus = 30+10=40 (<=max) => nLocal=40 */110  initPage(&bt, &page, 30, 50, 500);111 112  uint8_t cell[1024];113  memset(cell, 0, sizeof(cell));114  u8 *pCell = cell;115  size_t headerOffset = 0;116 117  CellInfo info;118  memset(&info, 0, sizeof(info));119  info.nPayload = 536;120  info.pPayload = pCell + headerOffset;121 122  test_btreeParseCellAdjustSizeForOverflow(&page, pCell, &info);123 124  TEST_ASSERT_EQUAL_UINT16(40, info.nLocal);125  TEST_ASSERT_EQUAL_UINT16((u16)(headerOffset + 40 + 4), info.nSize);126}127 128/* Case 5: minLocal == maxLocal boundary */129void test_btreeParseCellAdjustSizeForOverflow_min_equals_max(void){130  BtShared bt;131  MemPage page;132  /* min=max=60, usable=1024. nPayload=1080:133     (1080-60)=1020, %1020=0, surplus=60 (==max) => nLocal=60 */134  initPage(&bt, &page, 60, 60, 1024);135 136  uint8_t cell[2048];137  memset(cell, 0, sizeof(cell));138  u8 *pCell = cell;139  size_t headerOffset = 4;140 141  CellInfo info;142  memset(&info, 0, sizeof(info));143  info.nPayload = 1080;144  info.pPayload = pCell + headerOffset;145 146  test_btreeParseCellAdjustSizeForOverflow(&page, pCell, &info);147 148  TEST_ASSERT_EQUAL_UINT16(60, info.nLocal);149  TEST_ASSERT_EQUAL_UINT16((u16)(headerOffset + 60 + 4), info.nSize);150}151 152int main(void){153  UNITY_BEGIN();154  RUN_TEST(test_btreeParseCellAdjustSizeForOverflow_surplus_equals_max);155  RUN_TEST(test_btreeParseCellAdjustSizeForOverflow_surplus_less_than_max);156  RUN_TEST(test_btreeParseCellAdjustSizeForOverflow_surplus_greater_than_max);157  RUN_TEST(test_btreeParseCellAdjustSizeForOverflow_large_usable_modulo);158  RUN_TEST(test_btreeParseCellAdjustSizeForOverflow_min_equals_max);159  return UNITY_END();160}