Removes unnecessary try and error union from return typ

Added main function with basic functionality
main
2wenty1ne 2024-11-11 15:32:15 +01:00
parent 187a1d7f2b
commit d0904f568c
1 changed files with 29 additions and 17 deletions

View File

@ -12,13 +12,13 @@ pub fn LList(comptime T: type) type {
counter: u64,
pub fn init(this: *Self, allocator: Allocator) !void {
pub fn init(this: *Self, allocator: Allocator) void {
this.alloc = allocator;
this.first = null;
this.counter = 0;
}
pub fn deinit(this: *Self) !void {
pub fn deinit(this: *Self) void {
//? If there is no created Node
if (this.first == null) {
@ -40,7 +40,7 @@ pub fn LList(comptime T: type) type {
breakNext = true;
}
try currentNode.deinit();
currentNode.deinit();
currentNode = nextNode;
@ -48,7 +48,7 @@ pub fn LList(comptime T: type) type {
break;
}
}
try currentNode.deinit();
currentNode.deinit();
this.counter = 0;
}
@ -64,7 +64,7 @@ pub fn LList(comptime T: type) type {
return newNode;
}
pub fn size(this: *Self) !u64{
pub fn size(this: *Self) u64{
return this.counter;
}
@ -89,44 +89,56 @@ pub fn Node(comptime T: type) type {
}
pub fn deinit(this: *Self) !void {
pub fn deinit(this: *Self) void {
this.alloc.destroy(this.value);
this.alloc.destroy(this);
}
};
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var testList: LList(u64) = undefined;
testList.init(allocator);
_ = try testList.add(10);
_ = try testList.add(20);
testList.deinit();
}
test "init()" {
var testList: LList(u64) = undefined;
try testList.init(testingAllocator);
testList.init(testingAllocator);
try expect(testList.first == null);
try expect(testList.counter == 0);
try testList.deinit();
testList.deinit();
}
test "deinit()" {
var testList: LList(u64) = undefined;
try testList.init(testingAllocator);
testList.init(testingAllocator);
_ = try testList.add(1);
_ = try testList.add(2);
try testList.deinit();
testList.deinit();
}
test "add()" {
var testList: LList(u64) = undefined;
try testList.init(testingAllocator);
testList.init(testingAllocator);
const firstNode = try testList.add(1);
const secondNode = try testList.add(2);
@ -138,7 +150,7 @@ test "add()" {
try expect(thirdNode.value.* == 3);
try expect(fourtNode.value.* == 4);
try testList.deinit();
testList.deinit();
}
@ -146,15 +158,15 @@ test "add()" {
test "size()" {
var testList: LList(u64) = undefined;
try testList.init(testingAllocator);
testList.init(testingAllocator);
_ = try testList.add(1);
_ = try testList.add(2);
_ = try testList.add(3);
_ = try testList.add(4);
try expect(try testList.size() == 4);
try expect(testList.size() == 4);
try testList.deinit();
testList.deinit();
}