Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays as sequences #1741

Merged
merged 3 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions packages/buffered/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ class iso _TestReader is UnitTest
let b = Reader

b.append(recover [as U8:
0x42,
0xDE, 0xAD,
0xAD, 0xDE,
0xDE, 0xAD, 0xBE, 0xEF,
0xEF, 0xBE, 0xAD, 0xDE,
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED, 0xFA, 0xCE,
0xCE, 0xFA, 0xED, 0xFE, 0xEF, 0xBE, 0xAD, 0xDE,
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED, 0xFA, 0xCE,
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED, 0xFA, 0xCE,
0xCE, 0xFA, 0xED, 0xFE, 0xEF, 0xBE, 0xAD, 0xDE,
0xCE, 0xFA, 0xED, 0xFE, 0xEF, 0xBE, 0xAD, 0xDE
0x42
0xDE; 0xAD
0xAD; 0xDE
0xDE; 0xAD; 0xBE; 0xEF
0xEF; 0xBE; 0xAD; 0xDE
0xDE; 0xAD; 0xBE; 0xEF; 0xFE; 0xED; 0xFA; 0xCE
0xCE; 0xFA; 0xED; 0xFE; 0xEF; 0xBE; 0xAD; 0xDE
0xDE; 0xAD; 0xBE; 0xEF; 0xFE; 0xED; 0xFA; 0xCE
0xDE; 0xAD; 0xBE; 0xEF; 0xFE; 0xED; 0xFA; 0xCE
0xCE; 0xFA; 0xED; 0xFE; 0xEF; 0xBE; 0xAD; 0xDE
0xCE; 0xFA; 0xED; 0xFE; 0xEF; 0xBE; 0xAD; 0xDE
] end)

b.append(recover [as U8: 'h', 'i'] end)
b.append(recover [as U8: '\n', 't', 'h', 'e'] end)
b.append(recover [as U8: 'r', 'e', '\r', '\n'] end)
b.append(recover [as U8: 'h'; 'i'] end)
b.append(recover [as U8: '\n'; 't'; 'h'; 'e'] end)
b.append(recover [as U8: 'r'; 'e'; '\r'; '\n'] end)

// These expectations peek into the buffer without consuming bytes.
h.assert_eq[U8](b.peek_u8(), 0x42)
Expand Down Expand Up @@ -64,24 +64,25 @@ class iso _TestReader is UnitTest
h.assert_eq[String](b.line(), "hi")
h.assert_eq[String](b.line(), "there")

b.append(recover [as U8: 'h', 'i'] end)
b.append(recover [as U8: 'h'; 'i'] end)

try
b.line()
h.fail("shouldn't have a line")
end

b.append(recover [as U8: '!', '\n'] end)
b.append(recover [as U8: '!'; '\n'] end)
h.assert_eq[String](b.line(), "hi!")

b.append(recover [as U8: 's', 't', 'r', '1'] end)
b.append(recover [as U8: 's'; 't'; 'r'; '1'] end)
try
b.read_until(0)
h.fail("should fail reading until 0")
end
b.append(recover [as U8: 0] end)
b.append(recover [as U8: 'f', 'i', 'e', 'l', 'd', '1', ';',
'f', 'i', 'e', 'l', 'd', '2', ';', ';'] end)
b.append(recover [as U8:
'f'; 'i'; 'e'; 'l'; 'd'; '1'; ';'
'f'; 'i'; 'e'; 'l'; 'd'; '2'; ';'; ';'] end)
h.assert_eq[String](String.from_array(b.read_until(0)), "str1")
h.assert_eq[String](String.from_array(b.read_until(';')), "field1")
h.assert_eq[String](String.from_array(b.read_until(';')), "field2")
Expand Down Expand Up @@ -111,9 +112,11 @@ class iso _TestWriter is UnitTest
.>u128_be(0xDEADBEEFFEEDFACEDEADBEEFFEEDFACE)
.>u128_le(0xDEADBEEFFEEDFACEDEADBEEFFEEDFACE)

wb.write(recover [as U8: 'h', 'i'] end)
wb.writev(recover [as Array[U8]: [as U8: '\n', 't', 'h', 'e'],
[as U8: 'r', 'e', '\r', '\n']] end)
wb.write(recover [as U8: 'h'; 'i'] end)
wb.writev(recover
[as Array[U8]:
[as U8: '\n'; 't'; 'h'; 'e']
[as U8: 'r'; 'e'; '\r'; '\n']] end)

for bs in wb.done().values() do
try
Expand Down Expand Up @@ -149,12 +152,12 @@ class iso _TestWriter is UnitTest
h.assert_eq[String](b.line(), "hi")
h.assert_eq[String](b.line(), "there")

b.append(recover [as U8: 'h', 'i'] end)
b.append(recover [as U8: 'h'; 'i'] end)

try
b.line()
h.fail("shouldn't have a line")
end

b.append(recover [as U8: '!', '\n'] end)
b.append(recover [as U8: '!'; '\n'] end)
h.assert_eq[String](b.line(), "hi!")
90 changes: 45 additions & 45 deletions packages/builtin_test/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class iso _TestStringRunes is UnitTest

fun apply(h: TestHelper) ? =>
let s = "\u16ddx\ufb04"
let expect = [as U32: 0x16dd, 'x', 0xfb04]
let expect = [as U32: 0x16dd; 'x'; 0xfb04]
let result = Array[U32]

for c in s.runes() do
Expand Down Expand Up @@ -503,10 +503,10 @@ class iso _TestStringIsNullTerminated is UnitTest
h.assert_false("0123456".trim(2, 4).is_null_terminated())

h.assert_true(String.from_iso_array(recover
['a', 'b', 'c']
['a'; 'b'; 'c']
end).is_null_terminated())
h.assert_false(String.from_iso_array(recover
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] // power of two sized array
['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'] // power of two sized array
end).is_null_terminated())


Expand Down Expand Up @@ -698,9 +698,9 @@ class iso _TestStringJoin is UnitTest

fun apply(h: TestHelper) =>
h.assert_eq[String]("_".join(["zomg"]), "zomg")
h.assert_eq[String]("_".join(["hi", "there"]), "hi_there")
h.assert_eq[String](" ".join(["1", "", "2", ""]), "1 2 ")
h.assert_eq[String](" ".join([as Stringable: U32(1), U32(4)]), "1 4")
h.assert_eq[String]("_".join(["hi"; "there"]), "hi_there")
h.assert_eq[String](" ".join(["1"; ""; "2"; ""]), "1 2 ")
h.assert_eq[String](" ".join([as Stringable: U32(1); U32(4)]), "1 4")
h.assert_eq[String](" ".join(Array[String]), "")


Expand Down Expand Up @@ -931,11 +931,11 @@ class iso _TestStringFromArray is UnitTest
fun name(): String => "builtin/String.from_array"

fun apply(h: TestHelper) =>
let s_null = String.from_array(recover ['f', 'o', 'o', 0] end)
let s_null = String.from_array(recover ['f'; 'o'; 'o'; 0] end)
h.assert_eq[String](s_null, "foo\x00")
h.assert_eq[USize](s_null.size(), 4)

let s_no_null = String.from_array(recover ['f', 'o', 'o'] end)
let s_no_null = String.from_array(recover ['f'; 'o'; 'o'] end)
h.assert_eq[String](s_no_null, "foo")
h.assert_eq[USize](s_no_null.size(), 3)

Expand All @@ -944,13 +944,13 @@ class iso _TestStringFromIsoArray is UnitTest
fun name(): String => "builtin/String.from_iso_array"

fun apply(h: TestHelper) =>
let s = recover val String.from_iso_array(recover ['f', 'o', 'o'] end) end
let s = recover val String.from_iso_array(recover ['f'; 'o'; 'o'] end) end
h.assert_eq[String](s, "foo")
h.assert_eq[USize](s.size(), 3)
h.assert_true((s.space() == 3) xor s.is_null_terminated())

let s2 = recover val String.from_iso_array(recover
['1', '1', '1', '1', '1', '1', '1', '1']
['1'; '1'; '1'; '1'; '1'; '1'; '1'; '1']
end) end
h.assert_eq[String](s2, "11111111")
h.assert_eq[USize](s2.size(), 8)
Expand All @@ -962,7 +962,7 @@ class iso _TestStringSpace is UnitTest

fun apply(h: TestHelper) =>
let s = String.from_iso_array(recover
['1', '1', '1', '1', '1', '1', '1', '1']
['1'; '1'; '1'; '1'; '1'; '1'; '1'; '1']
end)

h.assert_eq[USize](s.size(), 8)
Expand All @@ -975,7 +975,7 @@ class iso _TestStringRecalc is UnitTest

fun apply(h: TestHelper) =>
let s: String ref = String.from_iso_array(recover
['1', '1', '1', '1', '1', '1', '1', '1']
['1'; '1'; '1'; '1'; '1'; '1'; '1'; '1']
end)
s.recalc()
h.assert_eq[USize](s.size(), 8)
Expand All @@ -989,7 +989,7 @@ class iso _TestStringRecalc is UnitTest
h.assert_true(s2.is_null_terminated())

let s3: String ref = String.from_iso_array(recover
['1', 0, 0, 0, 0, 0, 0, '1']
['1'; 0; 0; 0; 0; 0; 0; '1']
end)
s3.truncate(1)
s3.recalc()
Expand All @@ -1003,7 +1003,7 @@ class iso _TestStringTruncate is UnitTest

fun apply(h: TestHelper) =>
let s = recover ref String.from_iso_array(recover
['1', '1', '1', '1', '1', '1', '1', '1']
['1'; '1'; '1'; '1'; '1'; '1'; '1'; '1']
end) end
s.truncate(s.space())
h.assert_true(s.is_null_terminated())
Expand All @@ -1027,8 +1027,8 @@ class iso _TestArrayAppend is UnitTest
fun name(): String => "builtin/Array.append"

fun apply(h: TestHelper) ? =>
var a = ["one", "two", "three"]
var b = ["four", "five", "six"]
var a = ["one"; "two"; "three"]
var b = ["four"; "five"; "six"]
a.append(b)
h.assert_eq[USize](a.size(), 6)
h.assert_eq[String]("one", a(0))
Expand All @@ -1038,8 +1038,8 @@ class iso _TestArrayAppend is UnitTest
h.assert_eq[String]("five", a(4))
h.assert_eq[String]("six", a(5))

a = ["one", "two", "three"]
b = ["four", "five", "six"]
a = ["one"; "two"; "three"]
b = ["four"; "five"; "six"]
a.append(b, 1)
h.assert_eq[USize](a.size(), 5)
h.assert_eq[String]("one", a(0))
Expand All @@ -1048,8 +1048,8 @@ class iso _TestArrayAppend is UnitTest
h.assert_eq[String]("five", a(3))
h.assert_eq[String]("six", a(4))

a = ["one", "two", "three"]
b = ["four", "five", "six"]
a = ["one"; "two"; "three"]
b = ["four"; "five"; "six"]
a.append(b, 1, 1)
h.assert_eq[USize](a.size(), 4)
h.assert_eq[String]("one", a(0))
Expand All @@ -1062,8 +1062,8 @@ class iso _TestArrayConcat is UnitTest
fun name(): String => "builtin/Array.concat"

fun apply(h: TestHelper) ? =>
var a = ["one", "two", "three"]
var b = ["four", "five", "six"]
var a = ["one"; "two"; "three"]
var b = ["four"; "five"; "six"]
a.concat(b.values())
h.assert_eq[USize](a.size(), 6)
h.assert_eq[String]("one", a(0))
Expand All @@ -1073,8 +1073,8 @@ class iso _TestArrayConcat is UnitTest
h.assert_eq[String]("five", a(4))
h.assert_eq[String]("six", a(5))

a = ["one", "two", "three"]
b = ["four", "five", "six"]
a = ["one"; "two"; "three"]
b = ["four"; "five"; "six"]
a.concat(b.values(), 1)
h.assert_eq[USize](a.size(), 5)
h.assert_eq[String]("one", a(0))
Expand All @@ -1083,8 +1083,8 @@ class iso _TestArrayConcat is UnitTest
h.assert_eq[String]("five", a(3))
h.assert_eq[String]("six", a(4))

a = ["one", "two", "three"]
b = ["four", "five", "six"]
a = ["one"; "two"; "three"]
b = ["four"; "five"; "six"]
a.concat(b.values(), 1, 1)
h.assert_eq[USize](a.size(), 4)
h.assert_eq[String]("one", a(0))
Expand All @@ -1100,7 +1100,7 @@ class iso _TestArraySlice is UnitTest
fun name(): String => "builtin/Array.slice"

fun apply(h: TestHelper) ? =>
let a = ["one", "two", "three", "four", "five"]
let a = ["one"; "two"; "three"; "four"; "five"]

let b = a.slice(1, 4)
h.assert_eq[USize](b.size(), 3)
Expand Down Expand Up @@ -1136,10 +1136,10 @@ class iso _TestArrayTrim is UnitTest
fun name(): String => "builtin/Array.trim"

fun apply(h: TestHelper) =>
let orig: Array[U8] val = recover [0, 1, 2, 3, 4, 5, 6] end
h.assert_array_eq[U8]([as U8: 4, 5], orig.trim(4, 6))
h.assert_array_eq[U8]([as U8: 4, 5, 6], orig.trim(4, 7))
h.assert_array_eq[U8]([as U8: 4, 5, 6], orig.trim(4))
let orig = recover val [as U8: 0; 1; 2; 3; 4; 5; 6] end
h.assert_array_eq[U8]([as U8: 4; 5], orig.trim(4, 6))
h.assert_array_eq[U8]([as U8: 4; 5; 6], orig.trim(4, 7))
h.assert_array_eq[U8]([as U8: 4; 5; 6], orig.trim(4))
h.assert_array_eq[U8](Array[U8], orig.trim(4, 4))
h.assert_array_eq[U8](Array[U8], orig.trim(4, 1))

Expand All @@ -1151,11 +1151,11 @@ class iso _TestArrayTrimInPlace is UnitTest
fun name(): String => "builtin/Array.trim_in_place"

fun apply(h: TestHelper) =>
case(h, [4, 5], [0, 1, 2, 3, 4, 5, 6], 4, 6)
case(h, [4, 5, 6], [0, 1, 2, 3, 4, 5, 6], 4, 7)
case(h, [4, 5, 6], [0, 1, 2, 3, 4, 5, 6], 4)
case(h, Array[U8], [0, 1, 2, 3, 4, 5, 6], 4, 4)
case(h, Array[U8], [0, 1, 2, 3, 4, 5, 6], 4, 1)
case(h, [4; 5], [0; 1; 2; 3; 4; 5; 6], 4, 6)
case(h, [4; 5; 6], [0; 1; 2; 3; 4; 5; 6], 4, 7)
case(h, [4; 5; 6], [0; 1; 2; 3; 4; 5; 6], 4)
case(h, Array[U8], [0; 1; 2; 3; 4; 5; 6], 4, 4)
case(h, Array[U8], [0; 1; 2; 3; 4; 5; 6], 4, 1)

fun case(h: TestHelper, expected: Array[U8], orig: Array[U8], from: USize,
to: USize = -1)
Expand All @@ -1171,19 +1171,19 @@ class iso _TestArrayInsert is UnitTest
fun name(): String => "builtin/Array.insert"

fun apply(h: TestHelper) ? =>
let a = ["one", "three"]
let a = ["one"; "three"]
a.insert(0, "zero")
h.assert_array_eq[String](["zero", "one", "three"], a)
h.assert_array_eq[String](["zero"; "one"; "three"], a)

let b = ["one", "three"]
let b = ["one"; "three"]
b.insert(1, "two")
h.assert_array_eq[String](["one", "two", "three"], b)
h.assert_array_eq[String](["one"; "two"; "three"], b)

let c = ["one", "three"]
let c = ["one"; "three"]
c.insert(2, "four")
h.assert_array_eq[String](["one", "three", "four"], c)
h.assert_array_eq[String](["one"; "three"; "four"], c)

h.assert_error({()? => ["one", "three"].insert(3, "invalid") })
h.assert_error({()? => ["one"; "three"].insert(3, "invalid") })

class iso _TestArrayValuesRewind is UnitTest
"""
Expand All @@ -1192,7 +1192,7 @@ class iso _TestArrayValuesRewind is UnitTest
fun name(): String => "builtin/ArrayValues.rewind"

fun apply(h: TestHelper) ? =>
let av = [as U32: 1, 2, 3, 4].values()
let av = [as U32: 1; 2; 3; 4].values()

h.assert_eq[U32](1, av.next())
h.assert_eq[U32](2, av.next())
Expand Down Expand Up @@ -1221,7 +1221,7 @@ class iso _TestArrayFind is UnitTest
fun name(): String => "builtin/Array.find"

fun apply(h: TestHelper) ? =>
let a: Array[ISize] = [0, 1, 2, 3, 4, 1]
let a = [as ISize: 0; 1; 2; 3; 4; 1]
h.assert_eq[USize](1, a.find(1))
h.assert_eq[USize](5, a.find(1 where offset = 3))
h.assert_eq[USize](5, a.find(1 where nth = 1))
Expand Down
18 changes: 9 additions & 9 deletions packages/collections/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class iso _TestListsFrom is UnitTest
fun name(): String => "collections/Lists/from()"

fun apply(h: TestHelper) ? =>
let a = List[U32].from([1, 3, 5, 7, 9])
let a = List[U32].from([1; 3; 5; 7; 9])

h.assert_eq[USize](a.size(), 5)
h.assert_eq[U32](a(0), 1)
Expand Down Expand Up @@ -514,17 +514,17 @@ class iso _TestSort is UnitTest

fun apply(h: TestHelper) =>
test_sort[USize](h,
[23, 26, 31, 41, 53, 58, 59, 84, 93, 97],
[31, 41, 59, 26, 53, 58, 97, 93, 23, 84])
[23; 26; 31; 41; 53; 58; 59; 84; 93; 97],
[31; 41; 59; 26; 53; 58; 97; 93; 23; 84])
test_sort[I64](h,
[-5467984, -784, 0, 0, 42, 59, 74, 238, 905, 959, 7586, 7586, 9845],
[74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586])
[-5467984; -784; 0; 0; 42; 59; 74; 238; 905; 959; 7586; 7586; 9845],
[74; 59; 238; -784; 9845; 959; 905; 0; 0; 42; 7586; -5467984; 7586])
test_sort[F64](h,
[-959.7485, -784.0, 2.3, 7.8, 7.8, 59.0, 74.3, 238.2, 905, 9845.768],
[74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8])
[-959.7485; -784.0; 2.3; 7.8; 7.8; 59.0; 74.3; 238.2; 905; 9845.768],
[74.3; 59.0; 238.2; -784.0; 2.3; 9845.768; -959.7485; 905; 7.8; 7.8])
test_sort[String](h,
["", "%*&^*&^&", "***", "Hello", "bar", "f00", "foo", "foo"],
["", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"])
[""; "%*&^*&^&"; "***"; "Hello"; "bar"; "f00"; "foo"; "foo"],
[""; "Hello"; "foo"; "bar"; "foo"; "f00"; "%*&^*&^&"; "***"])

fun test_sort[A: (Comparable[A] val & Stringable)](
h: TestHelper,
Expand Down
Loading