+// ----------------------------------------------------------------------------
+// regular expressions
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_REGEX
+
+#include "wx/regex.h"
+
+static void TestRegExCompile()
+{
+ wxPuts(_T("*** Testing RE compilation ***\n"));
+
+ static struct RegExCompTestData
+ {
+ const wxChar *pattern;
+ bool correct;
+ } regExCompTestData[] =
+ {
+ { _T("foo"), true },
+ { _T("foo("), false },
+ { _T("foo(bar"), false },
+ { _T("foo(bar)"), true },
+ { _T("foo["), false },
+ { _T("foo[bar"), false },
+ { _T("foo[bar]"), true },
+ { _T("foo{"), true },
+ { _T("foo{1"), false },
+ { _T("foo{bar"), true },
+ { _T("foo{1}"), true },
+ { _T("foo{1,2}"), true },
+ { _T("foo{bar}"), true },
+ { _T("foo*"), true },
+ { _T("foo**"), false },
+ { _T("foo+"), true },
+ { _T("foo++"), false },
+ { _T("foo?"), true },
+ { _T("foo??"), false },
+ { _T("foo?+"), false },
+ };
+
+ wxRegEx re;
+ for ( size_t n = 0; n < WXSIZEOF(regExCompTestData); n++ )
+ {
+ const RegExCompTestData& data = regExCompTestData[n];
+ bool ok = re.Compile(data.pattern);
+
+ wxPrintf(_T("'%s' is %sa valid RE (%s)\n"),
+ data.pattern,
+ ok ? _T("") : _T("not "),
+ ok == data.correct ? _T("ok") : _T("ERROR"));
+ }
+}
+
+static void TestRegExMatch()
+{
+ wxPuts(_T("*** Testing RE matching ***\n"));
+
+ static struct RegExMatchTestData
+ {
+ const wxChar *pattern;
+ const wxChar *text;
+ bool correct;
+ } regExMatchTestData[] =
+ {
+ { _T("foo"), _T("bar"), false },
+ { _T("foo"), _T("foobar"), true },
+ { _T("^foo"), _T("foobar"), true },
+ { _T("^foo"), _T("barfoo"), false },
+ { _T("bar$"), _T("barbar"), true },
+ { _T("bar$"), _T("barbar "), false },
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ )
+ {
+ const RegExMatchTestData& data = regExMatchTestData[n];
+
+ wxRegEx re(data.pattern);
+ bool ok = re.Matches(data.text);
+
+ wxPrintf(_T("'%s' %s %s (%s)\n"),
+ data.pattern,
+ ok ? _T("matches") : _T("doesn't match"),
+ data.text,
+ ok == data.correct ? _T("ok") : _T("ERROR"));
+ }
+}
+
+static void TestRegExSubmatch()
+{
+ wxPuts(_T("*** Testing RE subexpressions ***\n"));
+
+ wxRegEx re(_T("([[:alpha:]]+) ([[:alpha:]]+) ([[:digit:]]+).*([[:digit:]]+)$"));
+ if ( !re.IsValid() )
+ {
+ wxPuts(_T("ERROR: compilation failed."));
+ return;
+ }
+
+ wxString text = _T("Fri Jul 13 18:37:52 CEST 2001");
+
+ if ( !re.Matches(text) )
+ {
+ wxPuts(_T("ERROR: match expected."));
+ }
+ else
+ {
+ wxPrintf(_T("Entire match: %s\n"), re.GetMatch(text).c_str());
+
+ wxPrintf(_T("Date: %s/%s/%s, wday: %s\n"),
+ re.GetMatch(text, 3).c_str(),
+ re.GetMatch(text, 2).c_str(),
+ re.GetMatch(text, 4).c_str(),
+ re.GetMatch(text, 1).c_str());
+ }
+}
+
+static void TestRegExReplacement()
+{
+ wxPuts(_T("*** Testing RE replacement ***"));
+
+ static struct RegExReplTestData
+ {
+ const wxChar *text;
+ const wxChar *repl;
+ const wxChar *result;
+ size_t count;
+ } regExReplTestData[] =
+ {
+ { _T("foo123"), _T("bar"), _T("bar"), 1 },
+ { _T("foo123"), _T("\\2\\1"), _T("123foo"), 1 },
+ { _T("foo_123"), _T("\\2\\1"), _T("123foo"), 1 },
+ { _T("123foo"), _T("bar"), _T("123foo"), 0 },
+ { _T("123foo456foo"), _T("&&"), _T("123foo456foo456foo"), 1 },
+ { _T("foo123foo123"), _T("bar"), _T("barbar"), 2 },
+ { _T("foo123_foo456_foo789"), _T("bar"), _T("bar_bar_bar"), 3 },
+ };
+
+ const wxChar *pattern = _T("([a-z]+)[^0-9]*([0-9]+)");
+ wxRegEx re(pattern);
+
+ wxPrintf(_T("Using pattern '%s' for replacement.\n"), pattern);
+
+ for ( size_t n = 0; n < WXSIZEOF(regExReplTestData); n++ )
+ {
+ const RegExReplTestData& data = regExReplTestData[n];
+
+ wxString text = data.text;
+ size_t nRepl = re.Replace(&text, data.repl);
+
+ wxPrintf(_T("%s =~ s/RE/%s/g: %u match%s, result = '%s' ("),
+ data.text, data.repl,
+ nRepl, nRepl == 1 ? _T("") : _T("es"),
+ text.c_str());
+ if ( text == data.result && nRepl == data.count )
+ {
+ wxPuts(_T("ok)"));
+ }
+ else
+ {
+ wxPrintf(_T("ERROR: should be %u and '%s')\n"),
+ data.count, data.result);
+ }
+ }
+}
+
+static void TestRegExInteractive()
+{
+ wxPuts(_T("*** Testing RE interactively ***"));
+
+ for ( ;; )
+ {
+ wxChar pattern[128];
+ wxPrintf(_T("\nEnter a pattern: "));
+ if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
+ break;
+
+ // kill the last '\n'
+ pattern[wxStrlen(pattern) - 1] = 0;
+
+ wxRegEx re;
+ if ( !re.Compile(pattern) )
+ {
+ continue;
+ }
+
+ wxChar text[128];
+ for ( ;; )
+ {
+ wxPrintf(_T("Enter text to match: "));
+ if ( !wxFgets(text, WXSIZEOF(text), stdin) )
+ break;
+
+ // kill the last '\n'
+ text[wxStrlen(text) - 1] = 0;
+
+ if ( !re.Matches(text) )
+ {
+ wxPrintf(_T("No match.\n"));
+ }
+ else
+ {
+ wxPrintf(_T("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
+
+ size_t start, len;
+ for ( size_t n = 1; ; n++ )
+ {
+ if ( !re.GetMatch(&start, &len, n) )
+ {
+ break;
+ }
+
+ wxPrintf(_T("Subexpr %u matched '%s'\n"),
+ n, wxString(text + start, len).c_str());
+ }
+ }
+ }
+ }
+}
+
+#endif // TEST_REGEX
+
+// ----------------------------------------------------------------------------
+// database
+// ----------------------------------------------------------------------------
+
+#if !wxUSE_ODBC
+ #undef TEST_ODBC
+#endif
+
+#ifdef TEST_ODBC
+
+#include <wx/db.h>
+
+static void TestDbOpen()
+{
+ HENV henv;
+ wxDb db(henv);
+}
+
+#endif // TEST_ODBC
+
+// ----------------------------------------------------------------------------
+// printf() tests
+// ----------------------------------------------------------------------------
+
+/*
+ NB: this stuff was taken from the glibc test suite and modified to build
+ in wxWindows: if I read the copyright below properly, this shouldn't
+ be a problem
+ */
+
+#ifdef TEST_PRINTF
+
+#ifdef wxTEST_PRINTF
+ // use our functions from wxchar.cpp
+ #undef wxPrintf
+ #undef wxSprintf
+
+ // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
+ // in the tests below
+ int wxPrintf( const wxChar *format, ... );
+ int wxSprintf( wxChar *str, const wxChar *format, ... );
+#endif
+
+#include "wx/longlong.h"
+
+#include <float.h>
+
+static void rfg1 (void);
+static void rfg2 (void);
+
+
+static void
+fmtchk (const wxChar *fmt)
+{
+ (void) wxPrintf(_T("%s:\t`"), fmt);
+ (void) wxPrintf(fmt, 0x12);
+ (void) wxPrintf(_T("'\n"));
+}
+
+static void
+fmtst1chk (const wxChar *fmt)
+{
+ (void) wxPrintf(_T("%s:\t`"), fmt);
+ (void) wxPrintf(fmt, 4, 0x12);
+ (void) wxPrintf(_T("'\n"));
+}
+
+static void
+fmtst2chk (const wxChar *fmt)
+{
+ (void) wxPrintf(_T("%s:\t`"), fmt);
+ (void) wxPrintf(fmt, 4, 4, 0x12);
+ (void) wxPrintf(_T("'\n"));
+}
+
+/* This page is covered by the following copyright: */
+
+/* (C) Copyright C E Chew
+ *
+ * Feel free to copy, use and distribute this software provided:
+ *
+ * 1. you do not pretend that you wrote it
+ * 2. you leave this copyright notice intact.
+ */
+
+/*
+ * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
+ */
+
+#define DEC -123
+#define INT 255
+#define UNS (~0)
+
+/* Formatted Output Test
+ *
+ * This exercises the output formatting code.
+ */
+
+static void
+fp_test (void)
+{
+ int i, j, k, l;
+ wxChar buf[7];
+ wxChar *prefix = buf;
+ wxChar tp[20];
+
+ wxPuts(_T("\nFormatted output test"));
+ wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
+ wxStrcpy(prefix, _T("%"));
+ for (i = 0; i < 2; i++) {
+ for (j = 0; j < 2; j++) {
+ for (k = 0; k < 2; k++) {
+ for (l = 0; l < 2; l++) {
+ wxStrcpy(prefix, _T("%"));
+ if (i == 0) wxStrcat(prefix, _T("-"));
+ if (j == 0) wxStrcat(prefix, _T("+"));
+ if (k == 0) wxStrcat(prefix, _T("#"));
+ if (l == 0) wxStrcat(prefix, _T("0"));
+ wxPrintf(_T("%5s |"), prefix);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6d |"));
+ wxPrintf(tp, DEC);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6o |"));
+ wxPrintf(tp, INT);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6x |"));
+ wxPrintf(tp, INT);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6X |"));
+ wxPrintf(tp, INT);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6u |"));
+ wxPrintf(tp, UNS);
+ wxPrintf(_T("\n"));
+ }
+ }
+ }
+ }
+ wxPrintf(_T("%10s\n"), (wxChar *) NULL);
+ wxPrintf(_T("%-10s\n"), (wxChar *) NULL);
+}
+
+static void TestPrintf()
+{
+ static wxChar shortstr[] = _T("Hi, Z.");
+ static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \
+I am ready for my first lesson today.");
+ int result = 0;
+
+ fmtchk(_T("%.4x"));
+ fmtchk(_T("%04x"));
+ fmtchk(_T("%4.4x"));
+ fmtchk(_T("%04.4x"));
+ fmtchk(_T("%4.3x"));
+ fmtchk(_T("%04.3x"));
+
+ fmtst1chk(_T("%.*x"));
+ fmtst1chk(_T("%0*x"));
+ fmtst2chk(_T("%*.*x"));
+ fmtst2chk(_T("%0*.*x"));
+
+ wxPrintf(_T("bad format:\t\"%b\"\n"));
+ wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
+
+ wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
+ wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
+ wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
+ wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
+ wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
+ wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
+ wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
+ wxPrintf(_T("left-adjusted ZLDN:\t\"%-010ld\"\n"), -123456);
+ wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
+ wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
+
+ wxPrintf(_T("zero-padded string:\t\"%010s\"\n"), shortstr);
+ wxPrintf(_T("left-adjusted Z string:\t\"%-010s\"\n"), shortstr);
+ wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
+ wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
+ wxPrintf(_T("null string:\t\"%s\"\n"), (wxChar *)NULL);
+ wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
+
+ wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
+ wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
+ wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
+ wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
+ wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
+ wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
+ wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
+ wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
+ wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
+ wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
+ wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
+ wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
+
+ wxPrintf (_T(" %6.5f\n"), .099999999860301614);
+ wxPrintf (_T(" %6.5f\n"), .1);
+ wxPrintf (_T("x%5.4fx\n"), .5);
+
+ wxPrintf (_T("%#03x\n"), 1);
+
+ //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
+
+ {
+ double d = FLT_MIN;
+ int niter = 17;
+
+ while (niter-- != 0)
+ wxPrintf (_T("%.17e\n"), d / 2);
+ fflush (stdout);
+ }
+
+ wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
+
+#define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
+ wxPrintf (FORMAT, 0.0, 0.0, 0.0);
+ wxPrintf (FORMAT, 1.0, 1.0, 1.0);
+ wxPrintf (FORMAT, -1.0, -1.0, -1.0);
+ wxPrintf (FORMAT, 100.0, 100.0, 100.0);
+ wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
+ wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
+ wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
+ wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
+ wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
+#undef FORMAT
+
+ {
+ wxChar buf[20];
+ int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
+
+ wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
+ rc, WXSIZEOF(buf), buf);
+#if 0
+ wxChar buf2[512];
+ wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
+ wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
+#endif
+ }
+
+ fp_test ();
+
+ wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
+ wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
+ wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
+ wxPrintf (_T("%g should be 123.456\n"), 123.456);
+ wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
+ wxPrintf (_T("%g should be 10\n"), 10.0);
+ wxPrintf (_T("%g should be 0.02\n"), 0.02);
+
+ {
+ double x=1.0;
+ wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
+ }
+
+ {
+ wxChar buf[200];
+
+ wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
+
+ result |= wxStrcmp (buf,
+ _T("onetwo three "));
+
+ wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
+ }
+
+#ifdef wxLongLong_t
+ {
+ wxChar buf[200];
+
+ wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
+ wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
+
+ if (wxStrcmp (buf, _T("40000000000")) != 0)
+ {
+ result = 1;
+ wxPuts (_T("\tFAILED"));
+ }
+ wxPuts (_T(""));
+ }
+#endif // wxLongLong_t
+
+ wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
+ wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
+
+ wxPuts (_T("--- Should be no further output. ---"));
+ rfg1 ();
+ rfg2 ();
+
+#if 0
+ {
+ wxChar bytes[7];
+ wxChar buf[20];
+
+ memset (bytes, '\xff', sizeof bytes);
+ wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
+ if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
+ || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
+ {
+ wxPuts (_T("%hhn overwrite more bytes"));
+ result = 1;
+ }
+ if (bytes[3] != 3)
+ {
+ wxPuts (_T("%hhn wrote incorrect value"));
+ result = 1;
+ }
+ }
+#endif
+}
+
+static void
+rfg1 (void)
+{
+ wxChar buf[100];
+
+ wxSprintf (buf, _T("%5.s"), _T("xyz"));
+ if (wxStrcmp (buf, _T(" ")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
+ wxSprintf (buf, _T("%5.f"), 33.3);
+ if (wxStrcmp (buf, _T(" 33")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
+ wxSprintf (buf, _T("%8.e"), 33.3e7);
+ if (wxStrcmp (buf, _T(" 3e+08")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
+ wxSprintf (buf, _T("%8.E"), 33.3e7);
+ if (wxStrcmp (buf, _T(" 3E+08")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
+ wxSprintf (buf, _T("%.g"), 33.3);
+ if (wxStrcmp (buf, _T("3e+01")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
+ wxSprintf (buf, _T("%.G"), 33.3);
+ if (wxStrcmp (buf, _T("3E+01")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
+}
+
+static void
+rfg2 (void)
+{
+ int prec;
+ wxChar buf[100];
+
+ prec = 0;
+ wxSprintf (buf, _T("%.*g"), prec, 3.3);
+ if (wxStrcmp (buf, _T("3")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
+ prec = 0;
+ wxSprintf (buf, _T("%.*G"), prec, 3.3);
+ if (wxStrcmp (buf, _T("3")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
+ prec = 0;
+ wxSprintf (buf, _T("%7.*G"), prec, 3.33);
+ if (wxStrcmp (buf, _T(" 3")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
+ prec = 3;
+ wxSprintf (buf, _T("%04.*o"), prec, 33);
+ if (wxStrcmp (buf, _T(" 041")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
+ prec = 7;
+ wxSprintf (buf, _T("%09.*u"), prec, 33);
+ if (wxStrcmp (buf, _T(" 0000033")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
+ prec = 3;
+ wxSprintf (buf, _T("%04.*x"), prec, 33);
+ if (wxStrcmp (buf, _T(" 021")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
+ prec = 3;
+ wxSprintf (buf, _T("%04.*X"), prec, 33);
+ if (wxStrcmp (buf, _T(" 021")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
+}
+
+#endif // TEST_PRINTF
+