Lichen

Annotated tests/string.py

792:d70932955645
2017-03-31 Paul Boddie Fixed non-recognition of deferred references in non-module, non-function scopes.
paul@292 1
s = "Hello"
paul@292 2
s += " world!"
paul@292 3
print s                     # Hello world!
paul@378 4
print len(s)                # 12
paul@384 5
print s[:5]                 # Hello
paul@384 6
print s[5:]                 #  world!
paul@384 7
print s[1:10:2]             # el ol
paul@384 8
print s[10:1:-2]            # drwol
paul@519 9
print s[11:]                # !
paul@519 10
print s[:-11]               # H
paul@519 11
print s[12:]                #
paul@519 12
print s[:-12]               #
paul@416 13
print s.find("w")           # 6
paul@416 14
print s.find("w", 7)        # -1
paul@416 15
print s.find("w", 0, 6)     # -1
paul@416 16
print s.index("o")          # 4
paul@515 17
print s.rfind("o")          # 7
paul@515 18
print s.rfind("o", 7)       # 7
paul@515 19
print s.rfind("o", 8)       # -1
paul@515 20
print s.rfind("o", 0, 7)    # 4
paul@416 21
paul@416 22
try:
paul@416 23
    print s.index("p")      # should raise an exception
paul@416 24
except ValueError, exc:
paul@518 25
    print 's.index("p"): value is not appropriate', repr(exc.value)
paul@416 26
paul@416 27
print s.startswith("Hello") # True
paul@416 28
print s.startswith("world") # False
paul@416 29
print s.endswith("world!")  # True
paul@416 30
print s.endswith("Hello")   # False
paul@292 31
paul@292 32
s2 = "Hello worlds!"
paul@292 33
print s2                    # Hello worlds!
paul@378 34
print len(s2)               # 13
paul@292 35
print s < s2                # True
paul@292 36
print s <= s2               # True
paul@292 37
print s == s2               # False
paul@292 38
print s != s2               # True
paul@292 39
print s >= s2               # False
paul@292 40
print s > s2                # False
paul@292 41
paul@292 42
print s[0]                  # H
paul@292 43
print s[-1]                 # !
paul@296 44
paul@296 45
print ord(s[0])             # 72
paul@296 46
paul@296 47
try:
paul@296 48
    print ord(s)            # should raise an exception
paul@296 49
except ValueError, exc:
paul@518 50
    print "ord(s): value is not appropriate", repr(exc.value)
paul@300 51
paul@531 52
print chr(72)               # H
paul@531 53
print repr(chr(0))          # "\x00"
paul@531 54
paul@531 55
try:
paul@531 56
    print repr(chr(-1))     # should raise an exception
paul@531 57
except ValueError, exc:
paul@531 58
    print "chr(-1): value is not appropriate", exc.value
paul@531 59
paul@342 60
l = ["Hello", "world!"]
paul@342 61
s3 = " ".join(l)
paul@342 62
print s3                    # Hello world!
paul@378 63
print len(s3)               # 12
paul@342 64
paul@342 65
s4 = "".join(l)
paul@342 66
print s4                    # Helloworld!
paul@378 67
print len(s4)               # 11
paul@378 68
paul@378 69
s5 = "--".join(l)
paul@378 70
print s5                    # Hello--world!
paul@378 71
print len(s5)               # 13
paul@342 72
paul@494 73
print "# hash(s):",
paul@300 74
print hash(s)
paul@494 75
print "# hash(s2):",
paul@342 76
print hash(s2)
paul@494 77
print "# hash(s3):",
paul@342 78
print hash(s3)
paul@494 79
print "# hash(s4):",
paul@342 80
print hash(s4)
paul@494 81
print "# hash(s5):",
paul@378 82
print hash(s5)
paul@514 83
paul@514 84
# Test multiplication of strings.
paul@514 85
paul@514 86
s6 = "abc"
paul@514 87
print s6 * -1               #
paul@514 88
print s6 * 0                #
paul@514 89
print s6 * 1                # abc
paul@514 90
print s6 * 2                # abcabc
paul@514 91
print -1 * s6               #
paul@514 92
print 0 * s6                #
paul@514 93
print 1 * s6                # abc
paul@514 94
print 2 * s6                # abcabc
paul@515 95
paul@515 96
# Test splitting of strings.
paul@515 97
paul@515 98
s7 = "Hello...\n  world,\n  planet,\n  globe."
paul@515 99
print s7.split()            # ["Hello...", "world,", "planet,", "globe."]
paul@515 100
print s7.split(maxsplit=2)  # ["Hello...", "world,", "planet,\n  globe."]
paul@515 101
print s7.split("\n")        # ["Hello...", "  world,", "  planet,", "  globe."]
paul@517 102
paul@517 103
# NOTE: To test rsplit once list.insert is implemented.
paul@517 104
paul@517 105
# Test stripping of strings.
paul@517 106
paul@517 107
s8 = "  \nHello world\n  "
paul@517 108
print repr(s8.strip())      # "Hello world"
paul@517 109
print repr(s8.lstrip())     # "Hello world\n  "
paul@517 110
print repr(s8.rstrip())     # "  \nHello world"
paul@517 111
paul@517 112
s9 = "xyzHello worldXYZ"
paul@517 113
print repr(s9.strip("xyYZ"))    # "zHello worldX"
paul@517 114
print repr(s9.lstrip("xyYZ"))   # "zHello worldXYZ"
paul@517 115
print repr(s9.rstrip("xyYZ"))   # "xyzHello worldX"
paul@518 116
paul@518 117
# Test quoting of strings.
paul@518 118
paul@518 119
print repr('?\n?\r?\t"')    # "\xe6\n\xf8\r\xe5\t\""