v2 / vlib / v / tests / reserved_keywords_as_struct_field_test.v
63 lines · 57 sloc · 1.21 KB · 5c4f7ca871e0ffa3fb6982d5b4d0a72901367f28
Raw
1// reserved_keywords_as_struct_field_test.v
2// Copyright (c) 2021 Pasha Radchenko <[email protected]>. All rights reserved.
3// Use of this source code is governed by an MIT license
4// that can be found in the LICENSE file.
5
6struct Empty {}
7
8// LLNode is struct which holds data and links
9struct LLNode {
10 data int
11 link LinkedList
12}
13
14// LinkedList represent a linked list
15type LinkedList = Empty | LLNode
16
17// insert performs inserting of the value into the LinkedList
18fn insert(ll LinkedList, val int) LinkedList {
19 match ll {
20 Empty {
21 return LLNode{val, Empty{}}
22 }
23 LLNode {
24 return LLNode{
25 ...ll
26 link: insert(ll.link, val)
27 }
28 }
29 }
30}
31
32// prepend performs inserting of the value on the top of the LinkedList
33fn prepend(ll LinkedList, val int) LinkedList {
34 match ll {
35 Empty {
36 return LLNode{val, Empty{}}
37 }
38 LLNode {
39 return LLNode{
40 data: val
41 link: ll
42 }
43 }
44 }
45}
46
47fn test_reserved_keywords_as_struct_field() {
48 mut ll := LinkedList(Empty{})
49 ll = insert(ll, 997)
50 ll = insert(ll, 998)
51 ll = insert(ll, 999)
52 mut desired_ll := LinkedList(LLNode{
53 data: 997
54 link: LinkedList(LLNode{
55 data: 998
56 link: LinkedList(LLNode{
57 data: 999
58 link: Empty{}
59 })
60 })
61 })
62 assert ll == desired_ll
63}
64