tfLite_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/lf-edge/ekuiper/internal/conf"
  19. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  20. "github.com/lf-edge/ekuiper/internal/topo/state"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. )
  23. func TestTffunc_Exec(t *testing.T) {
  24. contextLogger := conf.Log.WithField("rule", "testExec")
  25. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  26. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  27. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  28. type args struct {
  29. args []interface{}
  30. ctx api.FunctionContext
  31. }
  32. tests := []struct {
  33. name string
  34. args args
  35. want interface{}
  36. want1 bool
  37. }{
  38. {
  39. name: "fizzbuzz",
  40. args: args{
  41. args: []interface{}{
  42. "fizzbuzz_model",
  43. []interface{}{1, 2, 3, 4, 5, 6, 7},
  44. },
  45. ctx: fctx,
  46. },
  47. want: []interface{}{[]float32{0.9971661, 4.145413e-05, 0.0027840463, 8.373417e-06}},
  48. want1: true,
  49. },
  50. {
  51. name: "sin",
  52. args: args{
  53. args: []interface{}{
  54. "sin_model",
  55. []interface{}{1},
  56. },
  57. ctx: fctx,
  58. },
  59. want: []interface{}{[]float32{0.86996967}},
  60. want1: true,
  61. },
  62. {
  63. name: "xor",
  64. args: args{
  65. args: []interface{}{
  66. "xor_model",
  67. []interface{}{1, 0},
  68. },
  69. ctx: fctx,
  70. },
  71. want: []interface{}{[]float32{0.9586827}},
  72. want1: true,
  73. },
  74. }
  75. for _, tt := range tests {
  76. t.Run(tt.name, func(t *testing.T) {
  77. f := &Tffunc{}
  78. got, got1 := f.Exec(tt.args.args, tt.args.ctx)
  79. if !reflect.DeepEqual(got, tt.want) {
  80. t.Errorf("Exec() got = %v, want %v", got, tt.want)
  81. }
  82. if got1 != tt.want1 {
  83. t.Errorf("Exec() got1 = %v, want %v", got1, tt.want1)
  84. }
  85. })
  86. }
  87. }