File: test_fnmatch.cpp

package info (click to toggle)
emscripten 2.0.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,440 kB
  • sloc: ansic: 510,324; cpp: 384,763; javascript: 84,341; python: 51,362; sh: 50,019; pascal: 4,159; makefile: 3,409; asm: 2,150; lisp: 1,869; ruby: 488; cs: 142
file content (86 lines) | stat: -rw-r--r-- 2,991 bytes parent folder | download | duplicates (4)
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
80
81
82
83
84
85
86
/*
 * Copyright 2014 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

// 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;
    }
}