Skip to content

Commit

Permalink
Added empty function test
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Dec 30, 2024
1 parent e246097 commit b11e485
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions gen/function_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSimpleFunctionGeneration(t *testing.T) {
src := core.NewSourceView(
`func $32 @add $32 %a {
%b = ADD %a $32 #1
%c = ADD %b %a
RET
}`,
)
tkns, err := lex.NewTokenizer().Tokenize(src)
func generateFunctionFromSource(
t *testing.T,
source string,
) (*gen.FunctionInfo, core.ResultList) {
t.Helper()

sourceView := core.NewSourceView(source)

tkns, err := lex.NewTokenizer().Tokenize(sourceView)
assert.NoError(t, err)

tknView := parse.NewTokenView(tkns)
Expand All @@ -29,12 +29,22 @@ func TestSimpleFunctionGeneration(t *testing.T) {

ctx := &gen.FileGenerationContext{
GenerationContext: &testGenerationContext,
SourceContext: src.Ctx(),
SourceContext: sourceView.Ctx(),
Types: &TypeMap{intType.Name: intType},
}

generator := gen.NewFunctionGenerator()
function, results := generator.Generate(ctx, node)
return generator.Generate(ctx, node)
}

func TestSimpleFunctionGeneration(t *testing.T) {
src := `func $32 @add $32 %a {
%b = ADD %a $32 #1
%c = ADD %b %a
RET
}`

function, results := generateFunctionFromSource(t, src)
assert.True(t, results.IsEmpty())

assert.NotNil(t, function.EntryBlock)
Expand Down Expand Up @@ -73,35 +83,18 @@ func TestSimpleFunctionGeneration(t *testing.T) {
}

func TestIfElseFunctionGeneration(t *testing.T) {
src := core.NewSourceView(
`func @toBool $32 %n {
JZ %n .zero
.nonzero
%bool = ADD $32 #1 $32 #0
JMP .end
.zero
%bool = ADD $32 #0 $32 #0
.end
RET
}`,
)
tkns, err := lex.NewTokenizer().Tokenize(src)
assert.NoError(t, err)

tknView := parse.NewTokenView(tkns)
node, result := parse.NewFunctionParser().Parse(&tknView)
assert.Nil(t, result)

intType := &gen.NamedTypeInfo{Name: "$32", Size: 4}

ctx := &gen.FileGenerationContext{
GenerationContext: &testGenerationContext,
SourceContext: src.Ctx(),
Types: &TypeMap{intType.Name: intType},
}

generator := gen.NewFunctionGenerator()
function, results := generator.Generate(ctx, node)
src := `func @toBool $32 %n {
JZ %n .zero
.nonzero
%bool = ADD $32 #1 $32 #0
JMP .end
.zero
%bool = ADD $32 #0 $32 #0
.end
RET
}`

function, results := generateFunctionFromSource(t, src)
assert.True(t, results.IsEmpty())

entryBlock := function.EntryBlock
Expand All @@ -113,3 +106,11 @@ func TestIfElseFunctionGeneration(t *testing.T) {

assert.ElementsMatch(t, entryBlock.ForwardEdges, []*gen.BasicBlockInfo{nonzeroBlock, zeroBlock})
}

func TestEmptyFunctionGeneration(t *testing.T) {
function, results := generateFunctionFromSource(t, `func @empty { }`)
assert.False(t, results.IsEmpty())
assert.Nil(t, function)
details := results.Head.Value
assert.Contains(t, details[0].Message, "at least one instruction")
}

0 comments on commit b11e485

Please sign in to comment.