-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.zig
More file actions
68 lines (57 loc) · 1.94 KB
/
system.zig
File metadata and controls
68 lines (57 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! # System Module
//! This module provides basic system-level utilities and abstractions.
//!
//! It includes definitions for common data structures such as String and Vector (vint).
//! The String struct encapsulates a byte slice and provides methods for common string operations.
//! The vint struct represents a vector of integers with size tracking and element access methods.
const std = @import("std");
/// String struct represents a simple string abstraction
pub const String = struct {
data: []const u8,
pub fn from(input: []const u8) String {
return String{
.data = input,
};
}
pub fn len(self: String) usize {
return self.data.len;
}
pub fn equals(self: String, other: []const u8) bool {
return std.mem.eql(u8, self.data, other);
}
};
test "String struct works correctly" {
const str = String.from("Hello, Zig!");
try std.testing.expect(str.len() == 11);
try std.testing.expect(str.equals("Hello, Zig!"));
}
/// vint struct represents a simple vector of integers
pub const vint = struct {
size: u32,
data: []const i32,
pub fn fromArray(arr: []const i32) vint {
return vint{
.size = @intCast(arr.len),
.data = arr,
};
}
pub fn get(self: vint, index: u32) ?i32 {
if (index >= self.size) return null;
return self.data[@intCast(index)];
}
};
test "vector struct works correctly" {
const arr = [_]i32{ 1, 2, 3 };
const vec = vint.fromArray(&arr);
try std.testing.expect(vec.size == 3);
try std.testing.expect(vec.data[0] == 1);
try std.testing.expect(vec.data[1] == 2);
try std.testing.expect(vec.data[2] == 3);
}
test "vector get method works correctly" {
const arr = [_]i32{ 10, 20, 30, 40 };
const vec = vint.fromArray(&arr);
try std.testing.expect(vec.get(0) == 10);
try std.testing.expect(vec.get(2) == 30);
try std.testing.expect(vec.get(4) == null);
}