AryaWu/sqlite
0
1#include "sqliteInt.h"2#include "unity.h"3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6#include <stdint.h>7 8/* Unity fixtures */9void setUp(void) {10 /* Ensure a clean, configurable state with memstatus enabled before11 SQLite is initialized (necessary for sqlite3_memory_used() to work). */12 sqlite3_shutdown();13 /* Enable runtime memory status accounting */14 sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 1);15 /* Initialize now so that the memory mutex and subsystem are ready */16 TEST_ASSERT_EQUAL_INT(SQLITE_OK, sqlite3_initialize());17 /* Disable both soft and hard limits to start from a known baseline */18 (void)sqlite3_hard_heap_limit64(0);19 (void)sqlite3_soft_heap_limit64(0);20}21 22void tearDown(void) {23 /* Restore both limits to disabled state for isolation */24 (void)sqlite3_hard_heap_limit64(0);25 (void)sqlite3_soft_heap_limit64(0);26}27 28/* Helper to fetch current soft limit without changing it */29static sqlite3_int64 get_soft_limit_current(void){30 return sqlite3_soft_heap_limit64((sqlite3_int64)-1);31}32 33/* Tests */34 35void test_sqlite3_soft_heap_limit64_returns_prior_and_sets_limit(void) {36 sqlite3_int64 prior = get_soft_limit_current();37 /* Start should be 0 after setUp, but don't assume; use whatever it is */38 sqlite3_int64 new1 = 1234567;39 sqlite3_int64 ret1 = sqlite3_soft_heap_limit64(new1);40 TEST_ASSERT_EQUAL_INT64(prior, ret1);41 TEST_ASSERT_EQUAL_INT64(new1, get_soft_limit_current());42 43 sqlite3_int64 new2 = 7654321;44 sqlite3_int64 ret2 = sqlite3_soft_heap_limit64(new2);45 TEST_ASSERT_EQUAL_INT64(new1, ret2);46 TEST_ASSERT_EQUAL_INT64(new2, get_soft_limit_current());47}48 49void test_sqlite3_soft_heap_limit64_negative_argument_returns_current(void) {50 sqlite3_int64 setv = 789;51 (void)sqlite3_soft_heap_limit64(setv);52 sqlite3_int64 cur1 = get_soft_limit_current();53 TEST_ASSERT_EQUAL_INT64(setv, cur1);54 55 /* Calling with negative again should not change the value */56 sqlite3_int64 cur2 = get_soft_limit_current();57 TEST_ASSERT_EQUAL_INT64(setv, cur2);58}59 60void test_sqlite3_soft_heap_limit64_honors_hard_limit_and_zero_behavior(void) {61 /* Set an initial soft limit */62 sqlite3_int64 softA = 1000000;63 (void)sqlite3_soft_heap_limit64(softA);64 TEST_ASSERT_EQUAL_INT64(softA, get_soft_limit_current());65 66 /* Set a hard limit below the soft limit; soft should be clamped down */67 sqlite3_int64 hard = 500000;68 (void)sqlite3_hard_heap_limit64(hard);69 TEST_ASSERT_EQUAL_INT64(hard, get_soft_limit_current());70 71 /* Try to raise soft above hard: should clamp to hard */72 sqlite3_int64 retPrior = sqlite3_soft_heap_limit64(softA * 2);73 TEST_ASSERT_EQUAL_INT64(hard, retPrior);74 TEST_ASSERT_EQUAL_INT64(hard, get_soft_limit_current());75 76 /* Try to disable soft (n=0) while hard limit is set: remains at hard */77 sqlite3_int64 retPrior2 = sqlite3_soft_heap_limit64(0);78 TEST_ASSERT_EQUAL_INT64(hard, retPrior2);79 TEST_ASSERT_EQUAL_INT64(hard, get_soft_limit_current());80 81 /* Disable hard limit; then disable soft limit (n=0) */82 (void)sqlite3_hard_heap_limit64(0);83 sqlite3_int64 retPrior3 = sqlite3_soft_heap_limit64(0);84 TEST_ASSERT_EQUAL_INT64(hard, retPrior3);85 TEST_ASSERT_EQUAL_INT64((sqlite3_int64)0, get_soft_limit_current());86}87 88void test_sqlite3_soft_heap_limit64_nearlyfull_flag_toggling(void) {89 /* Ensure we have some measurable memory usage */90 sqlite3_int64 used_before = sqlite3_memory_used();91 size_t alloc_sz = 128 * 1024; /* 128 KiB */92 void *p = sqlite3_malloc((int)alloc_sz);93 TEST_ASSERT_NOT_NULL(p);94 95 sqlite3_int64 used_after = sqlite3_memory_used();96 TEST_ASSERT_TRUE(used_after >= used_before);97 98 /* Set soft limit above current usage: nearlyFull should be 0 */99 sqlite3_int64 high_limit = used_after + 64 * 1024; /* safely above */100 (void)sqlite3_soft_heap_limit64(high_limit);101 TEST_ASSERT_FALSE(sqlite3HeapNearlyFull());102 103 /* Set soft limit to be <= current usage: nearlyFull should be 1 */104 (void)sqlite3_soft_heap_limit64(used_after);105 TEST_ASSERT_TRUE(sqlite3HeapNearlyFull());106 107 /* Disable the soft limit: nearlyFull should be 0 */108 (void)sqlite3_soft_heap_limit64(0);109 TEST_ASSERT_FALSE(sqlite3HeapNearlyFull());110 111 sqlite3_free(p);112}113 114/* Unity main */115int main(void) {116 UNITY_BEGIN();117 RUN_TEST(test_sqlite3_soft_heap_limit64_returns_prior_and_sets_limit);118 RUN_TEST(test_sqlite3_soft_heap_limit64_negative_argument_returns_current);119 RUN_TEST(test_sqlite3_soft_heap_limit64_honors_hard_limit_and_zero_behavior);120 RUN_TEST(test_sqlite3_soft_heap_limit64_nearlyfull_flag_toggling);121 return UNITY_END();122}