4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2008 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 * are met:
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 *
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. */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/text.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdio.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdlib.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <string.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <atf-c.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/sanity.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/test_helpers.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Auxiliary functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#define REQUIRE_ERROR(exp) \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt do { \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err = exp; \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(atf_is_error(err)); \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_free(err); \
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } while (0)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntsize_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntarray_size(const char *words[])
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt size_t count;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char **word;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt count = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for (word = words; *word != NULL; word++)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt count++;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return count;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntcheck_split(const char *str, const char *delim, const char *words[])
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_t list;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char **word;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt size_t i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt printf("Splitting '%s' with delimiter '%s'\n", str, delim);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt CE(atf_text_split(str, delim, &list));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt printf("Expecting %zd words\n", array_size(words));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_CHECK_EQ(atf_list_size(&list), array_size(words));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
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]);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_list_fini(&list);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntword_acum(const char *word, void *data)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char *acum = data;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strcat(acum, word);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_no_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntword_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt size_t *counter = data;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt (*counter)++;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_no_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct fail_at {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int failpos;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt int curpos;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt};
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntword_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct fail_at *fa = data;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (fa->failpos == fa->curpos)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_no_memory_error(); /* Just a random error. */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fa->curpos++;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_no_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(for_each_word);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(for_each_word, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_text_for_each_word"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(for_each_word, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt size_t cnt;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char acum[1024];
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cnt = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strcpy(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 ATF_REQUIRE(cnt == 3);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(strcmp(acum, "123") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cnt = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strcpy(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 ATF_REQUIRE(cnt == 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(strcmp(acum, "1 2 3") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cnt = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strcpy(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_REQUIRE(cnt == 5);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(strcmp(acum, "12345") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt cnt = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt strcpy(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_REQUIRE(cnt == 5);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(strcmp(acum, "12345") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt struct fail_at fa;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fa.failpos = 3;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt fa.curpos = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err = atf_text_for_each_word("a b c d e", " ",
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt word_fail_at, &fa);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(atf_is_error(err));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(atf_error_is(err, "no_memory"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(fa.curpos == 3);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_free(err);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(format);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(format, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
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}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(format, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char *str;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_text_format(&str, "%s %s %d", "Test", "string", 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!atf_is_error(err));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(str);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntformat_ap(char **dest, const char *fmt, ...)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt va_list ap;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt va_start(ap, fmt);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_text_format_ap(dest, fmt, ap);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt va_end(ap);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!atf_is_error(err));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(format_ap);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(format_ap, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
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}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(format_ap, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char *str;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt format_ap(&str, "%s %s %d", "Test", "string", 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(strcmp(str, "Test string 1") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt free(str);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(split);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(split, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the split function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(split, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split(" ", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split(" ", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "a", "b", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("a b", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "a", "b", "c", "d", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("a b c d", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("foo bar", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", "baz", "foobar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("foo bar baz foobar", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split(" foo bar", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("foo bar", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("foo bar ", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "foo", "bar", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split(" foo bar ", " ", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(split_delims);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(split_delims, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the split function using "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "different delimiters");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(split_delims, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("", "/", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { " ", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split(" ", "/", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { " ", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split(" ", "/", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "a", "b", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("a/b", "/", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char *words[] = { "a", "bcd", "ef", NULL };
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(to_bool);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(to_bool, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_bool function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(to_bool, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt bool b;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("true", &b)); ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("TRUE", &b)); ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("yes", &b)); ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("YES", &b)); ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
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 RE(atf_text_to_bool("no", &b)); ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt RE(atf_text_to_bool("NO", &b)); ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = false;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = true;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = false;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("tru", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = true;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("tru", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = false;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("true2", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = true;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("true2", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = false;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("fals", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = true;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("fals", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = false;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("false2", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt b = true;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_bool("false2", &b));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(b);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC(to_long);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_HEAD(to_long, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_long function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TC_BODY(to_long, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt long l;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
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 l = 1212;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_long("", &l));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(l, 1212);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_long("foo", &l));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(l, 1212);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE_ERROR(atf_text_to_long("1234x", &l));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(l, 1212);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/* ---------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Main.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * --------------------------------------------------------------------- */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TP_ADD_TCS(tp)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, for_each_word);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, format);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, format_ap);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, split);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, split_delims);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, to_bool);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_TP_ADD_TC(tp, to_long);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_no_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}