import lib.assert.Assert class TestArrays {} class method do: block block value: [1, 2, 3, 4]. block value: [123, 10293810293, -123, 123]. block end -- Generates signed integers small enough to add without overflowing. class SmallIntegers {} class method do: block -- First around zero block value: -1. block value: 0. block value: 1. -- Then random numbers let rng = Random new. 100 times: { block value: (rng integer / 2) } end class ShortStringSeqs {} class method do: block -- FIXME: actual random generation, please block value: []. block value: [""]. block value: ["", ""]. block value: ["", "", ""]. block value: ["123", "", ""]. block value: ["", "123", ""]. block value: ["", "", "123"] end class Floats {} class method do: block block value: -0.0. block value: 0.0. block value: 1.0. block value: -1.0. block value: 0.1. block value: -0.1. let rng = Random new. 100 times: { block value: rng float } end class Box { value } method *** other self value * other method * other self value * other method + other self value + other method prefix- - (self value) end class Main { assert } class method run: cmd in: system let instance = self assert: (Assert reportingTo: system output). instance test. system exit: instance assert exitCode method test self testByteArrays. self testFloats. self testIntegers. self testStringOutput. self testPrecedence. self testPrefix. self testRecord. self testDictionary. self testRaise. method testRaise assert true: { ({ raise "BOOM" } onError: { |e c| e }) == "BOOM" } testing: "raise with string argument" method testDictionary assert true: { let d = { 1 + 1 + 40 -> "the" append: "Answer" }. (d at: 42) == "theAnswer" } testing: "dictionary syntax and accessor". assert true: { let d = Dictionary new. d put: "foo" at: "bar". (d at: "bar") == "foo" } testing: "dictionary new and put:at:" method testRecord assert true: { let r = {x: -10, y: 52}. r x + r y == 42 } testing: "record creation and accessors". method testPrefix assert forAll: (1 to: 10) that: { |n| let b = Box value: n. -n == -b } testing: "custom prefix method" method testPrecedence assert forAll: (2 to: 10) that: { |n| let a1 = n * n + n. let a2 = (n * n) + n. let b1 = (Box value: n) *** n + n. let b2 = n * (n + n). (a1 == a2) and: (b1 == b2) and: (a1 == b1) not } testing: "operator precedence" method testByteArrays assert forAll: (1 to: 10) that: { |n| let bytes = ByteArray new: n. let p = n / 2 + 1. let old = bytes at: p. bytes put: n at: p. let new = bytes at: p. (bytes size == n) and: (old == 0) and: (new == n) and: (n > 0) } testing: "ByteArray creation and access" method testFloats assert forAll: Floats that: { |x| x + x == x * 2.0 } testing: "float addition" method testIntegers assert forAll: SmallIntegers that: { |x| x + x == x * 2 } testing: "integer self addition". assert forAll: SmallIntegers that: { |x| x - x == 0 } testing: "integer self substraction". assert forAll: SmallIntegers that: { |x| x + 1 == 1 + x } testing: "integer addition commutates" method testStringOutput assert forAll: ShortStringSeqs that: { |seq| let out = StringOutput new. let cmp = "". seq do: { |s| cmp = cmp append: s. out print: s }. cmp == out content } testing: "string output" end