aboutsummaryrefslogtreecommitdiff
path: root/tests/core/fnmatch.c
blob: ebdb200909809c0d7fb63512647b2670199f1e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Begin test_fnmatch.cpp
#include <fnmatch.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class TestCase {
public:
    TestCase(const string& pattern, const string& testString, int flags, int expected) :
        pattern(pattern),
        testString(testString),
        flags(flags),
        expected(expected)
    {}
    string pattern;
    string testString;
    int flags;
    int expected;
};

int main()
{
    vector<TestCase> testCases;

    testCases.push_back(TestCase("*","anything",0,0));
    testCases.push_back(TestCase("*.txt","readme.txt",0,0));
    testCases.push_back(TestCase("*.txt","readme.info",0,FNM_NOMATCH));
    testCases.push_back(TestCase("*.t?t","readme.txt",0,0));
    testCases.push_back(TestCase("*.t?t","readme.tot",0,0));
    testCases.push_back(TestCase("*.t?t","readme.txxt",0,FNM_NOMATCH));
    testCases.push_back(TestCase("[a-g]1","c1",0,0));
    testCases.push_back(TestCase("[a-g]1","i1",0,FNM_NOMATCH));
    testCases.push_back(TestCase("[!a-g]1","i1",0,0));
    testCases.push_back(TestCase("a\\*","anything",0,FNM_NOMATCH));
    testCases.push_back(TestCase("a\\*","a*",0,0));
    testCases.push_back(TestCase("a\\*","a*",FNM_NOESCAPE,FNM_NOMATCH));
    testCases.push_back(TestCase("a\\*","a\\*",FNM_NOESCAPE,0));
    testCases.push_back(TestCase("*readme","/etc/readme",0,0));
    testCases.push_back(TestCase("*readme","/etc/readme",FNM_PATHNAME,FNM_NOMATCH));
    testCases.push_back(TestCase("/*/readme","/etc/readme",FNM_PATHNAME,0));
    testCases.push_back(TestCase("*readme","/etc/.readme",0,0));
    testCases.push_back(TestCase("*readme",".readme",FNM_PERIOD,FNM_NOMATCH));
    testCases.push_back(TestCase("*.readme","/etc/.readme",FNM_PERIOD,0));
    testCases.push_back(TestCase("*.readme","/etc/.readme",FNM_PERIOD|FNM_PATHNAME,FNM_NOMATCH));
    testCases.push_back(TestCase("/*/.readme","/etc/.readme",FNM_PERIOD|FNM_PATHNAME,0));
    testCases.push_back(TestCase("ReAdME","readme",0,FNM_NOMATCH));

    bool pass = true;

    for (vector<TestCase>::const_iterator it = testCases.begin(); it != testCases.end(); ++it)
    {
        int result = fnmatch(it->pattern.c_str(), it->testString.c_str(), it->flags);
        if (result == it->expected)
            cout << "Pass: ";
        else
        {
            cout << "Fail: ";
            pass = false;
        }

        cout << "fnmatch(" << it->pattern << ", " << it->testString << ", "
             << it->flags << ") returned " << result << ", expected "
             << it->expected << endl;
    }

    if (pass)
    {
        cout << "All tests passed." << endl;
        return 0;
    }
    else
    {
        cout << "Some tests failed." << endl;
        return 1;
    }
}