cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

strnstr.c (2514B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include <CUnit/CUnit.h>
     21#include <guacamole/string.h>
     22
     23#include <stdlib.h>
     24#include <string.h>
     25
     26/**
     27 * Verify guac_strnstr() behaviors:
     28 */
     29void test_string__strnstr() {
     30    char haystack[8] = {'a', 'h', 'i', ' ', 't', 'u', 'n', 'a'};
     31    char* result;
     32
     33    /* needle exists at start of haystack */
     34    result = guac_strnstr(haystack, "ah", sizeof(haystack));
     35    CU_ASSERT_EQUAL(result, haystack);
     36
     37    /* needle exists in the middle of haystack */
     38    result = guac_strnstr(haystack, "hi", sizeof(haystack));
     39    CU_ASSERT_EQUAL(result, haystack + 1);
     40
     41    /* needle exists at end of haystack */
     42    result = guac_strnstr(haystack, "tuna", sizeof(haystack));
     43    CU_ASSERT_EQUAL(result, haystack + 4);
     44
     45    /* needle doesn't exist in haystack, needle[0] isn't in haystack */
     46    result = guac_strnstr(haystack, "mahi", sizeof(haystack));
     47    CU_ASSERT_EQUAL(result, NULL);
     48
     49    /*
     50     * needle doesn't exist in haystack, needle[0] is in haystack,
     51     *   length wouldn't allow needle to exist
     52     */
     53    result = guac_strnstr(haystack, "narwhal", sizeof(haystack));
     54    CU_ASSERT_EQUAL(result, NULL);
     55
     56    /*
     57     * needle doesn't exist in haystack, needle[0] is in haystack,
     58     *   length would allow needle to exist
     59     */
     60    result = guac_strnstr(haystack, "taco", sizeof(haystack));
     61    CU_ASSERT_EQUAL(result, NULL);
     62
     63    /*
     64     * needle doesn't exist in haystack, needle[0] is in haystack
     65     *   multiple times
     66     */
     67    result = guac_strnstr(haystack, "ahha", sizeof(haystack));
     68    CU_ASSERT_EQUAL(result, NULL);
     69
     70    /* empty needle should return haystack according to API docs */
     71    result = guac_strnstr(haystack, "", sizeof(haystack));
     72    CU_ASSERT_EQUAL(result, haystack);
     73}