From 547818e02f58c4f57e140acc26bb19eb6284d775 Mon Sep 17 00:00:00 2001 From: William W Wold Date: Fri, 3 Feb 2017 14:03:53 -0800 Subject: [PATCH] added queue integration test --- examples/queue.pncn | 110 ++++++++++++++++++++++++++++ examples/tests/run_tests.pncn | 1 + other/pinecone.pncn | 133 +--------------------------------- 3 files changed, 112 insertions(+), 132 deletions(-) create mode 100644 examples/queue.pncn diff --git a/examples/queue.pncn b/examples/queue.pncn new file mode 100644 index 0000000..863a4f0 --- /dev/null +++ b/examples/queue.pncn @@ -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.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 "+out + | i = (in.offset+in.len-1)%in.array.len ? + out: " -| "+out + | + out: " "+out + print: out + ) + print: "}" + print + \\ +) + + + diff --git a/examples/tests/run_tests.pncn b/examples/tests/run_tests.pncn index 731af35..e6a8ac8 100644 --- a/examples/tests/run_tests.pncn +++ b/examples/tests/run_tests.pncn @@ -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" diff --git a/other/pinecone.pncn b/other/pinecone.pncn index 3db0d1b..0960ab2 100644 --- a/other/pinecone.pncn +++ b/other/pinecone.pncn @@ -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.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 "+out - | i = (in.offset+in.len-1)%in.array.len ? - out: " -| "+out - | - out: " "+out - print: out - ) - print: "}" - print - \\ -) - - - +print: "Hello World!"