test-string-util.c revision c174983474d4a010a18e3bb9a59e351a442480f5
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann/***
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann This file is part of systemd.
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann Copyright 2015 Lennart Poettering
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann systemd is free software; you can redistribute it and/or modify it
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann under the terms of the GNU Lesser General Public License as published by
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann the Free Software Foundation; either version 2.1 of the License, or
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann (at your option) any later version.
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann systemd is distributed in the hope that it will be useful, but
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann WITHOUT ANY WARRANTY; without even the implied warranty of
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann Lesser General Public License for more details.
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann You should have received a copy of the GNU Lesser General Public License
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann along with systemd; If not, see <http://www.gnu.org/licenses/>.
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann***/
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann#include "string-util.h"
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmannstatic void test_string_erase(void) {
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann char *x;
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), ""));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("1");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), "x"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("12");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), "xx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("123");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), "xxx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("1234");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), "xxxx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("12345");
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann assert_se(streq(string_erase(x), "xxxxx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann x = strdupa("123456");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), "xxxxxx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann x = strdupa("1234567");
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(streq(string_erase(x), "xxxxxxx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann x = strdupa("12345678");
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann assert_se(streq(string_erase(x), "xxxxxxxx"));
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann x = strdupa("123456789");
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann assert_se(streq(string_erase(x), "xxxxxxxxx"));
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann}
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmannstatic void test_ascii_strcasecmp_n(void) {
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
7566e26721ee95d6fc864e9e6654fb61bd3cd603Zbigniew Jędrzejewski-Szmek assert_se(ascii_strcasecmp_n("", "", 0) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("", "", 1) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("", "a", 1) < 0);
7566e26721ee95d6fc864e9e6654fb61bd3cd603Zbigniew Jędrzejewski-Szmek assert_se(ascii_strcasecmp_n("", "a", 2) < 0);
2ad8887a12aeff9108606bb31e1557103a3b95dfThomas Hindoe Paaboel Andersen assert_se(ascii_strcasecmp_n("a", "", 1) > 0);
2ad8887a12aeff9108606bb31e1557103a3b95dfThomas Hindoe Paaboel Andersen assert_se(ascii_strcasecmp_n("a", "", 2) > 0);
2ad8887a12aeff9108606bb31e1557103a3b95dfThomas Hindoe Paaboel Andersen assert_se(ascii_strcasecmp_n("a", "a", 1) == 0);
2ad8887a12aeff9108606bb31e1557103a3b95dfThomas Hindoe Paaboel Andersen assert_se(ascii_strcasecmp_n("a", "a", 2) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("a", "b", 1) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("a", "b", 2) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("b", "a", 1) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("b", "a", 2) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxYxxxx", 9) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxyxxxx", 9) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxyxxxx", 9) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxxxxxx", "xxxxYxxxx", 9) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxXxxxx", "xxxxYxxxx", 9) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxYxxxx", 9) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxxxxxx", 9) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxyxxxx", "xxxxXxxxx", 9) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxxxxxx", 9) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_n("xxxxYxxxx", "xxxxXxxxx", 9) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann}
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmannstatic void test_ascii_strcasecmp_nn(void) {
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann assert_se(ascii_strcasecmp_nn("", 0, "", 0) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("", 0, "", 1) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("", 1, "", 0) > 0);
cf347234ae78630dd4aad8907f027965e7fb9040David Herrmann assert_se(ascii_strcasecmp_nn("", 1, "", 1) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaAa", 4) == 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaa", 3, "aaAa", 4) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaa", 4, "aaAa", 4) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaaa", 4, "aaA", 3) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaaa", 4, "AAA", 4) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaaa", 4, "bbbb", 4) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("aaAA", 4, "BBbb", 4) < 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0);
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann}
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmannint main(int argc, char *argv[]) {
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann test_string_erase();
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann test_ascii_strcasecmp_n();
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann test_ascii_strcasecmp_nn();
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann return 0;
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann}
279da1e3f99b9c767a69849b5445e3cfd8d83376David Herrmann