Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_mem1_sqlite3MemSize.c105 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5 6/* Wrapper provided in mem1.c to access static sqlite3MemSize */7extern int test_sqlite3MemSize(void *pPrior);8 9void setUp(void) {10  /* Ensure we can (re)configure the memory subsystem to mem1 defaults */11  sqlite3_shutdown();12  sqlite3MemSetDefault();13  /* Initialize SQLite (safe even if already initialized after shutdown) */14  TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_initialize());15}16 17void tearDown(void) {18  /* Cleanly shutdown between tests so configuration is fresh each time */19  sqlite3_shutdown();20}21 22/* Helper: allocate n bytes via sqlite3_malloc and validate size >= n */23static void alloc_and_check_size(int n){24  void *p = sqlite3_malloc(n);25  TEST_ASSERT_NOT_NULL_MESSAGE(p, "sqlite3_malloc returned NULL (OOM?)");26  int sz = test_sqlite3MemSize(p);27  TEST_ASSERT_GREATER_OR_EQUAL_INT_MESSAGE(n, sz, "Reported size smaller than requested");28  sqlite3_free(p);29}30 31/* Helper: realloc to new size (aligned to 8), validate size >= new size */32static void realloc_and_check_size(int initial, int newsize){33  /* Ensure sizes are >0 and (for realloc) multiples of 8 to satisfy asserts */34  TEST_ASSERT_TRUE_MESSAGE(initial > 0, "initial size must be > 0");35  TEST_ASSERT_TRUE_MESSAGE(newsize > 0, "new size must be > 0");36  TEST_ASSERT_EQUAL_INT_MESSAGE(0, newsize % 8, "new size must be 8-byte aligned for realloc tests");37 38  void *p = sqlite3_malloc(initial);39  TEST_ASSERT_NOT_NULL_MESSAGE(p, "sqlite3_malloc returned NULL (OOM?)");40 41  /* Check size for initial allocation (should be >= initial) */42  int s0 = test_sqlite3MemSize(p);43  TEST_ASSERT_GREATER_OR_EQUAL_INT_MESSAGE(initial, s0, "Initial reported size smaller than requested");44 45  /* Reallocate to newsize */46  void *q = sqlite3_realloc(p, newsize);47  TEST_ASSERT_NOT_NULL_MESSAGE(q, "sqlite3_realloc returned NULL (OOM?)");48  int s1 = test_sqlite3MemSize(q);49  TEST_ASSERT_GREATER_OR_EQUAL_INT_MESSAGE(newsize, s1, "Post-realloc reported size smaller than requested");50 51  sqlite3_free(q);52}53 54/* 1) Basic small allocations, including non-8-aligned sizes */55void test_sqlite3MemSize_basic_allocations(void) {56  alloc_and_check_size(1);57  alloc_and_check_size(2);58  alloc_and_check_size(7);59  alloc_and_check_size(8);60  alloc_and_check_size(15);61  alloc_and_check_size(16);62  alloc_and_check_size(31);63  alloc_and_check_size(32);64}65 66/* 2) Multiple sizes in a loop to exercise various allocation buckets */67void test_sqlite3MemSize_multiple_sizes_loop(void) {68  for(int n = 1; n <= 128; n += 7){69    alloc_and_check_size(n);70  }71}72 73/* 3) Realloc grow: request larger block and validate reported size */74void test_sqlite3MemSize_realloc_grow(void) {75  realloc_and_check_size(64, 128);   /* 8-byte aligned sizes */76  realloc_and_check_size(32, 256);77  realloc_and_check_size(96, 160);78}79 80/* 4) Realloc shrink: request smaller block and validate reported size */81void test_sqlite3MemSize_realloc_shrink(void) {82  realloc_and_check_size(256, 64);83  realloc_and_check_size(160, 96);84  realloc_and_check_size(128, 32);85}86 87/* 5) Larger allocation to ensure behavior scales */88void test_sqlite3MemSize_large_allocation(void) {89  const int n = 262144; /* 256 KiB */90  void *p = sqlite3_malloc(n);91  TEST_ASSERT_NOT_NULL_MESSAGE(p, "sqlite3_malloc(256KiB) returned NULL (OOM?)");92  int sz = test_sqlite3MemSize(p);93  TEST_ASSERT_GREATER_OR_EQUAL_INT_MESSAGE(n, sz, "Reported size smaller than requested for large allocation");94  sqlite3_free(p);95}96 97int main(void) {98  UNITY_BEGIN();99  RUN_TEST(test_sqlite3MemSize_basic_allocations);100  RUN_TEST(test_sqlite3MemSize_multiple_sizes_loop);101  RUN_TEST(test_sqlite3MemSize_realloc_grow);102  RUN_TEST(test_sqlite3MemSize_realloc_shrink);103  RUN_TEST(test_sqlite3MemSize_large_allocation);104  return UNITY_END();105}