Skip to content

Commit

Permalink
Start fixing memory leaks on tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGavin committed Jun 4, 2024
1 parent ef77d6a commit 6c82924
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 238 deletions.
4 changes: 2 additions & 2 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ if "%1" == "CI" (
call "tools/odinfmt/tests.bat"
if %errorlevel% neq 0 exit /b 1
) else if "%1" == "test" (
odin test tests -collection:src=src -debug
odin test tests -collection:src=src -debug -define:ODIN_TEST_THREADS=1
) else if "%1" == "single_test" (
odin test tests -collection:src=src -test-name:%2
odin test tests -collection:src=src -define:ODIN_TEST_NAMES=%2 -debug
) else if "%1" == "debug" (
odin build src\ -show-timings -collection:src=src -microarch:native -out:ols.exe -o:minimal -no-bounds-check -use-separate-modules -debug
) else (
Expand Down
376 changes: 236 additions & 140 deletions builtin/intrinsics.odin

Large diffs are not rendered by default.

36 changes: 21 additions & 15 deletions src/common/util_windows.odin
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package common

import "core:strings"
import "core:mem"
import "core:fmt"
import "core:log"
import "core:mem"
import "core:strings"

import win32 "core:sys/windows"

Expand Down Expand Up @@ -36,8 +36,14 @@ get_case_sensitive_path :: proc(
)

if (file == win32.INVALID_HANDLE) {
log.errorf("Failed on get_case_sensitive_path(%v) at %v", path, location)
log_last_error()
when !ODIN_TEST {
log.errorf(
"Failed on get_case_sensitive_path(%v) at %v",
path,
location,
)
log_last_error()
}
return path
}

Expand Down Expand Up @@ -113,17 +119,17 @@ run_executable :: proc(
startup_info.dwFlags |= win32.STARTF_USESTDHANDLES

if !win32.CreateProcessW(
nil,
&win32.utf8_to_utf16(command)[0],
nil,
nil,
true,
0,
nil,
nil,
&startup_info,
&process_info,
) {
nil,
&win32.utf8_to_utf16(command)[0],
nil,
nil,
true,
0,
nil,
nil,
&startup_info,
&process_info,
) {
return 0, false, stdout[0:]
}

Expand Down
7 changes: 5 additions & 2 deletions src/server/build.odin
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ setup_index :: proc() {
)
indexer.index = make_memory_index(symbol_collection)

dir_exe, ok := filepath.abs(path.dir(os.args[0], context.temp_allocator))
dir_exe, ok := filepath.abs(
path.dir(os.args[0], context.temp_allocator),
context.temp_allocator,
)

if !ok {
log.error(
Expand All @@ -169,7 +172,7 @@ setup_index :: proc() {
return
}

try_build_package(path.join({dir_exe, "builtin"}))
try_build_package(path.join({dir_exe, "builtin"}, context.temp_allocator))
}

free_index :: proc() {
Expand Down
5 changes: 5 additions & 0 deletions src/server/collector.odin
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ delete_symbol_collection :: proc(collection: SymbolCollection) {
}

for k, v in collection.packages {
for k2, v2 in v.methods {
delete(v2)
}
delete(v.methods)
delete(v.objc_structs)
delete(v.symbols)
}

Expand Down
3 changes: 2 additions & 1 deletion src/server/documents.odin
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ document_close :: proc(uri_string: string) -> common.Error {
common.delete_uri(document.uri)

delete(document.text)
delete(document.package_name)

document.used_text = 0

Expand Down Expand Up @@ -500,7 +501,7 @@ parse_imports :: proc(document: ^Document, config: ^common.Config) {
import_: Package
import_.original = imp.fullpath
import_.name = path.join(
elems = {
elems = {
document.package_name,
imp.fullpath[1:len(imp.fullpath) - 1],
},
Expand Down
3 changes: 2 additions & 1 deletion src/server/symbol.odin
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ SymbolType :: enum {
Struct = 22,
Type_Function = 23,
Union = 7,
Type = 8, //For maps, arrays, slices, dyn arrays, matrixes, etc
Type = 8, //For maps, arrays, slices, dyn arrays, matrixes, etc
Unresolved = 1, //Use text if not being able to resolve it.
}

Expand Down Expand Up @@ -223,6 +223,7 @@ free_symbol :: proc(symbol: Symbol, allocator: mem.Allocator) {
common.free_ast(v.group, allocator)
case SymbolEnumValue:
delete(v.names, allocator)
delete(v.ranges, allocator)
case SymbolUnionValue:
common.free_ast(v.types, allocator)
case SymbolBitSetValue:
Expand Down
83 changes: 41 additions & 42 deletions src/testing/testing.odin
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ols_testing

import "core:fmt"
import "core:log"
import "core:mem"
import "core:odin/ast"
import "core:odin/parser"
Expand All @@ -27,7 +28,7 @@ Source :: struct {

@(private)
setup :: proc(src: ^Source) {
src.main = strings.clone(src.main)
src.main = strings.clone(src.main, context.temp_allocator)
src.document = new(server.Document, context.temp_allocator)
src.document.uri = common.create_uri(
"test/test.odin",
Expand All @@ -36,7 +37,10 @@ setup :: proc(src: ^Source) {
src.document.client_owned = true
src.document.text = transmute([]u8)src.main
src.document.used_text = len(src.document.text)
src.document.allocator = new(common.Scratch_Allocator)
src.document.allocator = new(
common.Scratch_Allocator,
context.temp_allocator,
)
src.document.package_name = "test"

common.scratch_allocator_init(
Expand Down Expand Up @@ -78,12 +82,9 @@ setup :: proc(src: ^Source) {

server.document_refresh(src.document, &src.config, nil)

/*
There is a lot code here that is used in the real code, then i'd like to see.
*/


for src_pkg in src.packages {
context.allocator = common.scratch_allocator(src.document.allocator)

uri := common.create_uri(
fmt.aprintf("test/%v/package.odin", src_pkg.pkg),
context.temp_allocator,
Expand All @@ -99,7 +100,7 @@ setup :: proc(src: ^Source) {

dir := filepath.base(filepath.dir(fullpath, context.temp_allocator))

pkg := new(ast.Package)
pkg := new(ast.Package, context.temp_allocator)
pkg.kind = .Normal
pkg.fullpath = fullpath
pkg.name = dir
Expand Down Expand Up @@ -133,8 +134,20 @@ setup :: proc(src: ^Source) {

@(private)
teardown :: proc(src: ^Source) {
//A lot of these deletes are managed by other systems in ols, but to simplify it, we just delete them here in tests.

server.free_index()
server.indexer.index = {}

delete(src.document.package_name)

for k, v in server.build_cache.loaded_pkgs {
delete(k)
}

delete(server.build_cache.loaded_pkgs)

common.scratch_allocator_destroy(src.document.allocator)
}

expect_signature_labels :: proc(
Expand All @@ -148,18 +161,17 @@ expect_signature_labels :: proc(
help, ok := server.get_signature_information(src.document, src.position)

if !ok {
testing.error(t, "Failed get_signature_information")
log.error("Failed get_signature_information")
}

if len(expect_labels) == 0 && len(help.signatures) > 0 {
testing.errorf(
t,
log.errorf(
"Expected empty signature label, but received %v",
help.signatures,
)
}

flags := make([]int, len(expect_labels))
flags := make([]int, len(expect_labels), context.temp_allocator)

for expect_label, i in expect_labels {
for signature, j in help.signatures {
Expand All @@ -171,8 +183,7 @@ expect_signature_labels :: proc(

for flag, i in flags {
if flag != 1 {
testing.errorf(
t,
log.errorf(
"Expected signature label %v, but received %v",
expect_labels[i],
help.signatures,
Expand All @@ -193,8 +204,7 @@ expect_signature_parameter_position :: proc(
help, ok := server.get_signature_information(src.document, src.position)

if help.activeParameter != position {
testing.errorf(
t,
log.errorf(
"expected parameter position %v, but received %v",
position,
help.activeParameter,
Expand Down Expand Up @@ -222,18 +232,17 @@ expect_completion_labels :: proc(
)

if !ok {
testing.error(t, "Failed get_completion_list")
log.error("Failed get_completion_list")
}

if len(expect_labels) == 0 && len(completion_list.items) > 0 {
testing.errorf(
t,
log.errorf(
"Expected empty completion label, but received %v",
completion_list.items,
)
}

flags := make([]int, len(expect_labels))
flags := make([]int, len(expect_labels), context.temp_allocator)

for expect_label, i in expect_labels {
for completion, j in completion_list.items {
Expand All @@ -245,8 +254,7 @@ expect_completion_labels :: proc(

for flag, i in flags {
if flag != 1 {
testing.errorf(
t,
log.errorf(
"Expected completion detail %v, but received %v",
expect_labels[i],
completion_list.items,
Expand Down Expand Up @@ -275,18 +283,17 @@ expect_completion_details :: proc(
)

if !ok {
testing.error(t, "Failed get_completion_list")
log.error("Failed get_completion_list")
}

if len(expect_details) == 0 && len(completion_list.items) > 0 {
testing.errorf(
t,
log.errorf(
"Expected empty completion label, but received %v",
completion_list.items,
)
}

flags := make([]int, len(expect_details))
flags := make([]int, len(expect_details), context.temp_allocator)

for expect_detail, i in expect_details {
for completion, j in completion_list.items {
Expand All @@ -298,8 +305,7 @@ expect_completion_details :: proc(

for flag, i in flags {
if flag != 1 {
testing.errorf(
t,
log.errorf(
"Expected completion label %v, but received %v",
expect_details[i],
completion_list.items,
Expand All @@ -319,20 +325,18 @@ expect_hover :: proc(
hover, _, ok := server.get_hover_information(src.document, src.position)

if !ok {
testing.error(t, "Failed get_hover_information")
log.error(t, "Failed get_hover_information")
}

if expect_hover_string == "" && hover.contents.value != "" {
testing.errorf(
t,
log.errorf(
"Expected empty hover string, but received %v",
hover.contents.value,
)
}

if !strings.contains(hover.contents.value, expect_hover_string) {
testing.errorf(
t,
log.errorf(
"Expected hover string %v, but received %v",
expect_hover_string,
hover.contents.value,
Expand All @@ -351,18 +355,14 @@ expect_definition_locations :: proc(
locations, ok := server.get_definition_location(src.document, src.position)

if !ok {
testing.error(t, "Failed get_definition_location")
log.error("Failed get_definition_location")
}

if len(expect_locations) == 0 && len(locations) > 0 {
testing.errorf(
t,
"Expected empty locations, but received %v",
locations,
)
log.errorf("Expected empty locations, but received %v", locations)
}

flags := make([]int, len(expect_locations))
flags := make([]int, len(expect_locations), context.temp_allocator)

for expect_location, i in expect_locations {
for location, j in locations {
Expand All @@ -374,8 +374,7 @@ expect_definition_locations :: proc(

for flag, i in flags {
if flag != 1 {
testing.errorf(
t,
log.errorf(
"Expected location %v, but received %v",
expect_locations[i].range,
locations,
Expand Down
Loading

0 comments on commit 6c82924

Please sign in to comment.