forked from nim-works/nimskull
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOC] Assorted test edits, mostly enum/arrays
- Loading branch information
1 parent
1bfc8a5
commit be7ae6d
Showing
19 changed files
with
454 additions
and
134 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
discard """ | ||
description: ''' | ||
Describe the basics of arrays: | ||
- fixed size list of a single type | ||
- literals | ||
- indexing | ||
- length | ||
''' | ||
target: "c cpp js" | ||
""" | ||
|
||
block literal: | ||
## It is possible to construct an empty array literal | ||
let a: array[0, int] = [] | ||
|
||
let b = [1, 2, 3] | ||
doAssert a == [], "two empty arrays are equal" | ||
doAssert b == [1, 2, 3], "array elements are compared by element pair" | ||
doAssert b != [1, 2, 3, 4], "array's don't partially match" | ||
|
||
block array_len: | ||
var | ||
a: array[0, int] | ||
b: array[10, int] | ||
doAssert a.len == 0, "an empty array has len 0" | ||
doAssert b.len == 10, "arrays reserve the space upfront" | ||
|
||
block array_indexing: | ||
var a = [1, 2] | ||
|
||
doAssert a[0] == 1, "arrays by default are zero based indexed" | ||
a[0] = 4 | ||
doAssert a[0] == 4, "arrays update in place" | ||
|
||
block array_indexing_with_offset: | ||
var index: array[2 .. 10, int] = [0, 1, 2, 3, 4, 5, 6, 7, 8] | ||
|
||
doAssert len(index) == 10 - 1 | ||
doAssert high(index) == 10 | ||
doAssert low(index) == 2 | ||
|
||
doAssert index[2] == 0 | ||
doAssert index[10] == 8 | ||
|
||
block curly_literal: | ||
block single_pairs: | ||
let curly = { | ||
"key1": "value1", | ||
"key2": "value2" | ||
} | ||
|
||
doAssert curly is array[2, (string, string)], "{} is an array literal as well" | ||
doAssert curly[0] == ("key1", "value1") | ||
doAssert curly[1] == ("key2", "value2") | ||
|
||
block multi_key: | ||
var used = 0 | ||
let curly = { | ||
"key1", "key2": (inc used; used), | ||
"key3": (inc used; used) | ||
} | ||
|
||
doAssert used == 3, "Expression is evaluated for each key" | ||
doAssert curly == [ | ||
("key1", 1), | ||
("key2", 2), | ||
("key3", 3) | ||
] | ||
|
||
block key_value_bracket: | ||
let bracket = [ | ||
0: "val0", | ||
1: "val1", | ||
2: "val2" | ||
] | ||
|
||
doAssert bracket is array[3, string] | ||
doAssert bracket == ["val0", "val1", "val2"] | ||
|
||
block array_different_types: | ||
# arrays can have more than just the `int` type | ||
var | ||
a = ["foo", "bar", "bar"] | ||
b = [[1], [2], [3]] | ||
doAssert a[2] == "bar" | ||
a[2] = "baz" | ||
doAssert a[2] == "baz", "element was updated" | ||
doAssert b[1][0] == 2, "chain index look ups work" |
Binary file not shown.
File renamed without changes.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Oops, something went wrong.