Create a unit7 for all unit tests.

This commit is contained in:
Christoffer Lerno
2025-02-24 01:05:45 +01:00
parent 70029cc4b8
commit 87725a3a9e
128 changed files with 9311 additions and 103 deletions

View File

@@ -28,16 +28,6 @@ fn usz! EnumMap.to_format(&self, Formatter* formatter) @dynamic
return n;
}
fn String EnumMap.to_string(&self, Allocator allocator) @dynamic
{
return string::format("%s", *self, allocator: allocator);
}
fn String EnumMap.to_tstring(&self) @dynamic
{
return string::tformat("%s", *self);
}
<*
@return "The total size of this map, which is the same as the number of enum values"
@pure

View File

@@ -156,7 +156,7 @@ fn void Object.init_map_if_needed(&self) @private
if (self.is_empty())
{
self.type = ObjectInternalMap.typeid;
self.map.tinit();
self.map.init(self.allocator);
}
}

View File

@@ -2,10 +2,11 @@
@require Type.kindof == ARRAY : "Required an array type"
*>
module std::collections::ringbuffer(<Type>);
import std::io;
def Element = $typeof((Type){}[0]);
struct RingBuffer
struct RingBuffer (Printable)
{
Type buf;
usz written;
@@ -19,7 +20,7 @@ fn void RingBuffer.init(&self) @inline
fn void RingBuffer.push(&self, Element c)
{
if (self.written < buf.len)
if (self.written < self.buf.len)
{
self.buf[self.written] = c;
self.written++;
@@ -27,14 +28,14 @@ fn void RingBuffer.push(&self, Element c)
else
{
self.buf[self.head] = c;
self.head = (self.head + 1) % buf.len;
self.head = (self.head + 1) % self.buf.len;
}
}
fn Element RingBuffer.get(&self, usz index) @operator([])
{
index %= buf.len;
usz avail = buf.len - self.head;
index %= self.buf.len;
usz avail = self.buf.len - self.head;
if (index < avail)
{
return self.buf[self.head + index];
@@ -48,25 +49,25 @@ fn Element! RingBuffer.pop(&self)
{
case self.written == 0:
return SearchResult.MISSING?;
case self.written < buf.len:
case self.written < self.buf.len:
self.written--;
return self.buf[self.written];
default:
self.head = (self.head - 1) % buf.len;
self.head = (self.head - 1) % self.buf.len;
return self.buf[self.head];
}
}
fn usz! RingBuffer.to_format(&self, Formatter* format)
fn usz! RingBuffer.to_format(&self, Formatter* format) @dynamic
{
// Improve this?
return format.printnf("%s", self.buf);
return format.printf("%s", self.buf);
}
fn usz RingBuffer.read(&self, usz index, Element[] buffer)
{
index %= buf.len;
if (self.written < buf.len)
index %= self.buf.len;
if (self.written < self.buf.len)
{
if (index >= self.written) return 0;
usz end = self.written - index;
@@ -74,7 +75,7 @@ fn usz RingBuffer.read(&self, usz index, Element[] buffer)
buffer[:n] = self.buf[index:n];
return n;
}
usz end = buf.len - self.head;
usz end = self.buf.len - self.head;
if (index >= end)
{
index -= end;
@@ -83,13 +84,13 @@ fn usz RingBuffer.read(&self, usz index, Element[] buffer)
buffer[:n] = self.buf[index:n];
return n;
}
if (buffer.len <= buf.len - index)
if (buffer.len <= self.buf.len - index)
{
usz n = buffer.len;
buffer[:n] = self.buf[self.head + index:n];
return n;
}
usz n1 = buf.len - index;
usz n1 = self.buf.len - index;
buffer[:n1] = self.buf[self.head + index:n1];
buffer = buffer[n1..];
index -= n1;
@@ -101,7 +102,7 @@ fn usz RingBuffer.read(&self, usz index, Element[] buffer)
fn void RingBuffer.write(&self, Element[] buffer)
{
usz i;
while (self.written < buf.len && i < buffer.len)
while (self.written < self.buf.len && i < buffer.len)
{
self.buf[self.written] = buffer[i++];
self.written++;
@@ -109,6 +110,6 @@ fn void RingBuffer.write(&self, Element[] buffer)
foreach (c : buffer[i..])
{
self.buf[self.head] = c;
self.head = (self.head + 1) % buf.len;
self.head = (self.head + 1) % self.buf.len;
}
}

View File

@@ -72,7 +72,7 @@ macro @check(#condition, String format = "", args...)
@stack_mem(512; Allocator allocator)
{
DString s;
s.new_init(allocator: allocator);
s.init(allocator);
s.appendf("check `%s` failed. ", $stringify(#condition));
s.appendf(format, ...args);
print_panicf(s.str_view());

View File

@@ -77,13 +77,13 @@ macro void! CsvReader.@each_row(self, int rows = int.max; @body(String[] row)) @
{
@stack_mem(512; Allocator mem)
{
String! s = io::readline(stream, mem);
String! s = io::readline(mem, stream);
if (catch err = s)
{
if (err == IoError.EOF) return;
return err?;
}
@body(s.split(sep, allocator: mem));
@body(s.split(mem, sep));
};
}
}

View File

@@ -27,9 +27,9 @@ fn Object*! tparse_string(String s)
fn Object*! parse(Allocator allocator, InStream s)
{
@stack_mem(512; Allocator mem)
@stack_mem(512; Allocator smem)
{
JsonContext context = { .last_string = dstring::new_with_capacity(mem, 64), .stream = s, .allocator = allocator };
JsonContext context = { .last_string = dstring::new_with_capacity(smem, 64), .stream = s, .allocator = allocator };
@pool(allocator)
{
return parse_any(&context);

View File

@@ -170,81 +170,81 @@ fn Url! parse(Allocator allocator, String url_string)
return url;
}
<*
Stringify a Url struct.
@param [in] self
@param [inout] allocator
@return "Url as a string"
*>
fn String Url.to_string(&self, Allocator allocator = allocator::heap()) @dynamic => @pool(allocator)
fn usz! Url.to_format(&self, Formatter* f) @dynamic
{
DString builder = dstring::tnew();
usz len;
// Add scheme if it exists
if (self.scheme != "")
{
builder.append_chars(self.scheme);
builder.append_char(':');
if (self.host.len > 0) builder.append_chars("//");
len += f.print(self.scheme)!;
len += f.print(":")!;
if (self.host.len > 0) len += f.print("//")!;
}
// Add username and password if they exist
if (self.username != "")
if (self.username)
{
String username = tencode(self.username, USERPASS);
builder.append_chars(username);
if (self.password != "")
@stack_mem(64; Allocator smem)
{
builder.append_char(':');
String password = tencode(self.password, USERPASS);
builder.append_chars(password);
len += f.print(encode(smem, self.username, USERPASS))!;
};
if (self.password)
{
len += f.print(":")!;
@stack_mem(64; Allocator smem)
{
len += f.print(encode(smem, self.password, USERPASS))!;
};
}
builder.append_char('@');
len += f.print("@")!;
}
// Add host
String host = tencode(self.host, HOST);
builder.append_chars(host);
@stack_mem(128; Allocator smem)
{
len += f.print(encode(smem, self.host, HOST))!;
};
// Add port
if (self.port != 0)
{
builder.append_char(':');
builder.appendf("%d", self.port);
}
if (self.port) len += f.printf(":%d", self.port)!;
// Add path
String path = tencode(self.path, PATH);
builder.append_chars(path);
@stack_mem(256; Allocator smem)
{
len += f.print(encode(smem, self.path, PATH))!;
};
// Add query if it exists (note that `query` is expected to
// be already properly encoded).
if (self.query != "")
if (self.query)
{
builder.append_char('?');
builder.append_chars(self.query);
len += f.print("?")!;
len += f.print(self.query)!;
}
// Add fragment if it exists
if (self.fragment != "")
if (self.fragment)
{
builder.append_char('#');
String fragment = tencode(self.fragment, FRAGMENT);
builder.append_chars(fragment);
@stack_mem(256; Allocator smem)
{
len += f.print("#")!;
len += f.print(encode(smem, self.fragment, FRAGMENT))!;
};
}
return builder.copy_str(allocator);
return len;
}
def UrlQueryValueList = List(<String>);
fn String Url.to_string(&self, Allocator allocator)
{
return string::format(allocator, "%s", *self);
}
def UrlQueryValueList = List{String};
struct UrlQueryValues
{
inline HashMap(<String, UrlQueryValueList>) map;
inline HashMap{String, UrlQueryValueList} map;
UrlQueryValueList key_order;
}
@@ -317,41 +317,35 @@ fn UrlQueryValues* UrlQueryValues.add(&self, String key, String value)
}
<*
Stringify UrlQueryValues into an encoded query string.
@param [in] self
@param [inout] allocator
@return "a percent-encoded query string"
*>
fn String UrlQueryValues.to_string(&self, Allocator allocator = allocator::heap()) @dynamic => @pool(allocator)
fn usz! UrlQueryValues.to_format(&self, Formatter* f) @dynamic
{
DString builder = dstring::tnew();
usz len;
usz i;
foreach (key: self.key_order)
{
String encoded_key = tencode(key, QUERY);
UrlQueryValueList! values = self.map.get(key);
if (catch values) continue;
foreach (value: values)
@stack_mem(128; Allocator mem)
{
if (i > 0) builder.append_char('&');
builder.append_chars(encoded_key);
builder.append_char('=');
String encoded_value = tencode(value, QUERY);
builder.append_chars(encoded_value);
i++;
}
};
return builder.copy_str(allocator);
String encoded_key = encode(mem, key, QUERY);
UrlQueryValueList! values = self.map.get(key);
if (catch values) continue;
foreach (value : values)
{
if (i > 0) len += f.print("&")!;
len += f.print(encoded_key)!;
len += f.print("=")!;
@stack_mem(256; Allocator smem)
{
len += f.print(encode(smem, value, QUERY))!;
};
i++;
}
};
}
return len;
}
fn void UrlQueryValues.free(&self)
{
self.map.@each(;String key, UrlQueryValueList values)