Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_main_sqlite3_uri_int64.c128 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <stdlib.h>4#include <string.h>5#include <stdint.h>6 7void setUp(void) {8  /* Ensure SQLite is initialized before tests */9  int rc = sqlite3_initialize();10  (void)rc; /* Suppress unused warning; sqlite3_initialize is idempotent */11}12 13void tearDown(void) {14  /* Shutdown SQLite after each test to keep environment clean */15  sqlite3_shutdown();16}17 18/* Helper to call the target function */19static sqlite3_int64 call_uri_int64(const char *uri, const char *param, sqlite3_int64 dflt){20  return sqlite3_uri_int64(uri, param, dflt);21}22 23void test_sqlite3_uri_int64_basic_decimal(void) {24  const char *uri = "file:test.db?x=123";25  sqlite3_int64 res = call_uri_int64(uri, "x", -1);26  TEST_ASSERT(res == 123);27}28 29void test_sqlite3_uri_int64_missing_param_returns_default(void) {30  const char *uri = "file:test.db?y=5";31  sqlite3_int64 dflt = 999;32  sqlite3_int64 res = call_uri_int64(uri, "x", dflt);33  TEST_ASSERT(res == dflt);34}35 36void test_sqlite3_uri_int64_invalid_numeric_returns_default(void) {37  const char *uri = "file:test.db?x=abc123";38  sqlite3_int64 dflt = 42;39  sqlite3_int64 res = call_uri_int64(uri, "x", dflt);40  TEST_ASSERT(res == dflt);41}42 43void test_sqlite3_uri_int64_empty_value_returns_default(void) {44  const char *uri = "file:test.db?x=";45  sqlite3_int64 dflt = 44;46  sqlite3_int64 res = call_uri_int64(uri, "x", dflt);47  TEST_ASSERT(res == dflt);48}49 50void test_sqlite3_uri_int64_hex_values(void) {51  const char *uri = "file:test.db?x=0x10&y=0Xff";52  sqlite3_int64 res_x = call_uri_int64(uri, "x", -1);53  sqlite3_int64 res_y = call_uri_int64(uri, "y", -1);54  TEST_ASSERT(res_x == 16);55  TEST_ASSERT(res_y == 255);56}57 58void test_sqlite3_uri_int64_negative_value(void) {59  const char *uri = "file:test.db?x=-42";60  sqlite3_int64 res = call_uri_int64(uri, "x", 0);61  TEST_ASSERT(res == -42);62}63 64void test_sqlite3_uri_int64_plus_sign_value(void) {65  const char *uri = "file:test.db?x=+77";66  sqlite3_int64 res = call_uri_int64(uri, "x", 0);67  TEST_ASSERT(res == 77);68}69 70void test_sqlite3_uri_int64_zero_value(void) {71  const char *uri = "file:test.db?x=0";72  sqlite3_int64 res = call_uri_int64(uri, "x", 5);73  TEST_ASSERT(res == 0);74}75 76void test_sqlite3_uri_int64_overflow_decimal_returns_default(void) {77  /* One more than INT64_MAX */78  const char *uri = "file:test.db?x=9223372036854775808";79  sqlite3_int64 dflt = 77;80  sqlite3_int64 res = call_uri_int64(uri, "x", dflt);81  TEST_ASSERT(res == dflt);82}83 84void test_sqlite3_uri_int64_overflow_hex_returns_default(void) {85  /* Larger than signed 64-bit max when treated as unsigned */86  const char *uri = "file:test.db?x=0xFFFFFFFFFFFFFFFF";87  sqlite3_int64 dflt = -123;88  sqlite3_int64 res = call_uri_int64(uri, "x", dflt);89  TEST_ASSERT(res == dflt);90}91 92void test_sqlite3_uri_int64_negative_overflow_returns_default(void) {93  /* Less than INT64_MIN */94  const char *uri = "file:test.db?x=-9223372036854775809";95  sqlite3_int64 dflt = 555;96  sqlite3_int64 res = call_uri_int64(uri, "x", dflt);97  TEST_ASSERT(res == dflt);98}99 100void test_sqlite3_uri_int64_ignores_unrelated_params(void) {101  const char *uri = "file:test.db?mode=memory&cache=shared&x=2468&vfs=xyz";102  sqlite3_int64 res = call_uri_int64(uri, "x", 1);103  TEST_ASSERT(res == 2468);104}105 106void test_sqlite3_uri_int64_null_filename_returns_default(void) {107  sqlite3_int64 dflt = 321;108  sqlite3_int64 res = call_uri_int64(NULL, "x", dflt);109  TEST_ASSERT(res == dflt);110}111 112int main(void) {113  UNITY_BEGIN();114  RUN_TEST(test_sqlite3_uri_int64_basic_decimal);115  RUN_TEST(test_sqlite3_uri_int64_missing_param_returns_default);116  RUN_TEST(test_sqlite3_uri_int64_invalid_numeric_returns_default);117  RUN_TEST(test_sqlite3_uri_int64_empty_value_returns_default);118  RUN_TEST(test_sqlite3_uri_int64_hex_values);119  RUN_TEST(test_sqlite3_uri_int64_negative_value);120  RUN_TEST(test_sqlite3_uri_int64_plus_sign_value);121  RUN_TEST(test_sqlite3_uri_int64_zero_value);122  RUN_TEST(test_sqlite3_uri_int64_overflow_decimal_returns_default);123  RUN_TEST(test_sqlite3_uri_int64_overflow_hex_returns_default);124  RUN_TEST(test_sqlite3_uri_int64_negative_overflow_returns_default);125  RUN_TEST(test_sqlite3_uri_int64_ignores_unrelated_params);126  RUN_TEST(test_sqlite3_uri_int64_null_filename_returns_default);127  return UNITY_END();128}