Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/stack/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186408
  max_deposit = "5000000ugnot"

stack.gno

// Package stack is an educational data-structure playground that keeps BOTH a
// LIFO stack and a FIFO queue of ints, so you can compare how the two behave.
package stack

import (
	"chain"
	"strconv"
	"strings"
)

// stackData holds the LIFO stack; the last pushed element is the "top".
var stackData []int

// queueData holds the FIFO queue; the oldest enqueued element is the "front".
var queueData []int

// Push adds n to the top of the LIFO stack.
func Push(cur realm, n int) {
	stackData = append(stackData, n)
	chain.Emit("Push", "n", strconv.Itoa(n), "size", strconv.Itoa(len(stackData)))
}

// Pop removes and returns the top element of the LIFO stack.
// It panics (a cross-realm abort) if the stack is empty.
func Pop(cur realm) int {
	if len(stackData) == 0 {
		panic("stack is empty: nothing to pop")
	}
	last := len(stackData) - 1
	n := stackData[last]
	stackData = stackData[:last]
	chain.Emit("Pop", "n", strconv.Itoa(n), "size", strconv.Itoa(len(stackData)))
	return n
}

// Enqueue adds n to the back of the FIFO queue.
func Enqueue(cur realm, n int) {
	queueData = append(queueData, n)
	chain.Emit("Enqueue", "n", strconv.Itoa(n), "size", strconv.Itoa(len(queueData)))
}

// Dequeue removes and returns the front element of the FIFO queue.
// It panics (a cross-realm abort) if the queue is empty.
func Dequeue(cur realm) int {
	if len(queueData) == 0 {
		panic("queue is empty: nothing to dequeue")
	}
	n := queueData[0]
	queueData = queueData[1:]
	chain.Emit("Dequeue", "n", strconv.Itoa(n), "size", strconv.Itoa(len(queueData)))
	return n
}

// StackSize returns the number of elements in the LIFO stack.
func StackSize() int { return len(stackData) }

// QueueSize returns the number of elements in the FIFO queue.
func QueueSize() int { return len(queueData) }

// renderInts formats a slice of ints as a space-separated list, e.g. "1 2 3".
func renderInts(xs []int) string {
	if len(xs) == 0 {
		return "_(empty)_"
	}
	parts := make([]string, len(xs))
	for i, x := range xs {
		parts[i] = strconv.Itoa(x)
	}
	return "`" + strings.Join(parts, " ") + "`"
}

// Render returns a Markdown view of both structures with top/front labeled,
// their sizes, and a one-line explanation of LIFO vs FIFO.
func Render(path string) string {
	var b strings.Builder

	b.WriteString("# Stack & Queue Playground\n\n")
	b.WriteString("A **stack** is LIFO (last in, first out) — the last item pushed is the first popped. ")
	b.WriteString("A **queue** is FIFO (first in, first out) — the first item enqueued is the first dequeued.\n\n")

	b.WriteString("## Stack (LIFO) — size ")
	b.WriteString(strconv.Itoa(len(stackData)))
	b.WriteString("\n\n")
	b.WriteString("Contents (bottom → top): ")
	b.WriteString(renderInts(stackData))
	b.WriteString("\n\n")
	if len(stackData) > 0 {
		b.WriteString("Top: `")
		b.WriteString(strconv.Itoa(stackData[len(stackData)-1]))
		b.WriteString("` — the next `Pop` returns this.\n\n")
	} else {
		b.WriteString("Top: _(none)_ — `Pop` would panic.\n\n")
	}

	b.WriteString("## Queue (FIFO) — size ")
	b.WriteString(strconv.Itoa(len(queueData)))
	b.WriteString("\n\n")
	b.WriteString("Contents (front → back): ")
	b.WriteString(renderInts(queueData))
	b.WriteString("\n\n")
	if len(queueData) > 0 {
		b.WriteString("Front: `")
		b.WriteString(strconv.Itoa(queueData[0]))
		b.WriteString("` — the next `Dequeue` returns this.\n\n")
	} else {
		b.WriteString("Front: _(none)_ — `Dequeue` would panic.\n\n")
	}

	b.WriteString("---\n\n")
	b.WriteString("_Tip: push the same sequence into both, then Pop vs Dequeue to see LIFO reverse it while FIFO preserves order._\n")

	return b.String()
}

stack_test.gno

package stack

import (
	"strings"
	"testing"

	"gno.land/p/nt/testutils/v0"
	"gno.land/p/nt/uassert/v0"
)

func resetState() {
	stackData = nil
	queueData = nil
}

// TestStackLIFO checks that the stack pops in reverse order (last in, first out).
func TestStackLIFO(cur realm, t *testing.T) {
	resetState()
	addr := testutils.TestAddress("alice")
	testing.SetRealm(testing.NewUserRealm(addr))

	Push(cross(cur), 1)
	Push(cross(cur), 2)
	Push(cross(cur), 3)
	uassert.Equal(t, 3, StackSize())

	uassert.Equal(t, 3, Pop(cross(cur)))
	uassert.Equal(t, 2, Pop(cross(cur)))
	uassert.Equal(t, 1, Pop(cross(cur)))
	uassert.Equal(t, 0, StackSize())
}

// TestQueueFIFO checks that the queue dequeues in insertion order (first in, first out).
func TestQueueFIFO(cur realm, t *testing.T) {
	resetState()
	addr := testutils.TestAddress("bob")
	testing.SetRealm(testing.NewUserRealm(addr))

	Enqueue(cross(cur), 1)
	Enqueue(cross(cur), 2)
	Enqueue(cross(cur), 3)
	uassert.Equal(t, 3, QueueSize())

	uassert.Equal(t, 1, Dequeue(cross(cur)))
	uassert.Equal(t, 2, Dequeue(cross(cur)))
	uassert.Equal(t, 3, Dequeue(cross(cur)))
	uassert.Equal(t, 0, QueueSize())
}

// TestEmptyPanics checks that popping/dequeuing empty structures aborts.
func TestEmptyPanics(cur realm, t *testing.T) {
	resetState()
	addr := testutils.TestAddress("carol")
	testing.SetRealm(testing.NewUserRealm(addr))

	uassert.AbortsWithMessage(t, cur, "stack is empty: nothing to pop", func() {
		Pop(cross(cur))
	})
	uassert.AbortsWithMessage(t, cur, "queue is empty: nothing to dequeue", func() {
		Dequeue(cross(cur))
	})
}

// TestRender checks that Render reports sizes, labels, and the LIFO/FIFO explanation.
func TestRender(cur realm, t *testing.T) {
	resetState()
	addr := testutils.TestAddress("dave")
	testing.SetRealm(testing.NewUserRealm(addr))

	Push(cross(cur), 7)
	Enqueue(cross(cur), 9)

	out := Render("")
	uassert.True(t, strings.Contains(out, "Stack (LIFO)"), "should label the stack")
	uassert.True(t, strings.Contains(out, "Queue (FIFO)"), "should label the queue")
	uassert.True(t, strings.Contains(out, "Top: `7`"), "should mark the top of the stack")
	uassert.True(t, strings.Contains(out, "Front: `9`"), "should mark the front of the queue")
}