StrCmp

Table of Contents

StrCmp

Description

Compare two Str objects

Parameters

NameDirectionDescription
strinFirst string
ostrinOther string

Usage example (Cross-references)

    }
    
    bool result = StrCmp(&output_clean, &expected_clean) == 0;
    
    if (!result) {
    }
    
    bool result = StrCmp(&output_clean, &expected_clean) == 0;
    
    if (!result) {
    }
    
    bool result = StrCmp(&output_clean, &expected_clean) == 0;
    
    if (!result) {
    // Helper function to compare persons
    bool compare_persons(const TestPerson* a, const TestPerson* b) {
    return a->id == b->id && StrCmp(&a->name, &b->name) == 0 && a->age == b->age && a->is_active == b->is_active &&
    a->salary == b->salary;
    }
    // Helper function to compare configs
    bool compare_configs(const TestConfig* a, const TestConfig* b) {
    if (a->debug_mode != b->debug_mode || a->timeout != b->timeout || StrCmp(&a->log_level, &b->log_level) != 0 ||
    VecLen(&a->features) != VecLen(&b->features)) {
    return false;
    
    for (size i = 0; i < VecLen(&a->features); i++) {
    if (StrCmp(&VecAt(&a->features, i), &VecAt(&b->features, i)) != 0) {
    return false;
    }
    // Compare values
    if (original.count == parsed.count && original.temperature == parsed.temperature &&
    original.enabled == parsed.enabled && StrCmp(&original.message, &parsed.message) == 0) {
    printf("[DEBUG] Simple round-trip test passed\n");
    } else {
    
    // Compare values
    if (parsed.empty.length == original.empty.length && StrCmp(&original.simple, &parsed.simple) == 0 &&
    StrCmp(&original.with_spaces, &parsed.with_spaces) == 0 &&
    StrCmp(&original.with_special, &parsed.with_special) == 0) {
    // Compare values
    if (parsed.empty.length == original.empty.length && StrCmp(&original.simple, &parsed.simple) == 0 &&
    StrCmp(&original.with_spaces, &parsed.with_spaces) == 0 &&
    StrCmp(&original.with_special, &parsed.with_special) == 0) {
    printf("[DEBUG] String round-trip test passed\n");
    if (parsed.empty.length == original.empty.length && StrCmp(&original.simple, &parsed.simple) == 0 &&
    StrCmp(&original.with_spaces, &parsed.with_spaces) == 0 &&
    StrCmp(&original.with_special, &parsed.with_special) == 0) {
    printf("[DEBUG] String round-trip test passed\n");
    } else {
    if (VecAt(&original_strings, i).length != VecAt(&parsed_strings, i).length ||
    (VecAt(&original_strings, i).length &&
    StrCmp(&VecAt(&original_strings, i), &VecAt(&parsed_strings, i)) != 0)) {
    strings_match = false;
    break;
    
    Str expected = StrInitFromZstr("Hello");
    success      = success && (StrCmp(&s, &expected) == 0);
    StrDeinit(&expected);
    StrClear(&s);
    
    expected = StrInitFromZstr("Hello, World!");
    success  = success && (StrCmp(&s, &expected) == 0);
    StrDeinit(&expected);
    
    Str expected = StrInitFromZstr("Alice");
    success      = success && (StrCmp(&name, &expected) == 0);
    StrDeinit(&expected);
    StrClear(&name);
    
    expected = StrInitFromZstr("Bob");
    success  = success && (StrCmp(&name, &expected) == 0);
    StrDeinit(&expected);
    
    Str  expected = StrInitFromZstr("Hello");
    bool str_pass = (StrCmp(&str_val, &expected) == 0);
    printf("str_val test: comparing with 'Hello', pass = %s\n", str_pass ? "true" : "false");
    success = success && str_pass;
    
    expected             = StrInitFromZstr("World");
    bool quoted_str_pass = (StrCmp(&str_val, &expected) == 0);
    printf("quoted str_val test: comparing with 'World', pass = %s\n", quoted_str_pass ? "true" : "false");
    success = success && quoted_str_pass;
    // Should read "hello" (stops at first space)
    Str  expected   = StrInitFromZstr("hello");
    bool test1_pass = (StrCmp(&result, &expected) == 0);
    printf("Expected: 'hello', Pass: %s\n\n", test1_pass ? "true" : "false");
    success = success && test1_pass;
    // Should read "HELLO" (stops at first space)
    Str  expected   = StrInitFromZstr("HELLO");
    bool test2_pass = (StrCmp(&result, &expected) == 0);
    printf("Expected: 'HELLO', Pass: %s\n\n", test2_pass ? "true" : "false");
    success = success && test2_pass;
    // Should read "mixed case" (converts the entire quoted string)
    Str  expected   = StrInitFromZstr("mixed case");
    bool test3_pass = (StrCmp(&result, &expected) == 0);
    printf("Expected: 'mixed case', Pass: %s\n\n", test3_pass ? "true" : "false");
    success = success && test3_pass;
    // Should read "ABC123XYZ" (only letters are converted, numbers unchanged)
    Str  expected   = StrInitFromZstr("ABC123XYZ");
    bool test4_pass = (StrCmp(&result, &expected) == 0);
    printf("Expected: 'ABC123XYZ', Pass: %s\n\n", test4_pass ? "true" : "false");
    success = success && test4_pass;
    // Should read "Hello" (stops at first space, no case conversion)
    Str  expected   = StrInitFromZstr("Hello");
    bool test5_pass = (StrCmp(&result, &expected) == 0);
    printf("Expected: 'Hello', Pass: %s\n\n", test5_pass ? "true" : "false");
    success = success && test5_pass;
    // Test string comparison functions
    bool test_str_cmp(void) {
    printf("Testing StrCmp and StrCmpCstr\n");
    
    Str s1 = StrInitFromZstr("Hello");
    
    // Test StrCmp with equal strings
    int  cmp1   = StrCmp(&s1, &s2);
    bool result = (cmp1 == 0);
    
    // Test StrCmp with different strings (H < W in ASCII)
    int cmp2 = StrCmp(&s1, &s3);
    result   = result && (cmp2 < 0);
    
    // Test StrCmp with string prefix - ZstrCompare compares the entire strings
    int cmp3 = StrCmp(&s1, &s4);
    result   = result && (cmp3 < 0); // "Hello" comes before "Hello World" lexicographically

Share :