Skip to content

Commit

Permalink
[mycpp/examples] Test typical UP_val casting pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy C committed Mar 12, 2024
1 parent acb9724 commit 091cdf9
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions mycpp/examples/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def GetRaw(self):
return f.getvalue(), self.num_chars


def Test1():
def TestCastBufWriter():
# type: () -> None
"""For debugging a problem with StackRoots generation"""

Expand Down Expand Up @@ -81,7 +81,23 @@ def tag(self):
return 2


def Test2():
def TestSwitchDowncast(val):
# type: (value_t) -> None

# typical UP_ pattern
UP_val = val
with tagswitch(val) as case:
if case(1):
val = cast(value__Int, UP_val)
print('Int = %d' % val.i)
elif case(2):
val = cast(value__Eggex, UP_val)
print('Eggex = %r' % val.ere)
else:
print('other')


def TestCastInSwitch():
# type: () -> None

# Inspired by HasAffix()
Expand All @@ -106,8 +122,10 @@ def Test2():
def run_tests():
# type: () -> None

Test1()
#Test2()
TestCastBufWriter()
TestSwitchDowncast(value__Eggex('[0-9]'))
TestSwitchDowncast(value__Int(42))
#TestCastInSwitch()


def run_benchmarks():
Expand Down

0 comments on commit 091cdf9

Please sign in to comment.