Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
112 changes: 112 additions & 0 deletions examples/virgil/circular_buffer.v3
Original file line number Diff line number Diff line change
@@ -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<int>;
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<int>.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;
}
}
55 changes: 55 additions & 0 deletions examples/virgil/expression_parser.v3
Original file line number Diff line number Diff line change
@@ -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';
}
39 changes: 39 additions & 0 deletions examples/virgil/identity_matrix.v3
Original file line number Diff line number Diff line change
@@ -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<Array<int>> {
var matrix = Array<Array<int>>.new(n);
for (i = 0; i < n; i++) {
var row = Array<int>.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;
}
51 changes: 51 additions & 0 deletions examples/virgil/levenshtein.v3
Original file line number Diff line number Diff line change
@@ -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<int>.new(n + 1);
for (j = 0; j <= n; j++) {
t[j] = j;
}

var u = Array<int>.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;
}
97 changes: 97 additions & 0 deletions examples/virgil/lru_cache.v3
Original file line number Diff line number Diff line change
@@ -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<int>;
var values: Array<int>;
var count: int;
var capacity: int;

// Constructs an LruCache with the specified maximum capacity.
new(capacity: int) {
this.capacity = capacity;
this.keys = Array<int>.new(capacity);
this.values = Array<int>.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;
}
}
}
Loading
Loading