Path: blob/main/contrib/atf/atf-c/detail/text_test.c
39507 views
/* Copyright (c) 2008 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include "atf-c/detail/text.h"2627#include <stdio.h>28#include <stdlib.h>29#include <string.h>3031#include <atf-c.h>3233#include "atf-c/detail/sanity.h"34#include "atf-c/detail/test_helpers.h"3536/* ---------------------------------------------------------------------37* Auxiliary functions.38* --------------------------------------------------------------------- */3940#define REQUIRE_ERROR(exp) \41do { \42atf_error_t err = exp; \43ATF_REQUIRE(atf_is_error(err)); \44atf_error_free(err); \45} while (0)4647static48size_t49array_size(const char *words[])50{51size_t count;52const char **word;5354count = 0;55for (word = words; *word != NULL; word++)56count++;5758return count;59}6061static62void63check_split(const char *str, const char *delim, const char *words[])64{65atf_list_t list;66const char **word;67size_t i;6869printf("Splitting '%s' with delimiter '%s'\n", str, delim);70CE(atf_text_split(str, delim, &list));7172printf("Expecting %zd words\n", array_size(words));73ATF_CHECK_EQ(atf_list_size(&list), array_size(words));7475for (word = words, i = 0; *word != NULL; word++, i++) {76printf("Word at position %zd should be '%s'\n", i, words[i]);77ATF_CHECK_STREQ((const char *)atf_list_index_c(&list, i), words[i]);78}7980atf_list_fini(&list);81}8283static84atf_error_t85word_acum(const char *word, void *data)86{87char *acum = data;8889strcat(acum, word);9091return atf_no_error();92}9394static95atf_error_t96word_count(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)97{98size_t *counter = data;99100(*counter)++;101102return atf_no_error();103}104105struct fail_at {106int failpos;107int curpos;108};109110static111atf_error_t112word_fail_at(const char *word ATF_DEFS_ATTRIBUTE_UNUSED, void *data)113{114struct fail_at *fa = data;115atf_error_t err;116117if (fa->failpos == fa->curpos)118err = atf_no_memory_error(); /* Just a random error. */119else {120fa->curpos++;121err = atf_no_error();122}123124return err;125}126127/* ---------------------------------------------------------------------128* Test cases for the free functions.129* --------------------------------------------------------------------- */130131ATF_TC(for_each_word);132ATF_TC_HEAD(for_each_word, tc)133{134atf_tc_set_md_var(tc, "descr", "Checks the atf_text_for_each_word"135"function");136}137ATF_TC_BODY(for_each_word, tc)138{139size_t cnt;140char acum[1024];141142cnt = 0;143strcpy(acum, "");144RE(atf_text_for_each_word("1 2 3", " ", word_count, &cnt));145RE(atf_text_for_each_word("1 2 3", " ", word_acum, acum));146ATF_REQUIRE(cnt == 3);147ATF_REQUIRE(strcmp(acum, "123") == 0);148149cnt = 0;150strcpy(acum, "");151RE(atf_text_for_each_word("1 2 3", ".", word_count, &cnt));152RE(atf_text_for_each_word("1 2 3", ".", word_acum, acum));153ATF_REQUIRE(cnt == 1);154ATF_REQUIRE(strcmp(acum, "1 2 3") == 0);155156cnt = 0;157strcpy(acum, "");158RE(atf_text_for_each_word("1 2 3 4 5", " ", word_count, &cnt));159RE(atf_text_for_each_word("1 2 3 4 5", " ", word_acum, acum));160ATF_REQUIRE(cnt == 5);161ATF_REQUIRE(strcmp(acum, "12345") == 0);162163cnt = 0;164strcpy(acum, "");165RE(atf_text_for_each_word("1 2.3.4 5", " .", word_count, &cnt));166RE(atf_text_for_each_word("1 2.3.4 5", " .", word_acum, acum));167ATF_REQUIRE(cnt == 5);168ATF_REQUIRE(strcmp(acum, "12345") == 0);169170{171struct fail_at fa;172fa.failpos = 3;173fa.curpos = 0;174atf_error_t err = atf_text_for_each_word("a b c d e", " ",175word_fail_at, &fa);176ATF_REQUIRE(atf_is_error(err));177ATF_REQUIRE(atf_error_is(err, "no_memory"));178ATF_REQUIRE(fa.curpos == 3);179atf_error_free(err);180}181}182183ATF_TC(format);184ATF_TC_HEAD(format, tc)185{186atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "187"strings using a variable parameters list");188}189ATF_TC_BODY(format, tc)190{191char *str;192atf_error_t err;193194err = atf_text_format(&str, "%s %s %d", "Test", "string", 1);195ATF_REQUIRE(!atf_is_error(err));196ATF_REQUIRE(strcmp(str, "Test string 1") == 0);197free(str);198}199200static201void202format_ap(char **dest, const char *fmt, ...)203{204va_list ap;205atf_error_t err;206207va_start(ap, fmt);208err = atf_text_format_ap(dest, fmt, ap);209va_end(ap);210211ATF_REQUIRE(!atf_is_error(err));212}213214ATF_TC(format_ap);215ATF_TC_HEAD(format_ap, tc)216{217atf_tc_set_md_var(tc, "descr", "Checks the construction of free-form "218"strings using a va_list argument");219}220ATF_TC_BODY(format_ap, tc)221{222char *str;223224format_ap(&str, "%s %s %d", "Test", "string", 1);225ATF_REQUIRE(strcmp(str, "Test string 1") == 0);226free(str);227}228229ATF_TC(split);230ATF_TC_HEAD(split, tc)231{232atf_tc_set_md_var(tc, "descr", "Checks the split function");233}234ATF_TC_BODY(split, tc)235{236{237const char *words[] = { NULL };238check_split("", " ", words);239}240241{242const char *words[] = { NULL };243check_split(" ", " ", words);244}245246{247const char *words[] = { NULL };248check_split(" ", " ", words);249}250251{252const char *words[] = { "a", "b", NULL };253check_split("a b", " ", words);254}255256{257const char *words[] = { "a", "b", "c", "d", NULL };258check_split("a b c d", " ", words);259}260261{262const char *words[] = { "foo", "bar", NULL };263check_split("foo bar", " ", words);264}265266{267const char *words[] = { "foo", "bar", "baz", "foobar", NULL };268check_split("foo bar baz foobar", " ", words);269}270271{272const char *words[] = { "foo", "bar", NULL };273check_split(" foo bar", " ", words);274}275276{277const char *words[] = { "foo", "bar", NULL };278check_split("foo bar", " ", words);279}280281{282const char *words[] = { "foo", "bar", NULL };283check_split("foo bar ", " ", words);284}285286{287const char *words[] = { "foo", "bar", NULL };288check_split(" foo bar ", " ", words);289}290}291292ATF_TC(split_delims);293ATF_TC_HEAD(split_delims, tc)294{295atf_tc_set_md_var(tc, "descr", "Checks the split function using "296"different delimiters");297}298ATF_TC_BODY(split_delims, tc)299{300301{302const char *words[] = { NULL };303check_split("", "/", words);304}305306{307const char *words[] = { " ", NULL };308check_split(" ", "/", words);309}310311{312const char *words[] = { " ", NULL };313check_split(" ", "/", words);314}315316{317const char *words[] = { "a", "b", NULL };318check_split("a/b", "/", words);319}320321{322const char *words[] = { "a", "bcd", "ef", NULL };323check_split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM", words);324}325}326327ATF_TC(to_bool);328ATF_TC_HEAD(to_bool, tc)329{330atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_bool function");331}332ATF_TC_BODY(to_bool, tc)333{334bool b;335336RE(atf_text_to_bool("true", &b)); ATF_REQUIRE(b);337RE(atf_text_to_bool("TRUE", &b)); ATF_REQUIRE(b);338RE(atf_text_to_bool("yes", &b)); ATF_REQUIRE(b);339RE(atf_text_to_bool("YES", &b)); ATF_REQUIRE(b);340341RE(atf_text_to_bool("false", &b)); ATF_REQUIRE(!b);342RE(atf_text_to_bool("FALSE", &b)); ATF_REQUIRE(!b);343RE(atf_text_to_bool("no", &b)); ATF_REQUIRE(!b);344RE(atf_text_to_bool("NO", &b)); ATF_REQUIRE(!b);345346b = false;347REQUIRE_ERROR(atf_text_to_bool("", &b));348ATF_REQUIRE(!b);349b = true;350REQUIRE_ERROR(atf_text_to_bool("", &b));351ATF_REQUIRE(b);352353b = false;354REQUIRE_ERROR(atf_text_to_bool("tru", &b));355ATF_REQUIRE(!b);356b = true;357REQUIRE_ERROR(atf_text_to_bool("tru", &b));358ATF_REQUIRE(b);359360b = false;361REQUIRE_ERROR(atf_text_to_bool("true2", &b));362ATF_REQUIRE(!b);363b = true;364REQUIRE_ERROR(atf_text_to_bool("true2", &b));365ATF_REQUIRE(b);366367b = false;368REQUIRE_ERROR(atf_text_to_bool("fals", &b));369ATF_REQUIRE(!b);370b = true;371REQUIRE_ERROR(atf_text_to_bool("fals", &b));372ATF_REQUIRE(b);373374b = false;375REQUIRE_ERROR(atf_text_to_bool("false2", &b));376ATF_REQUIRE(!b);377b = true;378REQUIRE_ERROR(atf_text_to_bool("false2", &b));379ATF_REQUIRE(b);380}381382ATF_TC(to_long);383ATF_TC_HEAD(to_long, tc)384{385atf_tc_set_md_var(tc, "descr", "Checks the atf_text_to_long function");386}387ATF_TC_BODY(to_long, tc)388{389long l;390391RE(atf_text_to_long("0", &l)); ATF_REQUIRE_EQ(l, 0);392RE(atf_text_to_long("-5", &l)); ATF_REQUIRE_EQ(l, -5);393RE(atf_text_to_long("5", &l)); ATF_REQUIRE_EQ(l, 5);394RE(atf_text_to_long("123456789", &l)); ATF_REQUIRE_EQ(l, 123456789);395396l = 1212;397REQUIRE_ERROR(atf_text_to_long("", &l));398ATF_REQUIRE_EQ(l, 1212);399REQUIRE_ERROR(atf_text_to_long("foo", &l));400ATF_REQUIRE_EQ(l, 1212);401REQUIRE_ERROR(atf_text_to_long("1234x", &l));402ATF_REQUIRE_EQ(l, 1212);403}404405/* ---------------------------------------------------------------------406* Main.407* --------------------------------------------------------------------- */408409ATF_TP_ADD_TCS(tp)410{411ATF_TP_ADD_TC(tp, for_each_word);412ATF_TP_ADD_TC(tp, format);413ATF_TP_ADD_TC(tp, format_ap);414ATF_TP_ADD_TC(tp, split);415ATF_TP_ADD_TC(tp, split_delims);416ATF_TP_ADD_TC(tp, to_bool);417ATF_TP_ADD_TC(tp, to_long);418419return atf_no_error();420}421422423