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