4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2008 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Auxiliary functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntcheck_split(const char *str, const char *delim, const char *words[])
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt printf("Splitting '%s' with delimiter '%s'\n", str, delim);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt printf("Expecting %zd words\n", array_size(words));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK_EQ(atf_list_size(&list), array_size(words));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for (word = words, i = 0; *word != NULL; word++, i++) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt printf("Word at position %zd should be '%s'\n", i, words[i]);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK_STREQ((const char *)atf_list_index_c(&list, i), words[i]);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntword_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntword_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_no_memory_error(); /* Just a random error. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_text_for_each_word"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2 3", " ", word_count, &cnt));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2 3", " ", word_acum, acum));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2 3", ".", word_count, &cnt));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2 3", ".", word_acum, acum));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2 3 4 5", " ", word_count, &cnt));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2 3 4 5", " ", word_acum, acum));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2.3.4 5", " .", word_count, &cnt));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_for_each_word("1 2.3.4 5", " .", word_acum, acum));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err = atf_text_for_each_word("a b c d e", " ",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "strings using a variable parameters list");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_text_format(&str, "%s %s %d", "Test", "string", 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "strings using a va_list argument");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the split function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "a", "b", "c", "d", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", "baz", "foobar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the split function using "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "different delimiters");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_bool function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("false", &b)); ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("FALSE", &b)); ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_long function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_long("0", &l)); ATF_REQUIRE_EQ(l, 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_long("-5", &l)); ATF_REQUIRE_EQ(l, -5);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_long("5", &l)); ATF_REQUIRE_EQ(l, 5);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_long("123456789", &l)); ATF_REQUIRE_EQ(l, 123456789);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */