Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_btree_btreeOverwriteContent.c132 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3 4#include <string.h>5#include <stdlib.h>6 7/* The test wrapper for the static function btreeOverwriteContent() is assumed8** to be provided by the module as:9**   int test_btreeOverwriteContent(MemPage*, u8*, const BtreePayload*, int, int);10*/11int test_btreeOverwriteContent(MemPage *pPage, u8 *pDest, const BtreePayload *pX, int iOffset, int iAmt);12 13static MemPage gPage; /* Minimal MemPage used for no-write scenarios */14 15void setUp(void) {16  memset(&gPage, 0, sizeof(gPage));17}18 19void tearDown(void) {20  /* nothing */21}22 23/* Helper to initialize a BtreePayload quickly */24static void initPayload(BtreePayload *pX, const void *pData, int nData, int nZero){25  memset(pX, 0, sizeof(*pX));26  pX->pData = (void*)pData;27  pX->nData = nData;28  pX->nZero = nZero;29}30 31/* Test 1: Zero-range no-op: nData <= 0 and pDest already zeros */32void test_btreeOverwriteContent_zero_range_noop(void) {33  /* Arrange */34  u8 dest[32];35  memset(dest, 0x00, sizeof(dest));36  const char data[] = "abc"; /* nData=3, but iOffset will be > nData */37  BtreePayload x;38  initPayload(&x, data, 3, 0);39  int iOffset = 10;          /* nData - iOffset = -7 <= 0 */40  int iAmt = 12;             /* arbitrary positive */41 42  /* Act */43  int rc = test_btreeOverwriteContent(&gPage, dest, &x, iOffset, iAmt);44 45  /* Assert */46  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);47  for(int i=0;i<32;i++){48    TEST_ASSERT_EQUAL_UINT8(0x00, dest[i]);49  }50}51 52/* Test 2: Exact-match no-op (data fully within nData, equal content) */53void test_btreeOverwriteContent_exact_match_noop(void) {54  /* Arrange */55  u8 dest[16];56  for(int i=0;i<16;i++) dest[i] = (u8)('A' + (i%3)); /* pattern A,B,C */57  BtreePayload x;58  initPayload(&x, dest, 16, 0); /* data identical to dest */59  int iOffset = 2;60  int iAmt = 8; /* Write range dest[2..9], all match */61 62  /* Make a snapshot for comparison after call */63  u8 before[16];64  memcpy(before, dest, sizeof(dest));65 66  /* Act */67  int rc = test_btreeOverwriteContent(&gPage, dest + iOffset, &x, iOffset, iAmt);68 69  /* Assert */70  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);71  TEST_ASSERT_EQUAL_UINT8_ARRAY(before, dest, 16);72}73 74/* Test 3: Mixed data+zeros where both already match75   - nData < iAmt, tail zeros region already zero, data region equal.76*/77void test_btreeOverwriteContent_mixed_match_noop(void) {78  /* Arrange */79  u8 dest[32];80  memset(dest, 0, sizeof(dest));81 82  /* Prepare data region [0..5] == "HELLO!" */83  static const u8 dataPart[] = {'H','E','L','L','O','!'};84  memcpy(dest, dataPart, sizeof(dataPart));85 86  /* pX: nData=6 (matches first part), followed by zeros (nZero arbitrary) */87  BtreePayload x;88  initPayload(&x, dataPart, (int)sizeof(dataPart), 10);89 90  int iOffset = 0;91  int iAmt = 12; /* We ask to write 12 bytes: 6 data + 6 zeros.92                    dest[6..11] already zeros. */93 94  /* Take a snapshot */95  u8 before[32];96  memcpy(before, dest, sizeof(dest));97 98  /* Act */99  int rc = test_btreeOverwriteContent(&gPage, dest + iOffset, &x, iOffset, iAmt);100 101  /* Assert */102  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);103  TEST_ASSERT_EQUAL_UINT8_ARRAY(before, dest, 32);104}105 106/* Test 4: iAmt==0 (no-op) */107void test_btreeOverwriteContent_amt_zero(void) {108  /* Arrange */109  u8 dest[8];110  for(int i=0;i<8;i++) dest[i] = (u8)(0x80 + i);111  BtreePayload x;112  static const u8 dummy[1] = {0xFF};113  initPayload(&x, dummy, 1, 0);114  u8 before[8];115  memcpy(before, dest, sizeof(dest));116 117  /* Act */118  int rc = test_btreeOverwriteContent(&gPage, dest, &x, 0, 0);119 120  /* Assert */121  TEST_ASSERT_EQUAL_INT(SQLITE_OK, rc);122  TEST_ASSERT_EQUAL_UINT8_ARRAY(before, dest, 8);123}124 125int main(void) {126  UNITY_BEGIN();127  RUN_TEST(test_btreeOverwriteContent_zero_range_noop);128  RUN_TEST(test_btreeOverwriteContent_exact_match_noop);129  RUN_TEST(test_btreeOverwriteContent_mixed_match_noop);130  RUN_TEST(test_btreeOverwriteContent_amt_zero);131  return UNITY_END();132}