ocaml: phase 6 Stack + Queue modules (+5 tests, 430 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Stack: ref-holding-list LIFO. push/pop/top/length/is_empty/clear. Queue: two-list (front, back) amortised O(1) queue. push/pop/length/ is_empty/clear. Both in OCaml syntax in runtime.sx.
This commit is contained in:
@@ -366,6 +366,48 @@
|
||||
let printf fmt = print_string fmt
|
||||
end ;;
|
||||
|
||||
module Stack = struct
|
||||
let create () = ref []
|
||||
let push x s = s := x :: !s
|
||||
let pop s =
|
||||
match !s with
|
||||
| [] -> failwith \"Stack.pop: empty\"
|
||||
| h :: t -> s := t ; h
|
||||
let top s =
|
||||
match !s with
|
||||
| [] -> failwith \"Stack.top: empty\"
|
||||
| h :: _ -> h
|
||||
let is_empty s = !s = []
|
||||
let length s = List.length !s
|
||||
let clear s = s := []
|
||||
end ;;
|
||||
|
||||
module Queue = struct
|
||||
(* Simple two-list amortized queue: (front, back). pop drains
|
||||
front; refill from rev back when front is empty. *)
|
||||
let create () = ref ([], [])
|
||||
let push x q =
|
||||
let p = !q in
|
||||
q := (List.append [] (match p with (f, b) -> f), x :: (match p with (_, b) -> b))
|
||||
let length q =
|
||||
let p = !q in
|
||||
let f = match p with (f, _) -> f in
|
||||
let b = match p with (_, b) -> b in
|
||||
List.length f + List.length b
|
||||
let is_empty q = length q = 0
|
||||
let pop q =
|
||||
let p = !q in
|
||||
let f = match p with (f, _) -> f in
|
||||
let b = match p with (_, b) -> b in
|
||||
match f with
|
||||
| h :: t -> q := (t, b) ; h
|
||||
| [] ->
|
||||
(match List.rev b with
|
||||
| [] -> failwith \"Queue.pop: empty\"
|
||||
| h :: t -> q := (t, []) ; h)
|
||||
let clear q = q := ([], [])
|
||||
end ;;
|
||||
|
||||
module Buffer = struct
|
||||
let create _ = ref []
|
||||
let add_string b s = b := s :: !b
|
||||
|
||||
@@ -1052,6 +1052,18 @@ cat > "$TMPFILE" << 'EPOCHS'
|
||||
(epoch 3502)
|
||||
(eval "(ocaml-run-program \"let b = Buffer.create 16 ;; Buffer.add_string b \\\"x\\\" ;; Buffer.clear b ;; Buffer.contents b\")")
|
||||
|
||||
;; ── Stack + Queue modules ─────────────────────────────────────
|
||||
(epoch 3600)
|
||||
(eval "(ocaml-run-program \"let s = Stack.create () ;; Stack.push 1 s ;; Stack.push 2 s ;; Stack.push 3 s ;; Stack.pop s\")")
|
||||
(epoch 3601)
|
||||
(eval "(ocaml-run-program \"let s = Stack.create () ;; Stack.push 1 s ;; Stack.push 2 s ;; Stack.length s\")")
|
||||
(epoch 3602)
|
||||
(eval "(ocaml-run-program \"let s = Stack.create () ;; Stack.push 1 s ;; Stack.top s\")")
|
||||
(epoch 3603)
|
||||
(eval "(ocaml-run-program \"let q = Queue.create () ;; Queue.push 1 q ;; Queue.push 2 q ;; Queue.push 3 q ;; Queue.pop q\")")
|
||||
(epoch 3604)
|
||||
(eval "(ocaml-run-program \"let q = Queue.create () ;; Queue.push 1 q ;; Queue.push 2 q ;; Queue.length q\")")
|
||||
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 180 "$SX_SERVER" < "$TMPFILE" 2>/dev/null)
|
||||
@@ -1665,6 +1677,13 @@ check 3500 "Buffer concat 'Hello, World'" '"Hello, World"'
|
||||
check 3501 "Buffer.length 3" '3'
|
||||
check 3502 "Buffer.clear empties" '""'
|
||||
|
||||
# ── Stack + Queue ──────────────────────────────────────────────
|
||||
check 3600 "Stack.pop LIFO" '3'
|
||||
check 3601 "Stack.length" '2'
|
||||
check 3602 "Stack.top" '1'
|
||||
check 3603 "Queue.pop FIFO" '1'
|
||||
check 3604 "Queue.length" '2'
|
||||
|
||||
TOTAL=$((PASS + FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL OCaml-on-SX tests passed"
|
||||
|
||||
Reference in New Issue
Block a user