VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstStrSimplePattern.cpp@ 14534

Last change on this file since 14534 was 13472, checked in by vboxsync, 16 years ago

IPRT: Added a couple of function for simple string pattern matching (from STAM).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1/* $Id: tstStrSimplePattern.cpp 13472 2008-10-22 09:19:00Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTStrSimplePattern.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/string.h>
36#include <iprt/stream.h>
37#include <iprt/err.h>
38#include <iprt/initterm.h>
39
40
41int main()
42{
43 int cErrors = 0;
44
45#define CHECK_EXPR(expr) \
46 do { bool const f = !!(expr); if (RT_UNLIKELY(!f)) { RTPrintf("tstStrSimplePattern(%d): %s!\n", __LINE__, #expr); cErrors++; } } while (0)
47#define CHECK_EXPR_MSG(expr, msg) \
48 do { \
49 bool const f = !!(expr); \
50 if (RT_UNLIKELY(!f)) { \
51 RTPrintf("tstStrSimplePattern(%d): %s!\n", __LINE__, #expr); \
52 RTPrintf("tstStrSimplePattern: "); \
53 RTPrintf msg; \
54 ++cErrors; \
55 } \
56 } while (0)
57
58 CHECK_EXPR(RTStrSimplePatternMatch("*", ""));
59 CHECK_EXPR(RTStrSimplePatternMatch("*", "asdfasdflkjasdlfkj"));
60 CHECK_EXPR(RTStrSimplePatternMatch("*?*?*?*?*", "asdfasdflkjasdlfkj"));
61 CHECK_EXPR(RTStrSimplePatternMatch("asdf??df", "asdfasdf"));
62 CHECK_EXPR(!RTStrSimplePatternMatch("asdf??dq", "asdfasdf"));
63 CHECK_EXPR(RTStrSimplePatternMatch("asdf*df", "asdfasdf"));
64 CHECK_EXPR(!RTStrSimplePatternMatch("asdf*dq", "asdfasdf"));
65 CHECK_EXPR(RTStrSimplePatternMatch("a*", "asdfasdf"));
66 CHECK_EXPR(RTStrSimplePatternMatch("a*f", "asdfasdf"));
67 CHECK_EXPR(!RTStrSimplePatternMatch("a*q", "asdfasdf"));
68 CHECK_EXPR(!RTStrSimplePatternMatch("a*q?", "asdfasdf"));
69 CHECK_EXPR(RTStrSimplePatternMatch("?*df", "asdfasdf"));
70
71 CHECK_EXPR(RTStrSimplePatternNMatch("*", 1, "", 0));
72 CHECK_EXPR(RTStrSimplePatternNMatch("*", ~(size_t)0, "", 0));
73 CHECK_EXPR(RTStrSimplePatternNMatch("*", ~(size_t)0, "", ~(size_t)0));
74 CHECK_EXPR(RTStrSimplePatternNMatch("*", 1, "asdfasdflkjasdlfkj", ~(size_t)0));
75 CHECK_EXPR(RTStrSimplePatternNMatch("*", ~(size_t)0, "asdfasdflkjasdlfkj", ~(size_t)0));
76 CHECK_EXPR(RTStrSimplePatternNMatch("*", 1, "asdfasdflkjasdlfkj", 3));
77 CHECK_EXPR(RTStrSimplePatternNMatch("*", 2, "asdfasdflkjasdlfkj", 10));
78 CHECK_EXPR(RTStrSimplePatternNMatch("*", 15, "asdfasdflkjasdlfkj", 10));
79 CHECK_EXPR(RTStrSimplePatternNMatch("*?*?*?*?*", 1, "asdfasdflkjasdlfkj", 128));
80 CHECK_EXPR(RTStrSimplePatternNMatch("*?*?*?*?*", 5, "asdfasdflkjasdlfkj", 0));
81 CHECK_EXPR(RTStrSimplePatternNMatch("*?*?*?*?*", 5, "asdfasdflkjasdlfkj", ~(size_t)0));
82 CHECK_EXPR(RTStrSimplePatternNMatch("*?*?*?*?*", ~(size_t)0, "asdfasdflkjasdlfkj", ~(size_t)0));
83 CHECK_EXPR(RTStrSimplePatternNMatch("asdf??df", 8, "asdfasdf", 8));
84 CHECK_EXPR(RTStrSimplePatternNMatch("asdf??df", ~(size_t)0, "asdfasdf", 8));
85 CHECK_EXPR(RTStrSimplePatternNMatch("asdf??df", ~(size_t)0, "asdfasdf", ~(size_t)0));
86 CHECK_EXPR(RTStrSimplePatternNMatch("asdf??df", 7, "asdfasdf", 7));
87 CHECK_EXPR(!RTStrSimplePatternNMatch("asdf??df", 7, "asdfasdf", 8));
88 CHECK_EXPR(!RTStrSimplePatternNMatch("asdf??dq", 8, "asdfasdf", 8));
89 CHECK_EXPR(RTStrSimplePatternNMatch("asdf??dq", 7, "asdfasdf", 7));
90 CHECK_EXPR(RTStrSimplePatternNMatch("asdf*df", 8, "asdfasdf", 8));
91 CHECK_EXPR(!RTStrSimplePatternNMatch("asdf*dq", 8, "asdfasdf", 8));
92 CHECK_EXPR(RTStrSimplePatternNMatch("a*", 10, "asdfasdf", 8));
93 CHECK_EXPR(RTStrSimplePatternNMatch("a*f", 3, "asdfasdf", ~(size_t)0));
94 CHECK_EXPR(!RTStrSimplePatternNMatch("a*q", 3, "asdfasdf", ~(size_t)0));
95 CHECK_EXPR(!RTStrSimplePatternNMatch("a*q?", 4, "asdfasdf", 9));
96 CHECK_EXPR(RTStrSimplePatternNMatch("?*df", 4, "asdfasdf", 8));
97
98 size_t offPattern;
99 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a*f|a??t", ~(size_t)0, "asdf", 4, NULL));
100 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a*f|a??t", ~(size_t)0, "asdf", 4, &offPattern));
101 CHECK_EXPR(offPattern == 5);
102 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a??t|a??f", ~(size_t)0, "asdf", 4, NULL));
103 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a??t|a??f", ~(size_t)0, "asdf", 4, &offPattern));
104 CHECK_EXPR(offPattern == 10);
105 CHECK_EXPR(RTStrSimplePatternMultiMatch("a*f|a??t|a??f", ~(size_t)0, "asdf", 4, NULL));
106 CHECK_EXPR(RTStrSimplePatternMultiMatch("a*f|a??t|a??f", ~(size_t)0, "asdf", 4, &offPattern));
107 CHECK_EXPR(offPattern == 0);
108 CHECK_EXPR(!RTStrSimplePatternMultiMatch("asdq|a??y|a??x", ~(size_t)0, "asdf", 4, NULL));
109 CHECK_EXPR(!RTStrSimplePatternMultiMatch("asdq|a??y|a??x", ~(size_t)0, "asdf", 4, &offPattern));
110 CHECK_EXPR(offPattern == ~(size_t)0);
111 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 9, "asdf", 4, NULL));
112 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 8, "asdf", 4, NULL));
113 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 7, "asdf", 4, NULL));
114 CHECK_EXPR(!RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 6, "asdf", 4, NULL));
115 CHECK_EXPR(!RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 5, "asdf", 4, NULL));
116 CHECK_EXPR(!RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 4, "asdf", 4, NULL));
117 CHECK_EXPR(!RTStrSimplePatternMultiMatch("asdq|a*f|a??t", 3, "asdf", 4, NULL));
118 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdf", 4, "asdf", 4, NULL));
119 CHECK_EXPR(RTStrSimplePatternMultiMatch("asdf|", 5, "asdf", 4, NULL));
120
121
122 /*
123 * Summary.
124 */
125 if (!cErrors)
126 RTPrintf("tstStrToNum: SUCCESS\n");
127 else
128 RTPrintf("tstStrToNum: FAILURE - %d errors\n", cErrors);
129 return !!cErrors;
130}
131
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette