Skip to content

Commit

Permalink
Mostly NULL -> nullptr C++ conversions & @test()
Browse files Browse the repository at this point in the history
There's definitely something funky going on with decorators, but I can't
see what it is right now.  I will have to dive deeper into that, before
I can use @test() more widely.
  • Loading branch information
michaelmalonenz committed Jun 23, 2024
1 parent 9d3a9cf commit 70bcf46
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 43 deletions.
16 changes: 13 additions & 3 deletions stdlib/comet/unittest.cmt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'coverage' as coverage

class test {}

class test_case {
init(*args) {
self.args = args
Expand Down Expand Up @@ -39,6 +41,12 @@ class Assert {
assert(self.value != expected, message)
}

is_less_than(expected, message=nil) {
if (message.nil?())
message = String.format('expected {0} to be less than {1}', self.value, expected)
assert(self.value < expected, message)
}

is_true(message=nil) {
if (message.nil?())
message = String.format('expected {0} to be true', self.value)
Expand Down Expand Up @@ -167,9 +175,11 @@ if (__MAIN__ == __FILE__) {
foreach (var to_import in args) {
import to_import as imported
foreach (var func in imported.functions()) {
if (func.name().starts_with?('test_')) {
var ignoreAttr = func.attributes().filter((|x|) { return x is ignore })
var test_cases = func.attributes().filter((|x|) { return x is test_case })
var attrs = func.attributes()
var is_test = attrs.find((|x|) { return x is test })
if (func.name().starts_with?('test_') || !is_test.nil?()) {
var ignoreAttr = attrs.filter((|x|) { return x is ignore })
var test_cases = attrs.filter((|x|) { return x is test_case })
if (!ignoreAttr.empty?()) {
ignored += 1
total += test_cases.empty?() ? 1 : test_cases.length()
Expand Down
10 changes: 5 additions & 5 deletions stdlib/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ void list_constructor(void *instanceData)
ListData *data = (ListData *)instanceData;
data->count = 0;
data->capacity = 0;
data->entries = NULL;
data->entries = nullptr;
}

void list_destructor(void *instanceData)
{
ListData *data = (ListData *)instanceData;
if (data->entries != NULL)
if (data->entries != nullptr)
FREE_ARRAY(list_node_t, data->entries, data->capacity);
data->capacity = 0;
data->count = 0;
data->entries = NULL;
data->entries = nullptr;
}

VALUE list_add(VM UNUSED(*vm), VALUE self, int arg_count, VALUE *arguments)
Expand Down Expand Up @@ -482,7 +482,7 @@ VALUE list_obj_to_string(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count), VALU
stream << "[";
for (int i = 0; i < data->count; i++)
{
call_function(vm, data->entries[i].item, common_strings[STRING_TO_STRING], 0, NULL);
call_function(vm, data->entries[i].item, common_strings[STRING_TO_STRING], 0, nullptr);
stream << string_get_cstr(peek(vm, 0));
if (i != data->count - 1)
stream << ", ";
Expand Down Expand Up @@ -583,7 +583,7 @@ void init_list(VM *vm)
vm,
"ListIterator",
&list_iterator_constructor,
NULL,
nullptr,
&mark_list_iterator,
"Iterator",
CLS_ITERATOR,
Expand Down
12 changes: 6 additions & 6 deletions stdlib/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ extern "C" {
#include "cometlib.h"

typedef struct {
Obj obj;
ObjInstance obj;
} process_data_t;

typedef struct {
Obj obj;
} proces_run_result_t;
ObjInstance obj;
} process_run_result_t;

static VALUE result_klass;

Expand All @@ -30,7 +30,7 @@ VALUE process_static_run(VM *vm, VALUE UNUSED(self), int UNUSED(arg_count), VALU
{
const char *cmd = string_get_cstr(arguments[0]);
FILE *cmd_file = popen(cmd, "r");
if (cmd_file == NULL)
if (cmd_file == nullptr)
{
throw_exception_native(vm, "Exception", "Could not run a process: %s", strerror(errno));
}
Expand All @@ -53,10 +53,10 @@ VALUE process_static_run(VM *vm, VALUE UNUSED(self), int UNUSED(arg_count), VALU

void init_process(VM *vm)
{
VALUE klass = defineNativeClass(vm, "Process", NULL, NULL, NULL, NULL, CLS_PROCESS, sizeof(process_data_t), true);
VALUE klass = defineNativeClass(vm, "Process", nullptr, nullptr, nullptr, nullptr, CLS_PROCESS, sizeof(process_data_t), true);
defineNativeMethod(vm, klass, &process_static_run, "run", 1, true);

result_klass = defineNativeClass(vm, "ProcessRunResult", NULL, NULL, NULL, NULL, CLS_PROCESS_RUN_RESULT, sizeof(proces_run_result_t), true);
result_klass = defineNativeClass(vm, "ProcessRunResult", nullptr, nullptr, nullptr, nullptr, CLS_PROCESS_RUN_RESULT, sizeof(process_run_result_t), true);
}

#ifdef __cplusplus
Expand Down
58 changes: 29 additions & 29 deletions stdlib/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,35 @@ void set_constructor(void *instanceData)
SetData *data = (SetData *)instanceData;
data->capacity = 0;
data->count = 0;
data->entries = NULL;
data->entries = nullptr;
}

void set_destructor(void *data)
{
SetData *set_data = (SetData *)data;
for (int i = 0; i < set_data->capacity; i++)
{
SetEntry *next = NULL;
if (set_data->entries[i] != NULL)
SetEntry *next = nullptr;
if (set_data->entries[i] != nullptr)
{
next = set_data->entries[i]->next;
FREE(SetEntry *, set_data->entries[i]);
}

while (next != NULL)
while (next != nullptr)
{
SetEntry *current = next;
next = current->next;
FREE(SetEntry *, current);
}
}
if (set_data->entries != NULL)
if (set_data->entries != nullptr)
{
FREE_ARRAY(SetEntry, set_data->entries, set_data->capacity);
}
set_data->capacity = 0;
set_data->count = 0;
set_data->entries = NULL;
set_data->entries = nullptr;
}

VALUE set_create(VM *vm)
Expand All @@ -77,14 +77,14 @@ VALUE set_create(VM *vm)
SetData *data = GET_NATIVE_INSTANCE_DATA(SetData, set);
data->capacity = 0;
data->count = 0;
data->entries = NULL;
data->entries = nullptr;

return pop(vm);
}

static uint32_t getIndex(VM *vm, Value value, int capacity)
{
call_function(vm, value, common_strings[STRING_HASH], 0, NULL);
call_function(vm, value, common_strings[STRING_HASH], 0, nullptr);
uint32_t result = ((uint32_t) number_get_value(peek(vm, 0))) % capacity;
pop(vm);
return result;
Expand All @@ -93,16 +93,16 @@ static uint32_t getIndex(VM *vm, Value value, int capacity)
static bool insert(VM *vm, SetEntry **entries, int capacity, SetEntry *entry)
{
uint32_t index = getIndex(vm, entry->key, capacity);
if (entries[index] == NULL)
if (entries[index] == nullptr)
{
entries[index] = entry;
return true;
}
else
{
SetEntry *current = entries[index];
SetEntry *previous = NULL;
while (current != NULL)
SetEntry *previous = nullptr;
while (current != nullptr)
{
if (compare_objects(vm, current->key, entry->key))
return false;
Expand All @@ -122,13 +122,13 @@ static void adjust_capacity(VM *vm, SetData *data)
for (int i = 0; i < data->capacity; i++)
{
SetEntry *current = data->entries[i];
while (current != NULL)
while (current != nullptr)
{
insert(vm, new_entries, new_capacity, current);
current = current->next;
}
}
if (data->entries != NULL)
if (data->entries != nullptr)
{
FREE_ARRAY(SetEntry *, data->entries, data->capacity);
}
Expand All @@ -144,7 +144,7 @@ VALUE set_add(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
adjust_capacity(vm, data);
}
SetEntry *new_entry = ALLOCATE(SetEntry, 1);
new_entry->next = NULL;
new_entry->next = nullptr;
new_entry->key = arguments[0];
if (insert(vm, data->entries, data->capacity, new_entry))
{
Expand All @@ -164,14 +164,14 @@ VALUE set_remove(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
for (int i = 0; i < data->capacity; i++)
{
SetEntry *current = data->entries[i];
SetEntry *previous = NULL;
while (current != NULL)
SetEntry *previous = nullptr;
while (current != nullptr)
{
if (compare_objects(vm, current->key, arguments[0]))
{
VALUE result = current->key;
push(vm, result);
if (previous == NULL)
if (previous == nullptr)
{
data->entries[i] = current->next;
}
Expand All @@ -195,7 +195,7 @@ VALUE set_iterable_contains_q(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *
SetData *data = GET_NATIVE_INSTANCE_DATA(SetData, self);
uint32_t index = getIndex(vm, arguments[0], data->capacity);
SetEntry *current = data->entries[index];
while (current != NULL)
while (current != nullptr)
{
if (compare_objects(vm, current->key, arguments[0])) {
return TRUE_VAL;
Expand All @@ -218,7 +218,7 @@ VALUE set_union(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
for (int i = 0; i < data->capacity; i++)
{
SetEntry *entry = data->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
set_add(vm, result, 1, &entry->key);
entry = entry->next;
Expand All @@ -228,7 +228,7 @@ VALUE set_union(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
for (int i = 0; i < other->capacity; i++)
{
SetEntry *entry = other->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
set_add(vm, result, 1, &entry->key);
entry = entry->next;
Expand All @@ -250,7 +250,7 @@ VALUE set_intersect(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
for (int i = 0; i < data->capacity; i++)
{
SetEntry *entry = data->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
if (set_iterable_contains_q(vm, self, 1, &entry->key) == TRUE_VAL)
set_add(vm, result, 1, &entry->key);
Expand All @@ -273,7 +273,7 @@ VALUE set_difference(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments
for (int i = 0; i < data->capacity; i++)
{
SetEntry *entry = data->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
if (set_iterable_contains_q(vm, arguments[0], 1, &entry->key) == FALSE_VAL) {
set_add(vm, result, 1, &entry->key);
Expand All @@ -292,7 +292,7 @@ VALUE set_to_list(VM *vm, VALUE self, int UNUSED(arg_count), VALUE UNUSED(*argum
for (int i = 0; i < data->capacity; i++)
{
SetEntry *entry = data->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
list_add(vm, list, 1, &entry->key);
entry = entry->next;
Expand All @@ -310,9 +310,9 @@ VALUE set_obj_to_string(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count), VALUE
for (int i = 0; i < data->capacity; i++)
{
SetEntry *entry = data->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
call_function(vm, data->entries[i]->key, common_strings[STRING_TO_STRING], 0, NULL);
call_function(vm, data->entries[i]->key, common_strings[STRING_TO_STRING], 0, nullptr);
stream << string_get_cstr(peek(vm, 0));
if (found != data->count - 1)
stream << ", ";
Expand Down Expand Up @@ -348,7 +348,7 @@ void set_mark_contents(VALUE self)
for (int i = 0; i < data->capacity; i++)
{
SetEntry *entry = data->entries[i];
while (entry != NULL)
while (entry != nullptr)
{
markValue(entry->key);
entry = entry->next;
Expand All @@ -365,7 +365,7 @@ VALUE set_iterable_count(VM *vm, VALUE self, int UNUSED(arg_count), VALUE UNUSED
void set_iterator_constructor(void *instanceData)
{
SetIterator *iter = (SetIterator *)instanceData;
iter->current = NULL;
iter->current = nullptr;
iter->index = -1;
iter->set = NIL_VAL;
iter->returned_count = 0;
Expand All @@ -375,7 +375,7 @@ VALUE set_iterator_get_next(VM UNUSED(*vm), VALUE self, int UNUSED(arg_count), V
{
SetIterator *iter = GET_NATIVE_INSTANCE_DATA(SetIterator, self);
SetData *set_data = GET_NATIVE_INSTANCE_DATA(SetData, iter->set);
while (iter->current == NULL)
while (iter->current == nullptr)
{
iter->index++;
iter->current = set_data->entries[iter->index];
Expand Down Expand Up @@ -432,7 +432,7 @@ void init_set(VM *vm)
vm,
"SetIterator",
&set_iterator_constructor,
NULL,
nullptr,
&mark_set_iterator,
"Iterator",
CLS_ITERATOR,
Expand Down
29 changes: 29 additions & 0 deletions test_scripts/process_test.cmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { test, Assert } from 'unittest'

# @test()
function test_process_can_run_returning_status_code() {
var result = Process.run('ls')

Assert.that(result.status_code).is_equal_to(0)
}

# @test()
function test_process_output_is_a_string() {
var result = Process.run('ls')

Assert.that(result.output).is_of_type(String)
}

# @test()
function test_process_sets_the_command_used() {
var result = Process.run('ls')

Assert.that(result.command).is_equal_to('ls')
}

# @test()
function test_process_shows_failure_in_status_code() {
var result = Process.run('unknown')

Assert.that(result.status_code).is_not_equal_to(0)
}
5 changes: 5 additions & 0 deletions vmlib/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
#include "objects.h"
#include "vm.h"

#ifdef __cplusplus
#define ALLOCATE(type, count) \
(type *)reallocate(nullptr, 0, sizeof(type) * (count))
#else
#define ALLOCATE(type, count) \
(type *)reallocate(NULL, 0, sizeof(type) * (count))
#endif

#define FREE(type, pointer) \
reallocate(pointer, sizeof(type), 0)
Expand Down

0 comments on commit 70bcf46

Please sign in to comment.