Commit 9ffc3f99 authored by Guillaume Charifi's avatar Guillaume Charifi

compiler/tests: Add test for classes.

parent 1dd111a4
Pipeline #418 passed with stages
in 2 minutes and 46 seconds
......@@ -12,6 +12,7 @@ UNITS-$(tgt)-y := \
bm/tests/uses.bm.ok \
bm/tests/aliases.bm.ok \
bm/tests/aliases_wrong_class_name.bm.notok \
bm/tests/classes.bm.ok \
bm/tests/functions.bm.ok \
bm/tests/ternary_cond.bm.ok \
bm/tests/interfaces.bm.ok
pack bm.tests.classes
private class ListEntry
{
public ListEntry next
public String data
public Bool isEmpty
public new(String data, ListEntry next)
{
this.next = next
this.data = data
this.isEmpty = false
}
public new(String data)
{
this.new(data, this)
}
public new()
{
this.new("")
this.isEmpty = true
}
}
public class List
{
private ListEntry head
private ListEntry tail
public new()
{
this.tail = this.head = new ListEntry()
}
public String get(Int id)
{
ListEntry entry = this.head
while id-- > 0
{
entry = entry.next
}
return entry.data
}
public Void add(String element)
{
if this.head.isEmpty
{
this.tail = this.head = new ListEntry(element)
return
}
this.tail.next = new ListEntry(element)
this.tail = this.tail.next
}
public Int length()
{
Int length = 0
ListEntry entry = this.head
if entry.isEmpty
{
return 0
}
while entry <> this.tail
{
entry = entry.next
length++
}
return length + 1
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment