Initial commit
Test (in pplus-core) / in-core (push) Failing after 28m44s

This commit is contained in:
alexm committed 2026-11-12 22:23:48 -05:00
commit 568474d52f
1591 files changed
+528213

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
# probe: a struct method that raises, caught by the caller
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: withdrew 30 left 120
# expect-stdout: refused 200 : insufficient funds
# expect-stdout: withdrew 120 left 0
# expect-stdout: ada 0
struct Account(Movable):
var owner: String
var balance: Int
def __init__(out self, owner: String, balance: Int):
self.owner = owner
self.balance = balance
def withdraw(mut self, amount: Int) raises:
if amount > self.balance:
raise Error("insufficient funds")
self.balance -= amount
def deposit(mut self, amount: Int):
self.balance += amount
def main():
var acct = Account("ada", 100)
acct.deposit(50)
for amount in [30, 200, 120]:
try:
acct.withdraw(amount)
print("withdrew", amount, "left", acct.balance)
except e:
print("refused", amount, ":", e)
print(acct.owner, acct.balance)
+26
View File
@@ -0,0 +1,26 @@
# probe: binary search over a sorted list, found and not found
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 7 3
# expect-stdout: 1 0
# expect-stdout: 13 6
# expect-stdout: 4 -1
# expect-stdout: 0 -1
# expect-stdout: 14 -1
def search(xs: List[Int], target: Int) -> Int:
var lo = 0
var hi = len(xs) - 1
while lo <= hi:
var mid = (lo + hi) // 2
if xs[mid] == target:
return mid
elif xs[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1
def main():
var xs: List[Int] = [1, 3, 5, 7, 9, 11, 13]
for t in [7, 1, 13, 4, 0, 14]:
print(t, search(xs, t))
+20
View File
@@ -0,0 +1,20 @@
# probe: var shadowing inside if and loop blocks
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: inner 11
# expect-stdout: outer 1
# expect-stdout: loop 0
# expect-stdout: loop 100
# expect-stdout: loop 200
# expect-stdout: total 0
def main():
var x = 1
if x == 1:
var x = 10
x += 1
print("inner", x)
print("outer", x)
var total = 0
for i in range(3):
var total = i * 100
print("loop", total)
print("total", total)
+21
View File
@@ -0,0 +1,21 @@
# probe: sorting a List[Int] in place with nested loops and a swap
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: [1, 2, 5, 5, 6, 9]
# expect-stdout: 0
def bubble_sort(mut xs: List[Int]):
var n = len(xs)
for i in range(n):
for j in range(n - 1 - i):
if xs[j] > xs[j + 1]:
var tmp = xs[j]
xs[j] = xs[j + 1]
xs[j + 1] = tmp
def main():
var xs: List[Int] = [5, 2, 9, 1, 5, 6]
bubble_sort(xs)
print(xs)
var empty = List[Int]()
bubble_sort(empty)
print(len(empty))
+23
View File
@@ -0,0 +1,23 @@
# probe: shifting letters through an alphabet string with find()
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: khoor zruog
# expect-stdout: hello world
# expect-stdout: bcd
def shift(s: String, k: Int) -> String:
var alphabet = String("abcdefghijklmnopqrstuvwxyz")
var out = String("")
for i in range(s.count_codepoints()):
var c = String(s[codepoint=i])
var at = alphabet.find(c)
if at < 0:
out += c
else:
out += String(alphabet[codepoint=(at + k) % 26])
return out
def main():
var secret = shift("hello world", 3)
print(secret)
print(shift(secret, 23))
print(shift("xyz", 4))
+26
View File
@@ -0,0 +1,26 @@
# probe: closures capturing mut state and var snapshots
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: count 4
# expect-stdout: count 9
# expect-stdout: 101 0
def main():
var count = 0
var step = 2
def tick() {mut count, imm step}:
count += step
tick()
tick()
print("count", count)
step = 5
tick()
print("count", count)
var base = 100
def from_base(x: Int) {var base} -> Int:
return base + x
base = 0
print(from_base(1), base)
+26
View File
@@ -0,0 +1,26 @@
# probe: while loops, a step count, and tracking a maximum
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: longest below 30: 27 with 111 steps
# expect-stdout: 111
def steps(n0: Int) -> Int:
var n = n0
var count = 0
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
count += 1
return count
def main():
var best = 0
var best_n = 1
for n in range(1, 30):
var s = steps(n)
if s > best:
best = s
best_n = n
print("longest below 30:", best_n, "with", best, "steps")
print(steps(27))
+17
View File
@@ -0,0 +1,17 @@
# probe: comptime constants and expressions used at run time
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 8 4 32
# expect-stdout: #.#.#.#.
# expect-stdout: hi!
comptime WIDTH = 8
comptime HEIGHT = WIDTH // 2
comptime GREETING = "hi"
def main():
print(WIDTH, HEIGHT, WIDTH * HEIGHT)
var row = String("")
for x in range(WIDTH):
row += "#" if x % 2 == 0 else "."
print(row)
print(GREETING + "!")
+32
View File
@@ -0,0 +1,32 @@
# probe: with over a struct with __enter__ and __exit__, normal and failing
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: begin load
# expect-stdout: loading
# expect-stdout: end load
# expect-stdout: begin parse
# expect-stdout: inside parse
# expect-stdout: end parse after error: bad token
# expect-stdout: done
@fieldwise_init
struct Section(Copyable, Movable):
var name: String
def __enter__(self) -> Self:
print("begin", self.name)
return self.copy()
def __exit__(self):
print("end", self.name)
def __exit__(self, err: Error) -> Bool:
print("end", self.name, "after error:", err)
return True
def main() raises:
with Section("load"):
print("loading")
with Section("parse") as s:
print("inside", s.name)
raise Error("bad token")
print("done")
+19
View File
@@ -0,0 +1,19 @@
# probe: a Dict as an inventory: update, pop, get with a default, keys
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: sold out: 2
# expect-stdout: kiwis: 0
# expect-stdout: plums: 9
# expect-stdout: apples 8
# expect-stdout: plums 9
def main() raises:
var stock = Dict[String, Int]()
stock["apples"] = 5
stock["pears"] = 2
stock["plums"] = 9
stock["apples"] += 3
var gone = stock.pop("pears")
print("sold out:", gone)
print("kiwis:", stock.get("kiwis").or_else(0))
print("plums:", stock.get("plums").or_else(0))
for k in stock.keys():
print(k, stock[k])
+29
View File
@@ -0,0 +1,29 @@
# probe: raising through calls, catching, and finally
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: checked 42
# expect-stdout: age 42
# expect-stdout: checked -3
# expect-stdout: bad: negative age
# expect-stdout: checked x
# expect-stdout: bad: String is not convertible to integer with base 10: 'x'
def parse_age(s: String) raises -> Int:
var n = atol(s)
if n < 0:
raise Error("negative age")
return n
def describe(s: String) -> String:
try:
var n = parse_age(s)
return "age " + String(n)
except e:
return "bad: " + String(e)
finally:
print("checked", s)
def main():
print(describe("42"))
print(describe("-3"))
print(describe("x"))
+13
View File
@@ -0,0 +1,13 @@
# probe: literal-only arithmetic is exact; with a variable it runs in Float64
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 0.3 0.3
# expect-stdout: 0.30000000000000004 0.30000000000000004
# expect-stdout: 2.0 1.0
# expect-stdout: 1.0
def main():
var one = 1.0
print(0.1 + 0.2, 0.1 * 3.0)
var tenth = 0.1
print(tenth + 0.2, tenth * 3.0)
print(2.0 / 3.0 * 3.0, one / 3.0 * 3.0)
print(1e16 + 1.0 - 1e16)
+25
View File
@@ -0,0 +1,25 @@
# probe: recursion with a Dict memo passed as mut
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 0 0
# expect-stdout: 5 5
# expect-stdout: 10 55
# expect-stdout: 15 610
# expect-stdout: 20 6765
# expect-stdout: 25 75025
# expect-stdout: 30 832040
# expect-stdout: memo size: 29
def fib(n: Int, mut memo: Dict[Int, Int]) raises -> Int:
if n < 2:
return n
if n in memo:
return memo[n]
var v = fib(n - 1, memo) + fib(n - 2, memo)
memo[n] = v
return v
def main() raises:
var memo = Dict[Int, Int]()
for n in range(0, 31, 5):
print(n, fib(n, memo))
print("memo size:", len(memo))
+44
View File
@@ -0,0 +1,44 @@
# probe: a normalized fraction type with arithmetic and printing
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 1/2 1/3
# expect-stdout: 5/6
# expect-stdout: 1/6
# expect-stdout: -1/3
def gcd(a: Int, b: Int) -> Int:
var x = a if a >= 0 else -a
var y = b if b >= 0 else -b
while y != 0:
var t = x % y
x = y
y = t
return x
@fieldwise_init
struct Frac(Copyable, Movable, Writable):
var num: Int
var den: Int
@staticmethod
def make(n: Int, d: Int) -> Frac:
var g = gcd(n, d)
var sign = -1 if d < 0 else 1
return Frac(sign * n // g, sign * d // g)
def __add__(self, other: Self) -> Self:
return Frac.make(self.num * other.den + other.num * self.den, self.den * other.den)
def __mul__(self, other: Self) -> Self:
return Frac.make(self.num * other.num, self.den * other.den)
def write_to(self, mut writer: Some[Writer]):
writer.write(self.num, "/", self.den)
def main():
var half = Frac.make(1, 2)
var third = Frac.make(2, 6)
print(half, third)
print(half + third)
print(half * third)
print(Frac.make(3, -9))
+19
View File
@@ -0,0 +1,19 @@
# probe: a function as a compile-time parameter, f: def(Int) thin -> Int in brackets (the manual's inplace_transform), with a named function and a lambda that captures nothing
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: [-3, -1, -4]
# expect-stdout: [9, 1, 16]
def map_in_place[f: def(Int) thin -> Int](mut xs: List[Int]):
for i in range(len(xs)):
xs[i] = f(xs[i])
def negate(x: Int) -> Int:
return -x
def main():
var xs: List[Int] = [3, 1, 4]
map_in_place[negate](xs)
print(xs)
map_in_place[lambda (x: Int) -> Int: x * x](xs)
print(xs)
+22
View File
@@ -0,0 +1,22 @@
# probe: functions as values the way the manual passes them: a type parameter F: def(Int) -> Int with f: F, for a named function, a lambda, and a capturing lambda
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: [1, 4, 9, 16]
# expect-stdout: [10, 20, 30, 40]
# expect-stdout: [101, 102, 103, 104]
def apply_all[F: def(Int) -> Int, //](f: F, xs: List[Int]) -> List[Int]:
var out = List[Int]()
for x in xs:
out.append(f(x))
return out^
def square(x: Int) -> Int:
return x * x
def main():
var xs: List[Int] = [1, 2, 3, 4]
print(apply_all(square, xs))
print(apply_all(lambda (x: Int) -> Int: x * 10, xs))
var offset = 100
print(apply_all(lambda (x: Int) -> Int: x + offset, xs))
+22
View File
@@ -0,0 +1,22 @@
# probe: recursion, // and % on Int
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 6 1 9
# expect-stdout: 12 42
# expect-stdout: lcm 1..10: 2520
def gcd(a: Int, b: Int) -> Int:
if b == 0:
return a
return gcd(b, a % b)
def lcm(a: Int, b: Int) -> Int:
return a // gcd(a, b) * b
def main():
print(gcd(48, 18), gcd(17, 5), gcd(0, 9))
print(lcm(4, 6), lcm(21, 6))
var total = 1
for k in range(1, 11):
total = lcm(total, k)
print("lcm 1..10:", total)
+24
View File
@@ -0,0 +1,24 @@
# probe: a list of structs, averages, and round()
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: ada 84.0 84.0
# expect-stdout: bo 77.0 77.0
@fieldwise_init
struct Student(Copyable, Movable):
var name: String
var scores: List[Int]
def mean(self) -> Float64:
var total = 0
for s in self.scores:
total += s
return Float64(total) / Float64(len(self.scores))
def main():
var a: List[Int] = [90, 85, 77]
var b: List[Int] = [60, 72, 81, 95]
var people = List[Student]()
people.append(Student("ada", a^))
people.append(Student("bo", b^))
for p in people:
print(p.name, p.mean(), round(p.mean()))
+22
View File
@@ -0,0 +1,22 @@
# probe: sized integers wrapping, and // and % with negative operands
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 125
# expect-stdout: -126
# expect-stdout: -121
# expect-stdout: 254
# expect-stdout: 7 3 2 1 2
# expect-stdout: -7 3 -3 2 -2
# expect-stdout: 7 -3 -3 -2 -2
# expect-stdout: -7 -3 2 -1 2
def main():
var a: Int8 = 120
for _ in range(3):
a += 5
print(a)
var u: UInt8 = 3
u -= 5
print(u)
for pair in [(7, 3), (-7, 3), (7, -3), (-7, -3)]:
var x = pair[0]
var y = pair[1]
print(x, y, x // y, x % y, x / y)
+14
View File
@@ -0,0 +1,14 @@
# probe: the same with a capturing lambda: the compiler names its type def(x: Int) -> Int, with no thin
# expect: compile-error
# note: kept as a checker test; the code below is exactly what godbolt rejected
def apply_all(xs: List[Int], f: def(Int) -> Int) -> List[Int]:
var out = List[Int]()
for x in xs:
out.append(f(x))
return out^
def main():
var xs: List[Int] = [1, 2, 3, 4]
var offset = 10
print(apply_all(xs, lambda (x: Int) -> Int: x + offset))
+19
View File
@@ -0,0 +1,19 @@
# probe: a function passed as a plain def(Int) -> Int argument is a compile error in 1.1.0 (that type is a type, not a value's): the compiler's words for a named function and a lambda
# expect: compile-error
# note: kept as a checker test; the code below is exactly what godbolt rejected
# note: kept as a checker test; the code below is exactly what godbolt rejected
def apply_all(xs: List[Int], f: def(Int) -> Int) -> List[Int]:
var out = List[Int]()
for x in xs:
out.append(f(x))
return out^
def square(x: Int) -> Int:
return x * x
def main():
var xs: List[Int] = [1, 2, 3, 4]
print(apply_all(xs, square))
print(apply_all(xs, lambda (x: Int) -> Int: x * 10))
+36
View File
@@ -0,0 +1,36 @@
# probe: values with a __deinit__ inside a loop and a branch
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: make keep
# expect-stdout: make loop0
# expect-stdout: drop loop0
# expect-stdout: pass 0
# expect-stdout: make loop1
# expect-stdout: odd pass uses loop1
# expect-stdout: drop loop1
# expect-stdout: make loop2
# expect-stdout: drop loop2
# expect-stdout: pass 2
# expect-stdout: after loop keep
# expect-stdout: drop keep
# expect-stdout: end
struct Tag(Movable):
var name: String
def __init__(out self, name: String):
self.name = name
print("make", name)
def __deinit__(deinit self):
print("drop", self.name)
def main():
var keep = Tag("keep")
for i in range(3):
var t = Tag("loop" + String(i))
if i == 1:
print("odd pass uses", t.name)
else:
print("pass", i)
print("after loop", keep.name)
print("end")
+38
View File
@@ -0,0 +1,38 @@
# probe: copy and move constructors through var arguments and transfers
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: new a
# expect-stdout: copy a
# expect-stdout: consumed a+
# expect-stdout: free a+
# expect-stdout: move a
# expect-stdout: holding a
# expect-stdout: free a
struct Box(Copyable, Movable):
var label: String
def __init__(out self, label: String):
self.label = label
print("new", label)
def __init__(out self, *, copy: Self):
self.label = copy.label + "+"
print("copy", copy.label)
def __init__(out self, *, deinit move: Self):
self.label = move.label^
print("move", self.label)
def __deinit__(deinit self):
print("free", self.label)
def consume(var b: Box):
print("consumed", b.label)
def main():
var a = Box("a")
var b = a.copy()
consume(b^)
var c = a^
print("holding", c.label)
+26
View File
@@ -0,0 +1,26 @@
# probe: List[List[Int]] built row by row, multiplied and printed
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: [7, 10]
# expect-stdout: [15, 22]
def matmul(a: List[List[Int]], b: List[List[Int]]) -> List[List[Int]]:
var out = List[List[Int]]()
for i in range(len(a)):
var row = List[Int]()
for j in range(len(b[0])):
var s = 0
for k in range(len(b)):
s += a[i][k] * b[k][j]
row.append(s)
out.append(row^)
return out^
def main():
var a = List[List[Int]]()
var r1: List[Int] = [1, 2]
var r2: List[Int] = [3, 4]
a.append(r1^)
a.append(r2^)
var c = matmul(a, a)
for row in c:
print(row)
+22
View File
@@ -0,0 +1,22 @@
# probe: a function returning Optional, tested and unwrapped
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: ada at 0
# expect-stdout: cy at 2
# expect-stdout: dee missing
# expect-stdout: 0 None
def index_of(xs: List[String], x: String) -> Optional[Int]:
for i in range(len(xs)):
if xs[i] == x:
return i
return None
def main():
var names: List[String] = ["ada", "bo", "cy"]
for q in ["ada", "cy", "dee"]:
var r = index_of(names, q)
if r:
print(q, "at", r.value())
else:
print(q, "missing")
print(index_of(names, "ada"), index_of(names, "zed"))
+21
View File
@@ -0,0 +1,21 @@
# probe: overloaded functions picked by count and by type
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: an Int 3
# expect-stdout: a String hi
# expect-stdout: a pair 1,2
def describe(x: Int) -> String:
return "an Int " + String(x)
def describe(x: String) -> String:
return "a String " + x
def describe(x: Int, y: Int) -> String:
return "a pair " + String(x) + "," + String(y)
def main():
print(describe(3))
print(describe("hi"))
print(describe(1, 2))
+27
View File
@@ -0,0 +1,27 @@
# probe: mut arguments changing a caller's list and struct
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 7
# expect-stdout: [0, 10, 20, 30, 0, 10] 6
@fieldwise_init
struct Counter(Copyable, Movable):
var hits: Int
def bump(mut c: Counter, by: Int):
c.hits += by
def fill(mut xs: List[Int], n: Int):
for i in range(n):
xs.append(i * 10)
def main():
var c = Counter(0)
bump(c, 3)
bump(c, 4)
print(c.hits)
var xs = List[Int]()
fill(xs, 4)
fill(xs, 2)
print(xs, len(xs))
+20
View File
@@ -0,0 +1,20 @@
# probe: codepoint indexing, lower(), and early returns
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: Level True
# expect-stdout: rotor True
# expect-stdout: Mojo False
# expect-stdout: a True
# expect-stdout: True
# expect-stdout: abBA True
def is_palindrome(s: String) -> Bool:
var t = s.lower()
var n = t.count_codepoints()
for i in range(n // 2):
if String(t[codepoint=i]) != String(t[codepoint=n - 1 - i]):
return False
return True
def main():
for w in ["Level", "rotor", "Mojo", "a", "", "abBA"]:
print(w, is_palindrome(w))
+21
View File
@@ -0,0 +1,21 @@
# probe: the sieve of Eratosthenes with a List[Bool]
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: 15 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
def primes_below(n: Int) -> List[Int]:
var is_prime = List[Bool]()
for _ in range(n):
is_prime.append(True)
var out = List[Int]()
for i in range(2, n):
if is_prime[i]:
out.append(i)
var j = i * i
while j < n:
is_prime[j] = False
j += i
return out^
def main():
var ps = primes_below(50)
print(len(ps), ps)
+26
View File
@@ -0,0 +1,26 @@
# probe: a FIFO queue with pop(0) and a fieldwise struct
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: first
# expect-stdout: second
# expect-stdout: third
# expect-stdout: fourth
@fieldwise_init
struct Queue(Movable):
var items: List[String]
def enqueue(mut self, x: String):
self.items.append(x)
def dequeue(mut self) -> String:
return self.items.pop(0)
def main():
var q = Queue(List[String]())
q.enqueue("first")
q.enqueue("second")
q.enqueue("third")
print(q.dequeue())
q.enqueue("fourth")
while len(q.items) > 0:
print(q.dequeue())
+23
View File
@@ -0,0 +1,23 @@
# probe: run-length encoding a string
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: aaabccdddd -> 3a1b2c4d
# expect-stdout: abc -> 1a1b1c
# expect-stdout: ->
# expect-stdout: zzzzzzzzzz -> 10z
def encode(s: String) -> String:
var out = String("")
var n = s.count_codepoints()
var i = 0
while i < n:
var c = String(s[codepoint=i])
var run = 1
while i + run < n and String(s[codepoint=i + run]) == c:
run += 1
out += String(run) + c
i += run
return out
def main():
for s in ["aaabccdddd", "abc", "", "zzzzzzzzzz"]:
print(s, "->", encode(s))
+22
View File
@@ -0,0 +1,22 @@
# probe: a Set: add, membership, size, and iteration order
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: unique: 4
# expect-stdout: 4
# expect-stdout: 1
# expect-stdout: 9
# expect-stdout: 7
# expect-stdout: dups: [4, 1, 9]
from std.collections import Set
def main():
var seen = Set[Int]()
var dups = List[Int]()
for x in [4, 1, 4, 9, 1, 7, 9]:
if x in seen:
dups.append(x)
seen.add(x)
print("unique:", len(seen))
for x in seen:
print(x)
print("dups:", dups)
+16
View File
@@ -0,0 +1,16 @@
# probe: SIMD arithmetic, broadcasting, and reductions
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: [1.5, 2.0, -3.0, 4.25]
# expect-stdout: [3.0, 4.0, -6.0, 8.5]
# expect-stdout: [2.5, 3.0, -2.0, 5.25]
# expect-stdout: 4.75 4.25 -3.0
# expect-stdout: [6, 2, 8, 2] 12
def main():
var v = SIMD[DType.float32, 4](1.5, 2.0, -3.0, 4.25)
var w = SIMD[DType.float32, 4](2.0)
print(v)
print(v * w)
print(v + 1.0)
print(v.reduce_add(), v.reduce_max(), v.reduce_min())
var k = SIMD[DType.int32, 4](3, 1, 4, 1)
print(k * 2, k.reduce_mul())
+33
View File
@@ -0,0 +1,33 @@
# probe: a struct wrapping a List with mut methods and an Optional result
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: size 3
# expect-stdout: 15 8 1
# expect-stdout: None
struct Stack(Movable):
var items: List[Int]
def __init__(out self):
self.items = List[Int]()
def push(mut self, x: Int):
self.items.append(x)
def pop(mut self) -> Optional[Int]:
if len(self.items) == 0:
return None
return self.items.pop()
def size(self) -> Int:
return len(self.items)
def main():
var s = Stack()
for x in [4, 8, 15]:
s.push(x)
print("size", s.size())
var a = s.pop()
var b = s.pop()
print(a, b, s.size())
_ = s.pop()
print(s.pop())
+22
View File
@@ -0,0 +1,22 @@
# probe: building strings with +, String(), join, and String(t"...") from a t-string
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: item1, item2, item3
# expect-stdout: ada: 1 | bo: 3
# expect-stdout: 0-1-4-9-16
def label(name: String, score: Int) -> String:
return String(t"{name}: {score}")
def main():
var parts = List[String]()
for i in range(1, 4):
parts.append("item" + String(i))
var joined = String(", ").join(parts)
print(joined)
print(label("ada", 1), "|", label("bo", 3))
var line = String("")
for i in range(5):
line += String(i * i)
if i < 4:
line += "-"
print(line)
+22
View File
@@ -0,0 +1,22 @@
# probe: a t-string is a TString of its values' types, not a String: returning one from a function declared -> String is a compile error
# expect: compile-error
# note: kept as a checker test; the code below is exactly what godbolt rejected
# note: kept as a checker test; the code below is exactly what godbolt rejected
# note: kept as a checker test; the code below is exactly what godbolt rejected
def label(name: String, score: Int) -> String:
return t"{name}: {score}"
def main():
var parts = List[String]()
for i in range(1, 4):
parts.append("item" + String(i))
var joined = String(", ").join(parts)
print(joined)
print(label("ada", 1), "|", label("bo", 3))
var line = String("")
for i in range(5):
line += String(i * i)
if i < 4:
line += "-"
print(line)
+25
View File
@@ -0,0 +1,25 @@
# probe: Float64 arithmetic and how print writes floats
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: -40.0 C is -40.0 F
# expect-stdout: 0.0 C is 32.0 F
# expect-stdout: 36.6 C is 97.88000000000001 F
# expect-stdout: 100.0 C is 212.0 F
# expect-stdout: 37.0
# expect-stdout: mean 24.15
def c_to_f(c: Float64) -> Float64:
return c * 9.0 / 5.0 + 32.0
def f_to_c(f: Float64) -> Float64:
return (f - 32.0) * 5.0 / 9.0
def main():
var readings: List[Float64] = [-40.0, 0.0, 36.6, 100.0]
for c in readings:
print(c, "C is", c_to_f(c), "F")
print(f_to_c(98.6))
var total = 0.0
for c in readings:
total += c
print("mean", total / 4.0)
+35
View File
@@ -0,0 +1,35 @@
# probe: a trait with a default method, used by two structs
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: a shape of area 7.0
# expect-stdout: a square of side 4.0
trait Shape:
def area(self) -> Float64:
...
def describe(self) -> String:
return "a shape of area " + String(self.area())
@fieldwise_init
struct Rect(Shape, Copyable, Movable):
var w: Float64
var h: Float64
def area(self) -> Float64:
return self.w * self.h
@fieldwise_init
struct Square(Shape, Copyable, Movable):
var side: Float64
def area(self) -> Float64:
return self.side * self.side
def describe(self) -> String:
return "a square of side " + String(self.side)
def main():
print(Rect(2.0, 3.5).describe())
print(Square(4.0).describe())
+39
View File
@@ -0,0 +1,39 @@
# probe: operator overloading and write_to on a small struct
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: Vec2(1.5, 0.5)
# expect-stdout: Vec2(0.5, 3.5)
# expect-stdout: Vec2(3.0, 6.0)
# expect-stdout: -2.5
# expect-stdout: True False
@fieldwise_init
struct Vec2(Copyable, Movable, Writable):
var x: Float64
var y: Float64
def __add__(self, other: Self) -> Self:
return Vec2(self.x + other.x, self.y + other.y)
def __sub__(self, other: Self) -> Self:
return Vec2(self.x - other.x, self.y - other.y)
def __mul__(self, k: Float64) -> Self:
return Vec2(self.x * k, self.y * k)
def __eq__(self, other: Self) -> Bool:
return self.x == other.x and self.y == other.y
def dot(self, other: Self) -> Float64:
return self.x * other.x + self.y * other.y
def write_to(self, mut writer: Some[Writer]):
writer.write("Vec2(", self.x, ", ", self.y, ")")
def main():
var a = Vec2(1.0, 2.0)
var b = Vec2(0.5, -1.5)
print(a + b)
print(a - b)
print(a * 3.0)
print(a.dot(b))
print(a == Vec2(1.0, 2.0), a == b)
+20
View File
@@ -0,0 +1,20 @@
# probe: splitting text and counting words in a Dict, in insertion order
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
# expect-stdout: the 3
# expect-stdout: cat 1
# expect-stdout: and 2
# expect-stdout: hat 1
# expect-stdout: bat 1
# expect-stdout: distinct: 5
def main() raises:
var text = String("the cat and the hat and the bat")
var counts = Dict[String, Int]()
for w in text.split(" "):
var word = String(w)
if word in counts:
counts[word] += 1
else:
counts[word] = 1
for e in counts.items():
print(e.key, e.value)
print("distinct:", len(counts))