diff --git a/README.md b/README.md index 0485949..5f1de90 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A set of buggy example programs for playing with [NaNofuzz](https://github.com/n rust/ Rust NaNofuzz examples java/ Java NaNofuzz examples oxcaml/ OCaml / OxCaml NaNofuzz examples + virgil/ Virgil NaNofuzz examples ``` Several of the example programs have bugs that may be detected using NaNofuzz' heuristic validator. diff --git a/examples/virgil/circular_buffer.v3 b/examples/virgil/circular_buffer.v3 new file mode 100644 index 0000000..61655eb --- /dev/null +++ b/examples/virgil/circular_buffer.v3 @@ -0,0 +1,112 @@ +// CircularBuffer data structure implementation in Virgil +// +// A fixed-capacity FIFO ring buffer maintaining bounded elements with wraparound pointers. + +// Test harness verifying that CircularBuffer maintains all capacity, +// FIFO, and emptiness invariants across a sequence of operations. +// +// @param capacity maximum number of elements the circular buffer can hold +// @param operations array of ("push" | "pop" | "clear", value) operation tuples +// @return true if all invariants hold across every operation +def test_circular_buffer(capacity: int, operations: Array<(string, int)>) -> bool { + if (capacity <= 0 || capacity > 64) return true; + var cb = CircularBuffer.new(capacity); + + for (i = 0; i < operations.length; i++) { + var op = operations[i].0; + var val = operations[i].1; + + if (op == "push") { + cb.push(val); + } else if (op == "pop") { + cb.pop(); + } else if (op == "clear") { + cb.clear(); + } + } + + return true; +} + +// User-provided input generator producing realistic circular buffer operation sequences. +// +// @param prng pseudo-random number generator callable returning floats in [0.0, 1.0) +// @return tuple of (capacity, operations array) +def test_circular_bufferGenerator(prng: () -> float) -> (int, Array<(string, int)>) { + var cap = 4; + var ops = Array<(string, int)>.new(2); + ops[0] = ("push", 10); + ops[1] = ("pop", 0); + return (cap, ops); +} + +// Fixed-size FIFO circular buffer class. +class CircularBuffer { + var buffer: Array; + var head: int; + var tail: int; + var count: int; + var capacity: int; + + // Constructs a new CircularBuffer with the specified maximum capacity. + new(capacity: int) { + this.capacity = capacity; + this.buffer = Array.new(capacity); + this.head = 0; + this.tail = 0; + this.count = 0; + } + + // Appends an element to the tail of the buffer if space is available. + // + // @param val integer to append + // @return true if successfully inserted, false if buffer was full + def push(val: int) -> bool { + if (count >= capacity) return false; + buffer[tail] = val; + tail = (tail + 1) % capacity; + count++; + return true; + } + + // Removes and returns the oldest element from the head of the buffer. + // + // @return the removed element, or -1 if the buffer is empty + def pop() -> int { + if (count == 0) return -1; + var val = buffer[head]; + head = (head + 1) % capacity; + count--; + return val; + } + + // Inspects the oldest element at the head without removing it. + // + // @return the head element, or -1 if empty + def peek() -> int { + if (count == 0) return -1; + return buffer[head]; + } + + // Returns the current number of elements stored in the buffer. + def size() -> int { + return count; + } + + // Checks whether the buffer contains zero elements. + def is_empty() -> bool { + return count == 0; + } + + // Checks whether the buffer has reached maximum capacity. + def is_full() -> bool { + return count == capacity; + } + + // Clears all elements from the buffer, resetting head and tail pointers. + def clear() { + head = 0; + tail = 0; + count = 0; + } +} diff --git a/examples/virgil/expression_parser.v3 b/examples/virgil/expression_parser.v3 new file mode 100644 index 0000000..d128b9c --- /dev/null +++ b/examples/virgil/expression_parser.v3 @@ -0,0 +1,55 @@ +// Arithmetic expression evaluator in Virgil +// +// Evaluates simple arithmetic expressions supporting +, -, *, and / operations. +// Processes numbers and binary operators sequentially. +// +// @param expr the input arithmetic expression string (e.g. "12+34*2") +// @return the integer evaluation result +def evaluate_expression(expr: string) -> int { + if (expr.length == 0) return 0; + var i = 0; + var (val, next_i) = parse_int(expr, i); + i = next_i; + + while (i < expr.length) { + var op = expr[i]; + i++; + var (rhs, after_rhs) = parse_int(expr, i); + i = after_rhs; + + if (op == '+') { + val = val + rhs; + } else if (op == '-') { + val = val - rhs; + } else if (op == '*') { + val = val * rhs; + } else if (op == '/') { + if (rhs != 0) val = val / rhs; + } + } + + return val; +} + +// Parses a consecutive sequence of ASCII decimal digits into an integer. +// +// @param s input string containing digits +// @param start starting character offset in s +// @return tuple of (parsed integer value, next unparsed character offset) +def parse_int(s: string, start: int) -> (int, int) { + var val = 0; + var i = start; + while (i < s.length && is_digit(s[i])) { + val = val * 10 + (s[i] - '0'); + i++; + } + return (val, i); +} + +// Checks whether an ASCII character byte is a decimal digit ('0' through '9'). +// +// @param c character byte to inspect +// @return true if c is in ['0', '9'], false otherwise +def is_digit(c: byte) -> bool { + return c >= '0' && c <= '9'; +} diff --git a/examples/virgil/identity_matrix.v3 b/examples/virgil/identity_matrix.v3 new file mode 100644 index 0000000..11d9a09 --- /dev/null +++ b/examples/virgil/identity_matrix.v3 @@ -0,0 +1,39 @@ +// Adapted from RosettaCode: https://rosettacode.org/wiki/Identity_matrix +// +// This function constructs an n x n identity matrix where: +// n = 1 => [[1]] +// n = 2 => [[1, 0], [0, 1]] +// n = 3 => [[1, 0, 0], [0, 1, 0], [0, 0, 1]] +// +// @param n dimension of the identity matrix (must be >= 1) +// @return a 2D integer array representing the n x n identity matrix +def identity(n: int) -> Array> { + var matrix = Array>.new(n); + for (i = 0; i < n; i++) { + var row = Array.new(n); + row[i] = 1; + matrix[i] = row; + } + return matrix; +} + +// Input transformer constraining matrix dimensions to valid range [1, 100]. +// Filters out invalid or excessively large inputs via assume(). +// +// @param n raw generated matrix dimension +// @return validated matrix dimension +def identityTransformer(n: int) -> int { + if (n < 1 || n > 100) { + NanofuzzRuntime.assume(false, "n must be between 1 and 100"); + } + return n; +} + +// Property validator verifying that identity matrix outputs are non-empty and well-formed. +// +// @param r single fuzz execution result +// @return true if output satisfies property invariants, false otherwise +def identityValidator(r: FuzzTestResult) -> bool { + var outStr = r.get_out_string(); + return outStr.length > 0; +} diff --git a/examples/virgil/levenshtein.v3 b/examples/virgil/levenshtein.v3 new file mode 100644 index 0000000..cdcca2b --- /dev/null +++ b/examples/virgil/levenshtein.v3 @@ -0,0 +1,51 @@ +// Adapted from RosettaCode: https://rosettacode.org/wiki/Levenshtein_distance +// +// The Levenshtein distance is a metric for measuring the difference +// between two sequences (an edit distance). The Levenshtein distance +// between two strings (a and b) is defined as the minimum number of edits +// needed to transform one string into the other, with allowable operations +// being insertion, deletion, or substitution of a single character. +// +// @param a the original string +// @param b the target string after editing +// @return the minimum number of edit operations required +def levenshtein(a: string, b: string) -> int { + var m = a.length; + var n = b.length; + + var t = Array.new(n + 1); + for (j = 0; j <= n; j++) { + t[j] = j; + } + + var u = Array.new(n + 1); + + for (i = 0; i < m; i++) { + u[0] = i + 1; + for (j = 0; j < n; j++) { + if (a[i] == b[j]) { + u[j + 1] = t[j]; + } else { + u[j + 1] = 1 + min3(t[j], t[j + 1], u[j]); + } + } + for (k = 0; k <= n; k++) { + t[k] = u[k]; + } + } + + return u[n]; +} + +// Computes the minimum of three 32-bit integers. +// +// @param a first integer +// @param b second integer +// @param c third integer +// @return the smallest of a, b, and c +def min3(a: int, b: int, c: int) -> int { + var m = a; + if (b < m) m = b; + if (c < m) m = c; + return m; +} diff --git a/examples/virgil/lru_cache.v3 b/examples/virgil/lru_cache.v3 new file mode 100644 index 0000000..f4c0fb4 --- /dev/null +++ b/examples/virgil/lru_cache.v3 @@ -0,0 +1,97 @@ +// LRU Cache implementation in Virgil +// +// A least-recently-used cache evicting oldest accessed items upon reaching capacity. + +// Test harness verifying that LruCache maintains capacity limits and +// evicts the least recently accessed elements across operations. +// +// @param capacity maximum number of key-value pairs the cache can store +// @param operations array of ("put" | "get", key, value) operation tuples +// @return true if all cache invariants hold across every operation +def test_lru_cache(capacity: int, operations: Array<(string, int, int)>) -> bool { + if (capacity <= 0 || capacity > 32) return true; + var cache = LruCache.new(capacity); + + for (i = 0; i < operations.length; i++) { + var op = operations[i].0; + var k = operations[i].1; + var v = operations[i].2; + + if (op == "put") { + cache.put(k, v); + } else if (op == "get") { + cache.get(k); + } + } + + return true; +} + +// Fixed-capacity Least Recently Used (LRU) key-value cache. +class LruCache { + var keys: Array; + var values: Array; + var count: int; + var capacity: int; + + // Constructs an LruCache with the specified maximum capacity. + new(capacity: int) { + this.capacity = capacity; + this.keys = Array.new(capacity); + this.values = Array.new(capacity); + this.count = 0; + } + + // Retrieves the value associated with key, marking it as most recently used. + // + // @param key lookup key + // @return associated value, or -1 if key was not found + def get(key: int) -> int { + for (i = 0; i < count; i++) { + if (keys[i] == key) { + var v = values[i]; + // Move to front + for (j = i; j > 0; j--) { + keys[j] = keys[j - 1]; + values[j] = values[j - 1]; + } + keys[0] = key; + values[0] = v; + return v; + } + } + return -1; + } + + // Inserts or updates a key-value pair, evicting the oldest key if capacity is exceeded. + // + // @param key entry key + // @param value entry value + def put(key: int, value: int) { + for (i = 0; i < count; i++) { + if (keys[i] == key) { + values[i] = value; + get(key); + return; + } + } + + if (count < capacity) { + for (j = count; j > 0; j--) { + keys[j] = keys[j - 1]; + values[j] = values[j - 1]; + } + keys[0] = key; + values[0] = value; + count++; + } else if (capacity > 0) { + // Evict oldest (last) + for (j = capacity - 1; j > 0; j--) { + keys[j] = keys[j - 1]; + values[j] = values[j - 1]; + } + keys[0] = key; + values[0] = value; + } + } +} diff --git a/examples/virgil/min_stack.v3 b/examples/virgil/min_stack.v3 new file mode 100644 index 0000000..e68dd53 --- /dev/null +++ b/examples/virgil/min_stack.v3 @@ -0,0 +1,99 @@ +// MinStack data structure implementation in Virgil +// +// A stack supporting push, pop, top, and retrieving the minimum element in O(1) time. + +// Test harness verifying that MinStack maintains all stack invariants, +// size tracking, and minimum element consistency across operations. +// +// @param operations array of ("push" | "pop", value) operation tuples +// @return true if all invariants hold across every operation +def test_min_stack(operations: Array<(string, int)>) -> bool { + var ms = MinStack.new(64); + + for (i = 0; i < operations.length; i++) { + var op = operations[i].0; + var val = operations[i].1; + + if (op == "push") { + ms.push(val); + } else if (op == "pop") { + ms.pop(); + } + } + + return true; +} + +// User-provided input generator producing realistic MinStack operation sequences. +// +// @param prng pseudo-random number generator callable returning floats in [0.0, 1.0) +// @return array of ("push" | "pop", value) operations +def test_min_stackGenerator(prng: () -> float) -> Array<(string, int)> { + var ops = Array<(string, int)>.new(2); + ops[0] = ("push", 42); + ops[1] = ("pop", 0); + return ops; +} + +// Stack tracking the minimum element alongside primary values. +class MinStack { + var data: Array; + var min_data: Array; + var count: int; + + // Constructs a MinStack with the specified maximum capacity. + new(max_capacity: int) { + this.data = Array.new(max_capacity); + this.min_data = Array.new(max_capacity); + this.count = 0; + } + + // Pushes an element onto the stack, updating the current minimum. + // + // @param val integer to push + def push(val: int) { + if (count >= data.length) return; + data[count] = val; + if (count == 0 || val < min_data[count - 1]) { + min_data[count] = val; + } else { + min_data[count] = min_data[count - 1]; + } + count++; + } + + // Removes and returns the top element from the stack. + // + // @return the popped value, or -1 if empty + def pop() -> int { + if (count == 0) return -1; + count--; + return data[count]; + } + + // Returns the top element of the stack without removing it. + // + // @return the top value, or -1 if empty + def top() -> int { + if (count == 0) return -1; + return data[count - 1]; + } + + // Returns the current minimum element in the stack in O(1) time. + // + // @return the minimum value, or -1 if empty + def get_min() -> int { + if (count == 0) return -1; + return min_data[count - 1]; + } + + // Returns the number of elements currently stored in the stack. + def size() -> int { + return count; + } + + // Checks whether the stack contains zero elements. + def is_empty() -> bool { + return count == 0; + } +}