Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Mar 27, 2024
1 parent 508d77f commit af23fe7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
12 changes: 7 additions & 5 deletions internal/rule/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ func (e *expressionTreeFactory) CreateExpressionTree(tokens []Token) (expression
// 13. While there are operators on the stack, pop them to the queue

func reversePolishNotationSort(tokens []Token) []Token {
var (
operatorStack arraystack.Stack
outputTokens []Token
)

var outputTokens []Token
operatorStack := arraystack.New()

for _, token := range tokens {
if isOperator(token) {
Expand Down Expand Up @@ -100,7 +99,10 @@ func reversePolishNotationSort(tokens []Token) []Token {
if token.Name == tokenRParen {
var lParen *Token
for !operatorStack.Empty() || lParen == nil {
v, _ := operatorStack.Pop()
v, ok := operatorStack.Pop()
if !ok {
continue
}

vt := v.(Token)

Expand Down
72 changes: 71 additions & 1 deletion internal/rule/factory_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package rule

import "testing"
import (
"reflect"
"testing"
)

func Test_isOperator(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -86,3 +89,70 @@ func Test_isTokenWithGreaterPrecedenceFromThePrecedenceSlices(t *testing.T) {
})
}
}

func Test_reversePolishNotationSort(t *testing.T) {
type args struct {
tokens []Token
}
tests := []struct {
name string
args args
want []Token
}{
{
name: "tokens should be ordered",
args: args{
tokens: []Token{
{
Name: tokenFunction,
Value: "LEN",
},
{
Name: tokenLParen,
Value: tokenLParen,
},
{
Name: tokenVariable,
Value: "p",
},
{
Name: tokenDot,
Value: ".",
},
{
Name: tokenField,
Value: "headers",
},
{
Name: tokenRParen,
Value: tokenRParen,
},
{
Name: tokenSpace,
Value: tokenSpace,
},
{
Name: tokenMoreThan,
Value: tokenMoreThan,
},
{
Name: tokenSpace,
Value: tokenSpace,
},
{
Name: tokenNumber,
Value: "5",
},
},
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := reversePolishNotationSort(tt.args.tokens); !reflect.DeepEqual(got, tt.want) {
t.Errorf("reversePolishNotationSort() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit af23fe7

Please sign in to comment.