Team Ai
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
tests_json_jsonLabelCompare.c162 linesDownload Raw Back to tests
1#include "sqliteInt.h"2#include "unity.h"3#include <string.h>4#include <stdlib.h>5#include <stdio.h>6 7/* The wrapper added to the module for testing static function jsonLabelCompare */8extern int test_jsonLabelCompare(const char *zLeft, u32 nLeft, int rawLeft,9                                 const char *zRight, u32 nRight, int rawRight);10 11void setUp(void) {12  /* No setup needed */13}14 15void tearDown(void) {16  /* No teardown needed */17}18 19/* Both raw, simple equal */20void test_jsonLabelCompare_equal_raw_simple(void) {21  const char *a = "foo";22  const char *b = "foo";23  int rc = test_jsonLabelCompare(a, (u32)strlen(a), 1, b, (u32)strlen(b), 1);24  TEST_ASSERT_EQUAL_INT(1, rc);25}26 27/* Both raw, length mismatch */28void test_jsonLabelCompare_not_equal_raw_length_mismatch(void) {29  const char *a = "foo";30  const char *b = "foobar";31  int rc = test_jsonLabelCompare(a, (u32)strlen(a), 1, b, (u32)strlen(b), 1);32  TEST_ASSERT_EQUAL_INT(0, rc);33}34 35/* Both raw, content mismatch */36void test_jsonLabelCompare_not_equal_raw_content_mismatch(void) {37  const char *a = "foo";38  const char *b = "foa";39  int rc = test_jsonLabelCompare(a, (u32)strlen(a), 1, b, (u32)strlen(b), 1);40  TEST_ASSERT_EQUAL_INT(0, rc);41}42 43/* Escaped newline vs raw newline */44void test_jsonLabelCompare_escaped_newline_vs_raw(void) {45  const char *esc = "line\\nbreak";46  const char *raw = "line\nbreak";47  int rc = test_jsonLabelCompare(esc, (u32)strlen(esc), 0, raw, (u32)strlen(raw), 1);48  TEST_ASSERT_EQUAL_INT(1, rc);49}50 51/* Escaped slash vs raw slash */52void test_jsonLabelCompare_escaped_slash_vs_raw(void) {53  const char *esc = "\\/";54  const char *raw = "/";55  int rc = test_jsonLabelCompare(esc, (u32)strlen(esc), 0, raw, (u32)strlen(raw), 1);56  TEST_ASSERT_EQUAL_INT(1, rc);57}58 59/* Escaped backslash vs raw backslash */60void test_jsonLabelCompare_escaped_backslash_vs_raw(void) {61  const char *esc = "\\\\";62  const char *raw = "\\";63  int rc = test_jsonLabelCompare(esc, (u32)strlen(esc), 0, raw, (u32)strlen(raw), 1);64  TEST_ASSERT_EQUAL_INT(1, rc);65}66 67/* Unicode escape vs raw UTF-8: café */68void test_jsonLabelCompare_unicode_escape_vs_utf8_cafe(void) {69  const char *esc = "caf\\u00E9";70  const char *utf8 = "café";71  int rc1 = test_jsonLabelCompare(esc, (u32)strlen(esc), 0, utf8, (u32)strlen(utf8), 1);72  int rc2 = test_jsonLabelCompare(utf8, (u32)strlen(utf8), 1, esc, (u32)strlen(esc), 0);73  TEST_ASSERT_EQUAL_INT(1, rc1);74  TEST_ASSERT_EQUAL_INT(1, rc2);75}76 77/* Surrogate pair escape vs raw UTF-8: 😀 (U+1F600) */78void test_jsonLabelCompare_surrogate_pair_vs_utf8_emoji(void) {79  const char *esc = "\\uD83D\\uDE00";80  const char *utf8 = "😀";81  int rc = test_jsonLabelCompare(esc, (u32)strlen(esc), 0, utf8, (u32)strlen(utf8), 1);82  TEST_ASSERT_EQUAL_INT(1, rc);83}84 85/* Both escaped equal sequence */86void test_jsonLabelCompare_both_escaped_equal(void) {87  const char *a = "\\u0041\\n\\u0042"; /* "A\nB" */88  const char *b = "\\u0041\\n\\u0042";89  int rc = test_jsonLabelCompare(a, (u32)strlen(a), 0, b, (u32)strlen(b), 0);90  TEST_ASSERT_EQUAL_INT(1, rc);91}92 93/* Escaped not equal: \u0041 (A) vs \u0042 (B) */94void test_jsonLabelCompare_escaped_not_equal(void) {95  const char *a = "\\u0041";96  const char *b = "\\u0042";97  int rc = test_jsonLabelCompare(a, (u32)strlen(a), 0, b, (u32)strlen(b), 0);98  TEST_ASSERT_EQUAL_INT(0, rc);99}100 101/* UTF-8 vs Unicode escapes: Hiragana あいう (U+3042, U+3044, U+3046) */102void test_jsonLabelCompare_utf8_vs_unicode_escape_hiragana(void) {103  const char *utf8 = "あいう";104  const char *esc  = "\\u3042\\u3044\\u3046";105  int rc = test_jsonLabelCompare(utf8, (u32)strlen(utf8), 1, esc, (u32)strlen(esc), 0);106  TEST_ASSERT_EQUAL_INT(1, rc);107}108 109/* Empty strings equal (both raw) */110void test_jsonLabelCompare_empty_strings_equal(void) {111  const char *a = "";112  const char *b = "";113  int rc = test_jsonLabelCompare(a, (u32)strlen(a), 1, b, (u32)strlen(b), 1);114  TEST_ASSERT_EQUAL_INT(1, rc);115}116 117/* Raw with embedded NUL: equal */118void test_jsonLabelCompare_embedded_nul_raw_equal(void) {119  const char leftBytes[]  = { 'a', '\0', 'b' };120  const char rightBytes[] = { 'a', '\0', 'b' };121  int rc = test_jsonLabelCompare(leftBytes,  (u32)3, 1,122                                 rightBytes, (u32)3, 1);123  TEST_ASSERT_EQUAL_INT(1, rc);124}125 126/* Raw with embedded NUL: not equal */127void test_jsonLabelCompare_embedded_nul_raw_not_equal(void) {128  const char leftBytes[]  = { 'a', '\0', 'b' };129  const char rightBytes[] = { 'a', '\0', 'c' };130  int rc = test_jsonLabelCompare(leftBytes,  (u32)3, 1,131                                 rightBytes, (u32)3, 1);132  TEST_ASSERT_EQUAL_INT(0, rc);133}134 135/* Backslash literal when marked raw should not be unescaped */136void test_jsonLabelCompare_backslash_literal_when_raw(void) {137  const char *withBackslash = "a\\b"; /* two characters: 'a', '\\', 'b' -> actually 3 chars */138  const char *noBackslash   = "ab";139  int rc = test_jsonLabelCompare(withBackslash, (u32)strlen(withBackslash), 1,140                                 noBackslash,   (u32)strlen(noBackslash),   1);141  TEST_ASSERT_EQUAL_INT(0, rc);142}143 144int main(void) {145  UNITY_BEGIN();146  RUN_TEST(test_jsonLabelCompare_equal_raw_simple);147  RUN_TEST(test_jsonLabelCompare_not_equal_raw_length_mismatch);148  RUN_TEST(test_jsonLabelCompare_not_equal_raw_content_mismatch);149  RUN_TEST(test_jsonLabelCompare_escaped_newline_vs_raw);150  RUN_TEST(test_jsonLabelCompare_escaped_slash_vs_raw);151  RUN_TEST(test_jsonLabelCompare_escaped_backslash_vs_raw);152  RUN_TEST(test_jsonLabelCompare_unicode_escape_vs_utf8_cafe);153  RUN_TEST(test_jsonLabelCompare_surrogate_pair_vs_utf8_emoji);154  RUN_TEST(test_jsonLabelCompare_both_escaped_equal);155  RUN_TEST(test_jsonLabelCompare_escaped_not_equal);156  RUN_TEST(test_jsonLabelCompare_utf8_vs_unicode_escape_hiragana);157  RUN_TEST(test_jsonLabelCompare_empty_strings_equal);158  RUN_TEST(test_jsonLabelCompare_embedded_nul_raw_equal);159  RUN_TEST(test_jsonLabelCompare_embedded_nul_raw_not_equal);160  RUN_TEST(test_jsonLabelCompare_backslash_literal_when_raw);161  return UNITY_END();162}