Skip to content

Commit

Permalink
black test for bytes (#574)
Browse files Browse the repository at this point in the history
* black test for bytes

* fmt

* update Map as well

---------

Co-authored-by: bob.hongbo.zhang <bob.hongbo.zhang@idea.edu.cn>
  • Loading branch information
bobzhang and bob.hongbo.zhang committed Jun 18, 2024
1 parent 798d4b5 commit c16e3a9
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 35 deletions.
10 changes: 5 additions & 5 deletions builtin/linkedhashmap.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,17 @@ fn debug_entries[K : Show, V : Show](self : Map[K, V]) -> String {

pub fn to_string[K : Show, V : Show](self : Map[K, V]) -> String {
if self.size == 0 {
return "Map::[]"
return "{}"
}
loop 0, self.head, "Map::[" {
_, None, _ as acc => return acc + "]"
loop 0, self.head, "{" {
_, None, _ as acc => return acc + "}"
i, Some({ key, value, idx, .. }), acc => {
let nxt = self.list[idx].next
if i > 0 {
// To remove the last comma
continue i + 1, nxt, acc + ", {\(key):\(value)}"
continue i + 1, nxt, acc + ", \(key):\(value)"
}
continue i + 1, nxt, acc + "{\(key):\(value)}"
continue i + 1, nxt, acc + "\(key):\(value)"
}
}
}
Expand Down
26 changes: 13 additions & 13 deletions builtin/linkedhashmap_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test "contains" {
}

test "from_array" {
let m = Map::[("a", 1), ("b", 2), ("c", 3)]
let m = { "a": 1, "b": 2, "c": 3 }
@assertion.assert_eq(m.get("a"), Some(1))?
@assertion.assert_eq(m.get("b"), Some(2))?
@assertion.assert_eq(m.get("c"), Some(3))?
Expand Down Expand Up @@ -148,14 +148,14 @@ test "is_empty" {
}

test "iter" {
let m : Map[String, Int] = Map::[("a", 1), ("b", 2), ("c", 3)]
let m = { "a": 1, "b": 2, "c": 3 }
let mut sum = 0
m.iter(fn(_k, v) { sum += v })
@assertion.assert_eq(sum, 6)?
}

test "iteri" {
let m : Map[String, Int] = Map::[("a", 1), ("b", 2), ("c", 3)]
let m = { "a": 1, "b": 2, "c": 3 }
let mut sum = 0
let mut s = ""
m.iteri(
Expand All @@ -169,7 +169,7 @@ test "iteri" {
}

test "clear" {
let m : Map[String, Int] = Map::[("a", 1), ("b", 2), ("c", 3)]
let m = { "a": 1, "b": 2, "c": 3 }
m.clear()
@assertion.assert_eq(m.size, 0)?
@assertion.assert_eq(m.capacity, 8)?
Expand Down Expand Up @@ -206,7 +206,7 @@ test "grow" {

test "as_iter" {
let buf = Buffer::make(20)
let map = Map::[(1, "one"), (2, "two"), (3, "three")]
let map = { 1: "one", 2: "two", 3: "three" }
map.as_iter().iter(
fn(e) {
let (k, v) = e
Expand All @@ -225,7 +225,7 @@ test "as_iter" {
}

test "iter order" {
let m = Map::[("one", 1)]
let m = { "one": 1 }
m["three"] = 3
m["two"] = 2
let buf = Buffer::make(0)
Expand All @@ -234,7 +234,7 @@ test "iter order" {
}

test "linked list" {
let m : Map[String, Int] = Map::[]
let m = { }
let buf = Buffer::make(0)
m.debug_write(buf)
inspect(buf, content="end")?
Expand Down Expand Up @@ -322,12 +322,12 @@ test "linked list" {
}

test "to_array" {
let map = Map::[(1, "one"), (2, "two"), (3, "three")]
let map = { 1: "one", 2: "two", 3: "three" }
inspect(map.to_array(), content="[(1, one), (2, two), (3, three)]")?
}

test "set_existing_key" {
let m : Map[String, Int] = Map::new()
let m = { }
m.set("a", 1)
m.set("b", 2)
m.set("c", 3)
Expand Down Expand Up @@ -667,15 +667,15 @@ test "op_equal" {
m1.set("a", 1)
m1.set("b", 2)
m1.set("c", 3)
let m2 : Map[String, Int] = Map::[("a", 1), ("b", 2), ("c", 3)]
let m2 = { "a": 1, "b": 2, "c": 3 }
@assertion.assert_eq(m1 == m2, true)?
}

test "to_string" {
let m : Map[String, Int] = Map::new()
inspect(m.to_string(), content="Map::[]")?
let m = { }
inspect(m.to_string(), content="{}")?
m.set("a", 1)
m.set("b", 2)
m.set("c", 3)
inspect(m.to_string(), content="Map::[{a:1}, {b:2}, {c:3}]")?
inspect(m.to_string(), content="{a:1, b:2, c:3}")?
}
17 changes: 17 additions & 0 deletions builtin/test/array_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

test {
inspect(Array::from_fixed_array([1, 2, 3, 4]), content="[1, 2, 3, 4]")?
}
6 changes: 6 additions & 0 deletions builtin/test/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"import" : [
"moonbitlang/core/builtin",
"moonbitlang/core/coverage"
]
}
10 changes: 10 additions & 0 deletions builtin/test/test.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package moonbitlang/core/builtin/test

// Values

// Types and methods

// Traits

// Extension Methods

10 changes: 9 additions & 1 deletion bytes/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/// # Usage
///
/// ```moonbit
/// let b = Bytes::[65, 66, 67]
/// let b = @bytes.of([65, 66, 67])
/// ```
///
pub fn Bytes::from_array(arr : Array[Int]) -> Bytes {
Expand All @@ -30,6 +30,14 @@ pub fn Bytes::from_array(arr : Array[Int]) -> Bytes {
rv
}

pub fn Bytes::of(arr : Array[Int]) -> Bytes {
let rv = Bytes::make(arr.length())
for i = 0; i < arr.length(); i = i + 1 {
rv[i] = arr[i].to_byte()
}
rv
}

pub fn to_array(self : Bytes) -> Array[Int] {
let rv = Array::make(self.length(), 0)
for i = 0; i < self.length(); i = i + 1 {
Expand Down
1 change: 1 addition & 0 deletions bytes/bytes.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package moonbitlang/core/bytes
impl Bytes {
from_array(Array[Int]) -> Bytes
hash(Bytes) -> Int
of(Array[Int]) -> Bytes
to_array(Bytes) -> Array[Int]
}

Expand Down
20 changes: 10 additions & 10 deletions bytes/bytes_test.mbt → bytes/test/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
// limitations under the License.

test "from_array" {
let b = Bytes::[65, 0, 66, 0, 67, 0]
let b = @bytes.of([65, 0, 66, 0, 67, 0])
inspect(b, content="ABC")?
}

test "hash" {
let b1 = Bytes::[
65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0,
]
let b2 = Bytes::[
65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 74, 0,
]
let b3 = Bytes::[128, 0, 0, 0, 0, 0, 0, 0]
let b4 = Bytes::[127, 255, 255, 255, 255, 255, 255, 255]
let b1 = @bytes.of(
[65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0],
)
let b2 = @bytes.of(
[65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 74, 0],
)
let b3 = @bytes.of([128, 0, 0, 0, 0, 0, 0, 0])
let b4 = @bytes.of([127, 255, 255, 255, 255, 255, 255, 255])
inspect(b1.hash(), content="273427599")?
inspect(b2.hash(), content="2013728637")?
inspect(b3.hash(), content="-983520567")?
Expand All @@ -36,6 +36,6 @@ test "hash" {
}

test "to_array" {
let b = Bytes::[65, 0, 66, 0, 67, 0]
let b = @bytes.of([65, 0, 66, 0, 67, 0])
inspect(b.to_array(), content="[65, 0, 66, 0, 67, 0]")?
}
3 changes: 3 additions & 0 deletions bytes/test/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"import" : ["moonbitlang/core/builtin", "moonbitlang/core/coverage", "moonbitlang/core/bytes"]
}
10 changes: 10 additions & 0 deletions bytes/test/test.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package moonbitlang/core/bytes/test

// Values

// Types and methods

// Traits

// Extension Methods

2 changes: 1 addition & 1 deletion json/json_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ test "deep access" {
inspect(
json.value("key").bind(as_array),
content=
#|Some([Number(1.0), True, Null, Array([]), Object(Map::[{key:String(\"value\")}, {value:Number(100.0)}])])
#|Some([Number(1.0), True, Null, Array([]), Object({key:String(\"value\"), value:Number(100.0)})])
,
)?
inspect(
Expand Down
10 changes: 5 additions & 5 deletions json/parse_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ fn test_parse(

//region objects
test "parses empty object" {
inspect(parse("{}"), content="Ok(Object(Map::[]))")?
inspect(parse("{}"), content="Ok(Object({}))")?
}

test "parses object" {
inspect(parse("{\"a\":1}"), content="Ok(Object(Map::[{a:Number(1.0)}]))")?
inspect(parse("{\"a\":1}"), content="Ok(Object({a:Number(1.0)}))")?
inspect(
parse("{\"a\":1,\"b\":2}"),
content="Ok(Object(Map::[{a:Number(1.0)}, {b:Number(2.0)}]))",
content="Ok(Object({a:Number(1.0), b:Number(2.0)}))",
)?
}

test "parses multiple properties" {
inspect(
parse("{\"abc\":1,\"def\":2}"),
content="Ok(Object(Map::[{abc:Number(1.0)}, {def:Number(2.0)}]))",
content="Ok(Object({abc:Number(1.0), def:Number(2.0)}))",
)?
}

test "parses nested objects" {
inspect(
parse("{\"a\":{\"b\":2}}"),
content="Ok(Object(Map::[{a:Object(Map::[{b:Number(2.0)}])}]))",
content="Ok(Object({a:Object({b:Number(2.0)})}))",
)?
}
//endregion
Expand Down

0 comments on commit c16e3a9

Please sign in to comment.