diff options
author | gbrandl <devnull@localhost> | 2007-04-25 17:06:07 +0200 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2007-04-25 17:06:07 +0200 |
commit | 41139bf9c8fdbe85cb27ec44abccd188b15ca6a8 (patch) | |
tree | d96b74ec8a274b3646b6fd2cd547f940c377f658 /tests | |
parent | 1b68b4671ab60608e2d308b0372b0629b3783360 (diff) | |
download | pygments-41139bf9c8fdbe85cb27ec44abccd188b15ca6a8.tar.gz |
[svn] Make mapfiles, make check, add D test file, make reindent.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/examplefiles/test.d | 135 | ||||
-rw-r--r-- | tests/run.py | 5 | ||||
-rw-r--r-- | tests/test_html_formatter.py | 2 | ||||
-rw-r--r-- | tests/test_token.py | 2 | ||||
-rw-r--r-- | tests/test_util.py | 13 |
5 files changed, 147 insertions, 10 deletions
diff --git a/tests/examplefiles/test.d b/tests/examplefiles/test.d new file mode 100644 index 00000000..02fe8f73 --- /dev/null +++ b/tests/examplefiles/test.d @@ -0,0 +1,135 @@ +// Created by Lionello Lunesu and placed in the public domain. +// This file has been modified from its original version. +// It has been formatted to fit your screen. +module phoneno; // optional +import std.stdio; // writefln +import std.ctype; // isdigit +import std.stream; // BufferedFile + +// Just for readability (imagine char[][][char[]]) +alias char[] string; +alias string[] stringarray; + +/// Strips non-digit characters from the string (COW) +string stripNonDigit( in string line ) +{ + string ret; + foreach(uint i, c; line) { + // Error: std.ctype.isdigit at C:\dmd\src\phobos\std\ctype.d(37) + // conflicts with std.stream.isdigit at C:\dmd\src\phobos\std\stream.d(2924) + if (!std.ctype.isdigit(c)) { + if (!ret) + ret = line[0..i]; + } + else if (ret) + ret ~= c; + } + return ret?ret:line; +} + +unittest { + assert( stripNonDigit("asdf") == "" ); + assert( stripNonDigit("\'13-=2 4kop") == "1324" ); +} + +/// Converts a word into a number, ignoring all non alpha characters +string wordToNum( in string word ) +{ +// translation table for the task at hand +const char[256] TRANSLATE = + " " // 0 + " 0123456789 " // 32 + " 57630499617851881234762239 " // 64 + " 57630499617851881234762239 " + " " + " " + " " + " "; + string ret; + foreach(c; cast(ubyte[])word) + if (TRANSLATE[c] != ' ') + ret ~= TRANSLATE[c]; + return ret; +} + +unittest { + // Test wordToNum using the table from the task description. + assert( "01112223334455666777888999" == + wordToNum("E | J N Q | R W X | D S Y | F T | A M | C I V | B K U | L O P | G H Z")); + assert( "01112223334455666777888999" == + wordToNum("e | j n q | r w x | d s y | f t | a m | c i v | b k u | l o p | g h z")); + assert( "0123456789" == + wordToNum("0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9")); +} + +void main( string[] args ) +{ + // This associative array maps a number to an array of words. + stringarray[string] num2words; + + foreach(string word; new BufferedFile("dictionary.txt" ) ) + num2words[ wordToNum(word) ] ~= word.dup; // must dup + + /// Finds all alternatives for the given number + /// (should have been stripped from non-digit characters) + stringarray _FindWords( string numbers, bool digitok ) + in { + assert(numbers.length > 0); + } + out(result) { + foreach (a; result) + assert( wordToNum(a) == numbers ); + } + body { + stringarray ret; + bool foundword = false; + for (uint t=1; t<=numbers.length; ++t) { + auto alternatives = numbers[0..t] in num2words; + if (!alternatives) + continue; + foundword = true; + if (numbers.length > t) { + // Combine all current alternatives with all alternatives + // of the rest (next piece can start with a digit) + foreach (a2; _FindWords( numbers[t..$], true ) ) + foreach(a1; *alternatives) + ret ~= a1 ~ " " ~ a2; + } + else + ret ~= *alternatives; // append these alternatives + } + // Try to keep 1 digit, only if we're allowed and no other + // alternatives were found + // Testing "ret.length" makes more sense than testing "foundword", + // but the other implementations seem to do just this. + if (digitok && !foundword) { //ret.length == 0 + if(numbers.length > 1) { + // Combine 1 digit with all altenatives from the rest + // (next piece can not start with a digit) + foreach (a; _FindWords( numbers[1..$], false ) ) + ret ~= numbers[0..1] ~ " " ~ a; + } + else + ret ~= numbers[0..1]; // just append this digit + } + return ret; + } + + /// (This function was inlined in the original program) + /// Finds all alternatives for the given phone number + /// Returns: array of strings + stringarray FindWords( string phone_number ) + { + if (!phone_number.length) + return null; + // Strip the non-digit characters from the phone number, and + // pass it to the recursive function (leading digit is allowed) + return _FindWords( stripNonDigit(phone_number), true ); + } + + // Read the phone numbers + foreach(string phone; new BufferedFile("input.txt" ) ) + foreach(alternative; FindWords( phone ) ) + writefln(phone, ": ", alternative ); +} + diff --git a/tests/run.py b/tests/run.py index 457a97a1..bd285c2b 100644 --- a/tests/run.py +++ b/tests/run.py @@ -76,8 +76,9 @@ def run_tests(with_coverage=False): WIDTH = 70 - print >>sys.stderr, ('Pygments Test Suite running %s, stand by...' % - (with_coverage and "with coverage analysis" or "")).center(WIDTH) + print >>sys.stderr, \ + ('Pygments Test Suite running %s, stand by...' % + (with_coverage and "with coverage analysis" or "")).center(WIDTH) print >>sys.stderr, ('(using Python %s)' % sys.version.split()[0]).center(WIDTH) print >>sys.stderr, '='*WIDTH diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py index e9583445..ed949a88 100644 --- a/tests/test_html_formatter.py +++ b/tests/test_html_formatter.py @@ -98,7 +98,7 @@ class HtmlFormatterTest(unittest.TestCase): fmt = HtmlFormatter() sd = fmt.get_style_defs() self.assert_(sd.startswith('.')) - + fmt = HtmlFormatter(cssclass='foo') sd = fmt.get_style_defs() self.assert_(sd.startswith('.foo')) diff --git a/tests/test_token.py b/tests/test_token.py index ac57126c..8cf779f7 100644 --- a/tests/test_token.py +++ b/tests/test_token.py @@ -46,6 +46,6 @@ class TokenTest(unittest.TestCase): except SystemExit: pass - + if __name__ == '__main__': unittest.main() diff --git a/tests/test_util.py b/tests/test_util.py index 737a40f4..4ea30400 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -18,7 +18,7 @@ class UtilTest(unittest.TestCase): def test_getoptions(self): raises = self.assertRaises equals = self.assertEquals - + equals(util.get_bool_opt({}, 'a', True), True) equals(util.get_bool_opt({}, 'a', 1), True) equals(util.get_bool_opt({}, 'a', 'true'), True) @@ -39,14 +39,14 @@ class UtilTest(unittest.TestCase): def f1(): """ docstring headline - + other text """ def f2(): """ docstring headline - + other text """ @@ -58,14 +58,15 @@ class UtilTest(unittest.TestCase): def analyse(text): return 0.5 analyse = util.make_analysator(analyse) - self.assertEquals(X.analyse(''), 0.5) + self.assertEquals(X.analyse(''), 0.5) def test_shebang_matches(self): self.assert_(util.shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?')) self.assert_(util.shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')) self.assert_(util.shebang_matches('#!/usr/bin/startsomethingwith python', r'python(2\.\d)?')) - self.assert_(util.shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?')) + self.assert_(util.shebang_matches('#!C:\\Python2.4\\Python.exe', + r'python(2\.\d)?')) self.failIf(util.shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')) self.failIf(util.shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')) @@ -84,6 +85,6 @@ class UtilTest(unittest.TestCase): '<?xml ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">')) self.assert_(util.looks_like_xml('<html xmlns>abc</html>')) self.failIf(util.looks_like_xml('<html>')) - + if __name__ == '__main__': unittest.main() |