Skip to content

Commit

Permalink
added queue integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
wmww committed Feb 3, 2017
1 parent 37889ac commit 547818e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 132 deletions.
110 changes: 110 additions & 0 deletions examples/queue.pncn
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# this is an implementation of a queue using a dynamically sized circular buffer
# this was wretten for Pinecone v0.3, which does not yet have references, so a single global queue is used instead of a queue object

q: Queue
main

main :: {}:
(
enqueue: 1
enqueue: 2
enqueue: 3
enqueue: 4
enqueue: 5
enqueue: 6

print: dequeue
print: dequeue
print: dequeue
print: dequeue

enqueue: 7
enqueue: 8
enqueue: 9

print: dequeue
print: dequeue

print: q
)

Queue :: {array: IntArray, offset: Int, len: Int}

Queue :: {} -> {Queue}: (
(IntArray: 1), 0, 0
)

resize :: {Int}: (

newArray: IntArray: in

i: 0 | i<q.len | i: i+1 @ (
tmp: q.array
newArray.set: i, (tmp.get: (i+q.offset)%q.array.len)
)

q: newArray, 0, q.len
)

enqueue :: {Int}: (

q.len >= q.array.len ?
resize: q.array.len*2

q: q.array, q.offset, q.len+1

tmp: q.array
tmp.set: (q.offset+q.len-1)%q.array.len, in
)

dequeue :: {} -> {Int}: (

tmp: q.array
out: tmp.get: q.offset%q.array.len
q: q.array, q.offset+1, q.len-1

q.len < q.array.len/3+1 ?
resize: q.array.len/3+1

out
)

peek :: {}: (
tmp: q.array
tmp.get: q.offset%q.array.len
)

print :: {Queue}: (

print: "queue {"
i: 0 | i<in.len | i: i+1 @ (
#Bool: i ?
# print: ", "
ary: in.array
print: " "+(ary.get: (i+in.offset)%in.array.len).String
)
print: "}"
print

//
print: "raw array {"
i: 0 | i<in.array.len | i: i+1 @ (
#Bool: i ?
# print: ", "
ary: in.array
out: (ary.get: i).String
i = in.offset ?
out: " -> "+out
| i = (in.offset+in.len-1)%in.array.len ?
out: " -| "+out
|
out: " "+out
print: out
)
print: "}"
print
\\
)



1 change: 1 addition & 0 deletions examples/tests/run_tests.pncn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ print

print: "integration tests:"
runTest: "../morse", "pinecone\\nquit", "enter some text or type quit: .--. .. -. . -.-. --- -. . \nenter some text or type quit: \n"
runTest: "../queue", "1\n2\n3\n4\n5\n6\nqueue {\n 7\n 8\n 9\n}\n\n"
print
print: "tests done"

Expand Down
133 changes: 1 addition & 132 deletions other/pinecone.pncn
Original file line number Diff line number Diff line change
@@ -1,133 +1,2 @@
# print: "Hello World!"

q: Queue
main

main :: {}:
(
enqueue: 3
enqueue: 3
enqueue: 3
enqueue: 3
enqueue: 3
enqueue: 3
enqueue: 3

print: q

print: dequeue
print: dequeue
print: dequeue
print: dequeue

print: q

enqueue: 1
enqueue: 2
enqueue: 3
enqueue: 4
enqueue: 5
enqueue: 6
enqueue: 7
enqueue: 8
enqueue: 9
enqueue: 9
enqueue: 9
enqueue: 9
enqueue: 9
enqueue: 9

print: dequeue
print: dequeue
print: dequeue
print: dequeue
print: dequeue
print: dequeue
print: dequeue
print: dequeue


print: q
)

Queue :: {array: IntArray, offset: Int, len: Int}

Queue :: {} -> {Queue}: (
(IntArray: 1), 0, 0
)

resize :: {Int}: (

newArray: IntArray: in

i: 0 | i<q.len | i: i+1 @ (
tmp: q.array
newArray.set: i, (tmp.get: (i+q.offset)%q.array.len)
)

q: newArray, 0, q.len
)

enqueue :: {Int}: (

q.len >= q.array.len ?
resize: q.array.len*2

q: q.array, q.offset, q.len+1

tmp: q.array
tmp.set: (q.offset+q.len-1)%q.array.len, in
)

dequeue :: {} -> {Int}: (

tmp: q.array
out: tmp.get: q.offset%q.array.len
q: q.array, q.offset+1, q.len-1

q.len < q.array.len/3+1 ?
resize: q.array.len/3+1

out
)

peek :: {}: (
tmp: q.array
tmp.get: q.offset%q.array.len
)

print :: {Queue}: (

print: "queue {"
i: 0 | i<in.len | i: i+1 @ (
#Bool: i ?
# print: ", "
ary: in.array
print: " "+(ary.get: (i+in.offset)%in.array.len).String
)
print: "}"
print

//
print: "raw array {"
i: 0 | i<in.array.len | i: i+1 @ (
#Bool: i ?
# print: ", "
ary: in.array
out: (ary.get: i).String
i = in.offset ?
out: " -> "+out
| i = (in.offset+in.len-1)%in.array.len ?
out: " -| "+out
|
out: " "+out
print: out
)
print: "}"
print
\\
)



print: "Hello World!"

0 comments on commit 547818e

Please sign in to comment.