| 1 | #!/usr/bin/env python2
|
| 2 | """
|
| 3 | test_switch.py
|
| 4 | """
|
| 5 | from __future__ import print_function
|
| 6 |
|
| 7 | import os
|
| 8 |
|
| 9 | from mycpp.mylib import switch, str_switch, log
|
| 10 |
|
| 11 |
|
| 12 | def TestString(s):
|
| 13 | # type: (str) -> None
|
| 14 |
|
| 15 | #print('''
|
| 16 | with str_switch(s) as case:
|
| 17 | # Problem: if you switch on length, do you duplicate the bogies
|
| 18 | if case('spam'):
|
| 19 | print('== %s ==' % s)
|
| 20 | print('SPAM')
|
| 21 | print('yes')
|
| 22 |
|
| 23 | elif case('foo'):
|
| 24 | print('== %s ==' % s)
|
| 25 | print('FOO')
|
| 26 |
|
| 27 | elif case('bar'): # same length
|
| 28 | print('== %s ==' % s)
|
| 29 | print('BAR')
|
| 30 |
|
| 31 | else:
|
| 32 | print('== %s ==' % s)
|
| 33 | print('neither')
|
| 34 | #''')
|
| 35 | print('--')
|
| 36 | print('')
|
| 37 |
|
| 38 |
|
| 39 | def TestNumSwitch():
|
| 40 | # type: () -> None
|
| 41 |
|
| 42 | x = 5
|
| 43 | with switch(x) as case:
|
| 44 | if case(0):
|
| 45 | print('zero')
|
| 46 | print('zero')
|
| 47 |
|
| 48 | elif case(1, 2):
|
| 49 | print('one or two')
|
| 50 |
|
| 51 | elif case(3, 4):
|
| 52 | print('three or four')
|
| 53 |
|
| 54 | else:
|
| 55 | print('default')
|
| 56 | print('another')
|
| 57 |
|
| 58 |
|
| 59 | def run_tests():
|
| 60 | # type: () -> None
|
| 61 |
|
| 62 | TestString('spam')
|
| 63 | TestString('bar')
|
| 64 | TestString('zzz') # same length as bar
|
| 65 | TestString('different len')
|
| 66 |
|
| 67 | TestNumSwitch()
|
| 68 |
|
| 69 |
|
| 70 | def run_benchmarks():
|
| 71 | # type: () -> None
|
| 72 | raise NotImplementedError()
|
| 73 |
|
| 74 |
|
| 75 | if __name__ == '__main__':
|
| 76 | if os.getenv('BENCHMARK'):
|
| 77 | log('Benchmarking...')
|
| 78 | run_benchmarks()
|
| 79 | else:
|
| 80 | run_tests()
|