platform_bionic/tests/fnmatch_test.cpp
Elliott Hughes 6675ad3227 Add trivial tests for fnmatch(3).
These cover the parts of fnmatch() that I think I understand. Missing
FNM_LEADING_DIR because I still haven't understood that yet. All the
descriptions of its behavior that I've seen are slightly (or very)
inconsistent, and it seems like the implementations are genuinely
different too.

Test: treehugger
Change-Id: I1284c406639bef1bfe74db3cad45965c8ab29c2b
2020-12-02 14:36:13 -08:00

86 lines
3.1 KiB
C++

/*
* Copyright (C) 2020 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <gtest/gtest.h>
#include <fnmatch.h>
TEST(fnmatch, basic) {
EXPECT_EQ(0, fnmatch("abc", "abc", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("abc", "abd", 0));
}
TEST(fnmatch, casefold) {
EXPECT_EQ(FNM_NOMATCH, fnmatch("abc", "aBc", 0));
EXPECT_EQ(0, fnmatch("abc", "aBc", FNM_CASEFOLD));
}
TEST(fnmatch, character_class) {
// Literal.
EXPECT_EQ(0, fnmatch("ab[cd]", "abc", 0));
EXPECT_EQ(0, fnmatch("ab[cd]", "abd", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab[cd]", "abe", 0));
// Inverted literal.
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab[^cd]", "abc", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab[^cd]", "abd", 0));
EXPECT_EQ(0, fnmatch("ab[^cd]", "abe", 0));
// Range.
EXPECT_EQ(0, fnmatch("a[0-9]b", "a0b", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("a[0-9]b", "aOb", 0));
// Inverted range.
EXPECT_EQ(0, fnmatch("a[^0-9]b", "aOb", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("a[^0-9]b", "a0b", 0));
// Named.
EXPECT_EQ(0, fnmatch("a[[:digit:]]b", "a0b", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("a[[:digit:]]b", "aOb", 0));
// Inverted named.
EXPECT_EQ(FNM_NOMATCH, fnmatch("a[^[:digit:]]b", "a0b", 0));
EXPECT_EQ(0, fnmatch("a[^[:digit:]]b", "aOb", 0));
}
TEST(fnmatch, wild_any) {
EXPECT_EQ(0, fnmatch("ab*", "ab", 0));
EXPECT_EQ(0, fnmatch("ab*", "abc", 0));
EXPECT_EQ(0, fnmatch("ab*", "abcd", 0));
EXPECT_EQ(0, fnmatch("ab*", "ab/cd", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab*", "ab/cd", FNM_PATHNAME));
}
TEST(fnmatch, wild_one) {
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?", "ab", 0));
EXPECT_EQ(0, fnmatch("ab?", "abc", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?", "abcd", 0));
EXPECT_EQ(0, fnmatch("ab?d", "abcd", 0));
EXPECT_EQ(0, fnmatch("ab?cd", "ab/cd", 0));
EXPECT_EQ(FNM_NOMATCH, fnmatch("ab?cd", "ab/cd", FNM_PATHNAME));
}