def run_tests(): # type: () -> None log("escaped: %s", escape('<html>', True)) # TODO: Fix double escaping here s = 'xo--xo' log("%s\n", s.replace('xo', 'OX'))
def IntDemo(): # type: () -> None intlist = [] # type: List[int] # NOTE: this annotation is required, or MyPy has a partial type. # I thoguht it would be able to infer it from expressions below. intlist.append(1) intlist.append(2) intlist.append(3) log("len(intlist) = %d", len(intlist)) for i in intlist: log("i = %d", i) strlist = [] # type: List[str] strlist.append('a') strlist.append('b') log("len(strlist) = %d", len(strlist)) for s in strlist: log("s = %s", s) #strlist.pop() #strlist.pop() x = strlist.pop() log("len(strlist) = %d", len(strlist))
def run_tests(): # type: () -> None x = 33 # NOTE: This is very slow and should be separated result = fib_recursive(x) log('fib_recursive(%d) = %d', x, result)
def run_tests(): # type: () -> None print('foo' + 'bar') print('foo' * 3) obj = Foo() print('foo' + obj.s) s = 'mystr' print('[%s]' % s) s = 'mystr' print('[%s, %s]' % (s, 'abc')) print('%s: 5%%-100%%' % 'abc') print('<a href="foo.html">%s</a>' % 'anchor') log("foo? %d", 'foo' in s) log("str? %d", 'str' in s) print("'single'") print('"double"') # test escape codes print("a\tb\nc\td\n") x = 'x' print("%s\tb\n%s\td\n" % (x, x))
def TupleDemo(): # type: () -> None t2 = (3, 'hello') # type: Tuple[int, str] # Destructuring myint, mystr = t2 log('myint = %d', myint) log('mystr = %s', mystr) # Does this ever happen? Or do we always use destructring? #log('t2[0] = %d', t2[0]) #log('t2[1] = %s', t2[1]) x = 3 if x in (3, 4, 5): print('yes') else: print('no') p = Point(3, 4) if p.x in (3, 4, 5): print('yes') else: print('no') s = 'foo' if s in ('foo', 'bar'): print('yes') else: print('no')
def run_benchmarks(): # type: () -> None # For testing shared_ptr optimization s = 'foo' for i in xrange(100000): s = strfunc(s) log("len = %d", len(s))
def run_tests(): # type: () -> None ListDemo() log('') TupleDemo() log('') DictDemo()
def TupleDemo(): # type: () -> None t2 = (3, 'hello') # type: Tuple[int, str] # Destructuring myint, mystr = t2 log('myint = %d', myint) log('mystr = %s', mystr)
def run_benchmarks(): # type: () -> None i = 0 n = 2000000 result = 0 while i < n: result += module1.fortytwo() i = i + 1 log('result = %d', result)
def run_benchmarks(): # type: () -> None d = DirStack() for i in xrange(1000000): try: with ctx_DirStack(d, 'foo') as _: if i % 10000 == 0: raise MyError() except MyError: log('exception')
def run_tests(): # type: () -> None log("result: %s", BackslashEscape('echo *.[ch] *.?', GLOB_META_CHARS)) # 200K iterations takes ~433 ms in Python, and ~311 ms in C with -O2 (~600 ms # with -O0) The algorithm is very inefficient. There are many tiny objects # being allocated. TestNotIn()
def run_benchmarks(): # type: () -> None n = 1000000 i = 0 intlist = [] # type: List[int] strlist = [] # type: List[str] while i < n: intlist.append(i) strlist.append("foo") i += 1 log('Appended %d items to 2 lists', n)
def run_benchmarks(): # type: () -> None n = 1 # Just one iteration is enough x = 33 result = -1 i = 0 while i < n: result = fib_recursive(x) i += 1 log('fib_recursive(%d) = %d', x, result)
def DictDemo(): # type: () -> None d = {} # type: Dict[str, int] d['foo'] = 42 # TODO: implement len(Dict) and Dict::remove() and enable this if 0: log('len(d) = %d', len(d)) del d['foo'] log('len(d) = %d', len(d))
def run_tests(): # type: () -> None stdout = mylib.Stdout() out = TextOutput(stdout) out.write('foo\n') out.write('bar\n') log('Wrote %d bytes', out.num_chars) #b = Base() d = Derived() #log(b.method()) print(d.method()) print(f(d))
def run_benchmarks(): # type: () -> None n = 500000 x = 33 result = -1 i = 0 while i < n: result = fib_iter(x) i += 1 log('fib_iter(%d) = %d', x, result) log('Ran %d iterations of fib_iter', n)
def run_tests(): # type: () -> None f = mylib.BufWriter() for i in xrange(30): f.write(chr(i + 65)) contents = f.getvalue() log('Wrote %d bytes to StringIO', len(contents)) log('contents = %s ... %s', contents[:10], contents[-10:]) f2 = mylib.Stdout() f2.write('stdout\n')
def run_tests(): # type: () -> None # Use cases: # # Many in cmd_exec.py # # fd_state.Push(...) and Pop # BeginAlias, EndAlias # PushSource, PopSource (opens files) # source # eval -- no file opened, but need to change the current file # PushTemp, PopTemp for env bindings # PushErrExit, PopErrExit # loop_level in cmd_exec e = _ErrExit() e.errexit = True # initially true for do_raise in [False, True]: log('') log('-> errexit %d', e.errexit) try: DoWork(e, do_raise) except RuntimeError: log('exited with exception') log('<- errexit %d', e.errexit)
def run_tests(): # type: () -> None # Use cases: # # Many in cmd_exec.py # # fd_state.Push(...) and Pop # BeginAlias, EndAlias # PushSource, PopSource (opens files) # source # eval -- no file opened, but need to change the current file # PushTemp, PopTemp for env bindings # PushErrExit, PopErrExit # loop_level in cmd_exec d = DirStack() for do_raise in [False, True]: log('') log('-> dir stack %d', len(d.stack)) try: DoWork(d, do_raise) except MyError: log('exited with exception') log('<- dir stack %d', len(d.stack))
def run_tests(): # type: () -> None log('constant string') stderr_line('stderr_line') # Positional args log("log %d %s", 42, "LL") # Escaped %% log("[%%] %d %s", 42, "LL") log(CONST) # mycpp generates dynamic_fmt_dummy() for DYNAMIC format string. # TODO: We want to do something else. # p_die(CONST, span_id=-1) # Keyword args give location info for X_die() span_id = 123 p_die('hello %d %s', 3, "PP", span_id=span_id) # No keyword arguments e_die('hello %d', 42) e_die('hello')
def DictDemo(): # type: () -> None # regression nonempty = {'a': 'b'} # type: Dict[str, str] d = {} # type: Dict[str, int] d['foo'] = 42 # TODO: implement len(Dict) and Dict::remove() and enable this if 0: log('len(d) = %d', len(d)) del d['foo'] log('len(d) = %d', len(d))
def run_benchmarks(): # type: () -> None n = 500000 x = 33 result = -1 f = mylib.BufWriter() out = TextOutput(f) i = 0 while i < n: out.write('foo\n') i += 1 log('Ran %d iterations', n) log('Wrote %d bytes', out.num_chars)
def run_tests(): # type: () -> None # NOTE: Output is meant to be inspected if mylib.CPP: log('CPP') else: log('CPP') if mylib.PYTHON: log('PYTHON') else: log('PYTHON') if 0: log('ZERO')
def run_benchmarks(): # type: () -> None n = 10000 result = 0 i = 0 while i < n: f = mylib.BufWriter() for j in xrange(30): f.write(chr(j + 65)) result += len(f.getvalue()) i += 1 log('Ran %d iterations', n) log('result = %d', result)
def run_benchmarks(): # type: () -> None # NOTE: Raising this exposes quadratic behavior n = 50000 x = 33 result = -1 f = mylib.BufWriter() out = TextOutput(f) i = 0 while i < n: out.write('foo\n') i += 1 log('Ran %d iterations', n) log('Wrote %d bytes', out.num_chars)
def run_benchmarks(): # type: () -> None n = 500000 result = 0 i = 0 while i < n: for j in xrange(3, 10): result += j for j, c in enumerate(CATS): result += j result += len(c) i += 1 log('result = %d', result) log('Ran %d iterations of xrange/enumerate', n)
def DoWork(d, do_raise): # type: (DirStack, bool) -> None # problem # with self.mem.ctx_Call(...) # PushCall/PopCall # with self.mem.ctx_Temp(...) # PushTemp/PopCall # with self.mem.ctx_Source(...) # PushSource/PopSource # # Scope_Call # Scope_Temp # PROBLEM: WE LOST TYPE CHECKING! #with e.Context('zz') as _: with ctx_DirStack(d, 'foo') as _: log(' in context stack %d', len(d.stack)) if do_raise: Error(do_raise)
def run_tests(): # type: () -> None print('foo' + 'bar') print('foo' * 3) obj = Foo() print('foo' + obj.s) s = 'mystr' print('[%s]' % s) s = 'mystr' print('[%s, %s]' % (s, 'abc')) print('%s: 5%%-100%%' % 'abc') print('<a href="foo.html">%s</a>' % 'anchor') log("foo? %d", 'foo' in s) log("str? %d", 'str' in s)
def run_benchmarks(): # type: () -> None n = 1000000 mystr = 'abcd' mylist = ['w', 'x', 'y', 'z'] result = 0 i = 0 while i < n: # C++ has a big advantage here #result += len(mystr) #result += len(mylist) # C++ shows no real advantage here result += len(mystr[1:]) result += len(mylist[1:]) i += 1 log('result = %d', result) log('iterations = %d', n)
def TestMaybeStrEquals(): # type: () -> None a = 'foo' b = 'bar' x = '' # type: Optional[str] # TODO: Conditionally assigning x = None doesn't work. log('a == b -> %d', a == b) log('a != b -> %d', a != b) log('a == x -> %d', a == x) log('a != x -> %d', a != x)