aboutsummaryrefslogtreecommitdiff
path: root/examples/test-suite/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/test-suite/src')
-rw-r--r--examples/test-suite/src/math.zig70
-rw-r--r--examples/test-suite/src/strings.zig86
2 files changed, 156 insertions, 0 deletions
diff --git a/examples/test-suite/src/math.zig b/examples/test-suite/src/math.zig
new file mode 100644
index 0000000..ef870ce
--- /dev/null
+++ b/examples/test-suite/src/math.zig
@@ -0,0 +1,70 @@
+const std = @import("std");
+
+pub fn factorial(n: u64) u64 {
+ if (n == 0) return 1;
+ return n * factorial(n - 1);
+}
+
+pub fn isPrime(n: u64) bool {
+ if (n < 2) return false;
+ if (n == 2) return true;
+ if (n % 2 == 0) return false;
+ var i: u64 = 3;
+ while (i * i <= n) : (i += 2) {
+ if (n % i == 0) return false;
+ }
+ return true;
+}
+
+pub fn gcd(a: u64, b: u64) u64 {
+ var x = a;
+ var y = b;
+ while (y != 0) {
+ const t = y;
+ y = x % y;
+ x = t;
+ }
+ return x;
+}
+
+// ── Tests with "math: " prefix ────────────────────────────────────────────────
+// Run all: zig build test
+// Run math only: zig build test -- --test-filter "math:"
+
+test "math: factorial base cases" {
+ try std.testing.expectEqual(@as(u64, 1), factorial(0));
+ try std.testing.expectEqual(@as(u64, 1), factorial(1));
+ try std.testing.expectEqual(@as(u64, 2), factorial(2));
+}
+
+test "math: factorial larger values" {
+ try std.testing.expectEqual(@as(u64, 120), factorial(5));
+ try std.testing.expectEqual(@as(u64, 5040), factorial(7));
+ try std.testing.expectEqual(@as(u64, 3628800), factorial(10));
+}
+
+test "math: isPrime small primes" {
+ try std.testing.expect(isPrime(2));
+ try std.testing.expect(isPrime(3));
+ try std.testing.expect(isPrime(5));
+ try std.testing.expect(isPrime(97));
+}
+
+test "math: isPrime composites and edge cases" {
+ try std.testing.expect(!isPrime(0));
+ try std.testing.expect(!isPrime(1));
+ try std.testing.expect(!isPrime(4));
+ try std.testing.expect(!isPrime(100));
+}
+
+test "math: gcd basics" {
+ try std.testing.expectEqual(@as(u64, 6), gcd(12, 18));
+ try std.testing.expectEqual(@as(u64, 1), gcd(7, 13));
+ try std.testing.expectEqual(@as(u64, 5), gcd(5, 0));
+ try std.testing.expectEqual(@as(u64, 5), gcd(0, 5));
+}
+
+// Deliberately named differently — filtered OUT by "--test-filter math:"
+test "number theory: Euclid handles equal inputs" {
+ try std.testing.expectEqual(@as(u64, 42), gcd(42, 42));
+}
diff --git a/examples/test-suite/src/strings.zig b/examples/test-suite/src/strings.zig
new file mode 100644
index 0000000..ba53f8c
--- /dev/null
+++ b/examples/test-suite/src/strings.zig
@@ -0,0 +1,86 @@
+const std = @import("std");
+
+/// Trim leading and trailing ASCII whitespace.
+pub fn trim(s: []const u8) []const u8 {
+ return std.mem.trim(u8, s, " \t\r\n");
+}
+
+/// Count non-overlapping occurrences of `needle` in `haystack`.
+pub fn countOccurrences(haystack: []const u8, needle: []const u8) usize {
+ if (needle.len == 0) return 0;
+ var count: usize = 0;
+ var i: usize = 0;
+ while (i + needle.len <= haystack.len) {
+ if (std.mem.eql(u8, haystack[i .. i + needle.len], needle)) {
+ count += 1;
+ i += needle.len;
+ } else {
+ i += 1;
+ }
+ }
+ return count;
+}
+
+/// Reverse a string byte-by-byte (ASCII safe; not grapheme-safe).
+pub fn reverseAlloc(allocator: std.mem.Allocator, s: []const u8) ![]u8 {
+ const result = try allocator.alloc(u8, s.len);
+ for (s, 0..) |byte, i| result[s.len - 1 - i] = byte;
+ return result;
+}
+
+/// Returns true if `s` starts with `prefix`.
+pub fn startsWith(s: []const u8, prefix: []const u8) bool {
+ return std.mem.startsWith(u8, s, prefix);
+}
+
+// ── Tests with "strings: " prefix ────────────────────────────────────────────
+// Run all: zig build test
+// Run strings only: zig build test -- --test-filter "strings:"
+
+test "strings: trim removes spaces" {
+ try std.testing.expectEqualStrings("hello", trim(" hello "));
+}
+
+test "strings: trim handles empty and whitespace-only" {
+ try std.testing.expectEqualStrings("", trim(""));
+ try std.testing.expectEqualStrings("", trim(" \t\n"));
+}
+
+test "strings: trim preserves inner whitespace" {
+ try std.testing.expectEqualStrings("hello world", trim(" hello world "));
+}
+
+test "strings: countOccurrences basic" {
+ try std.testing.expectEqual(@as(usize, 3), countOccurrences("abcabcabc", "abc"));
+}
+
+test "strings: countOccurrences empty needle" {
+ try std.testing.expectEqual(@as(usize, 0), countOccurrences("hello", ""));
+}
+
+test "strings: countOccurrences no match" {
+ try std.testing.expectEqual(@as(usize, 0), countOccurrences("hello", "xyz"));
+}
+
+test "strings: countOccurrences overlapping boundary" {
+ // Non-overlapping: "aa" in "aaaa" = 2
+ try std.testing.expectEqual(@as(usize, 2), countOccurrences("aaaa", "aa"));
+}
+
+test "strings: reverseAlloc" {
+ const reversed = try reverseAlloc(std.testing.allocator, "hello");
+ defer std.testing.allocator.free(reversed);
+ try std.testing.expectEqualStrings("olleh", reversed);
+}
+
+test "strings: reverseAlloc empty" {
+ const reversed = try reverseAlloc(std.testing.allocator, "");
+ defer std.testing.allocator.free(reversed);
+ try std.testing.expectEqualStrings("", reversed);
+}
+
+test "strings: startsWith" {
+ try std.testing.expect(startsWith("hello world", "hello"));
+ try std.testing.expect(!startsWith("hello world", "world"));
+ try std.testing.expect(startsWith("abc", ""));
+}