How to use Test method of filter Package

Best Gauge code snippet using filter.Test

ranger_test.go

Source:ranger_test.go Github

copy

Full Screen

...29 "github.com/pingcap/tidb/util/ranger"30 "github.com/pingcap/tidb/util/testkit"31 "github.com/pingcap/tidb/util/testleak"32)33func TestT(t *testing.T) {34 TestingT(t)35}36var _ = Suite(&testRangerSuite{})37type testRangerSuite struct {38 *parser.Parser39}40func (s *testRangerSuite) SetUpSuite(c *C) {41 s.Parser = parser.New()42}43func newDomainStoreWithBootstrap(c *C) (*domain.Domain, kv.Storage, error) {44 cluster := mocktikv.NewCluster()45 mocktikv.BootstrapWithSingleStore(cluster)46 mvccStore := mocktikv.MustNewMVCCStore()47 store, err := mockstore.NewMockTikvStore(48 mockstore.WithCluster(cluster),49 mockstore.WithMVCCStore(mvccStore),50 )51 c.Assert(err, IsNil)52 session.SetSchemaLease(0)53 session.SetStatsLease(0)54 if err != nil {55 return nil, nil, errors.Trace(err)56 }57 dom, err := session.BootstrapSession(store)58 return dom, store, errors.Trace(err)59}60func (s *testRangerSuite) TestTableRange(c *C) {61 defer testleak.AfterTest(c)()62 dom, store, err := newDomainStoreWithBootstrap(c)63 defer func() {64 dom.Close()65 store.Close()66 }()67 c.Assert(err, IsNil)68 testKit := testkit.NewTestKit(c, store)69 testKit.MustExec("use test")70 testKit.MustExec("drop table if exists t")71 testKit.MustExec("create table t(a int, b int, c int unsigned)")72 tests := []struct {73 exprStr string74 accessConds string75 filterConds string76 resultStr string77 }{78 {79 exprStr: "a = 1",80 accessConds: "[eq(test.t.a, 1)]",81 filterConds: "[]",82 resultStr: "[[1,1]]",83 },84 {85 exprStr: "1 = a",86 accessConds: "[eq(1, test.t.a)]",87 filterConds: "[]",88 resultStr: "[[1,1]]",89 },90 {91 exprStr: "a != 1",92 accessConds: "[ne(test.t.a, 1)]",93 filterConds: "[]",94 resultStr: "[[-inf,1) (1,+inf]]",95 },96 {97 exprStr: "1 != a",98 accessConds: "[ne(1, test.t.a)]",99 filterConds: "[]",100 resultStr: "[[-inf,1) (1,+inf]]",101 },102 {103 exprStr: "a > 1",104 accessConds: "[gt(test.t.a, 1)]",105 filterConds: "[]",106 resultStr: "[(1,+inf]]",107 },108 {109 exprStr: "1 < a",110 accessConds: "[lt(1, test.t.a)]",111 filterConds: "[]",112 resultStr: "[(1,+inf]]",113 },114 {115 exprStr: "a >= 1",116 accessConds: "[ge(test.t.a, 1)]",117 filterConds: "[]",118 resultStr: "[[1,+inf]]",119 },120 {121 exprStr: "1 <= a",122 accessConds: "[le(1, test.t.a)]",123 filterConds: "[]",124 resultStr: "[[1,+inf]]",125 },126 {127 exprStr: "a < 1",128 accessConds: "[lt(test.t.a, 1)]",129 filterConds: "[]",130 resultStr: "[[-inf,1)]",131 },132 {133 exprStr: "1 > a",134 accessConds: "[gt(1, test.t.a)]",135 filterConds: "[]",136 resultStr: "[[-inf,1)]",137 },138 {139 exprStr: "a <= 1",140 accessConds: "[le(test.t.a, 1)]",141 filterConds: "[]",142 resultStr: "[[-inf,1]]",143 },144 {145 exprStr: "1 >= test.t.a",146 accessConds: "[ge(1, test.t.a)]",147 filterConds: "[]",148 resultStr: "[[-inf,1]]",149 },150 {151 exprStr: "(a)",152 accessConds: "[test.t.a]",153 filterConds: "[]",154 resultStr: "[[-inf,0) (0,+inf]]",155 },156 {157 exprStr: "a in (1, 3, NULL, 2)",158 accessConds: "[in(test.t.a, 1, 3, <nil>, 2)]",159 filterConds: "[]",160 resultStr: "[[1,1] [2,2] [3,3]]",161 },162 {163 exprStr: `a IN (8,8,81,45)`,164 accessConds: "[in(test.t.a, 8, 8, 81, 45)]",165 filterConds: "[]",166 resultStr: `[[8,8] [45,45] [81,81]]`,167 },168 {169 exprStr: "a between 1 and 2",170 accessConds: "[ge(test.t.a, 1) le(test.t.a, 2)]",171 filterConds: "[]",172 resultStr: "[[1,2]]",173 },174 {175 exprStr: "a not between 1 and 2",176 accessConds: "[or(lt(test.t.a, 1), gt(test.t.a, 2))]",177 filterConds: "[]",178 resultStr: "[[-inf,1) (2,+inf]]",179 },180 {181 exprStr: "a between 2 and 1",182 accessConds: "[ge(test.t.a, 2) le(test.t.a, 1)]",183 filterConds: "[]",184 resultStr: "[]",185 },186 {187 exprStr: "a not between 2 and 1",188 accessConds: "[or(lt(test.t.a, 2), gt(test.t.a, 1))]",189 filterConds: "[]",190 resultStr: "[[-inf,+inf]]",191 },192 {193 exprStr: "a IS NULL",194 accessConds: "[isnull(test.t.a)]",195 filterConds: "[]",196 resultStr: "[]",197 },198 {199 exprStr: "a IS NOT NULL",200 accessConds: "[not(isnull(test.t.a))]",201 filterConds: "[]",202 resultStr: "[[-inf,+inf]]",203 },204 {205 exprStr: "a IS TRUE",206 accessConds: "[istrue(test.t.a)]",207 filterConds: "[]",208 resultStr: "[[-inf,0) (0,+inf]]",209 },210 {211 exprStr: "a IS NOT TRUE",212 accessConds: "[not(istrue(test.t.a))]",213 filterConds: "[]",214 resultStr: "[[0,0]]",215 },216 {217 exprStr: "a IS FALSE",218 accessConds: "[isfalse(test.t.a)]",219 filterConds: "[]",220 resultStr: "[[0,0]]",221 },222 {223 exprStr: "a IS NOT FALSE",224 accessConds: "[not(isfalse(test.t.a))]",225 filterConds: "[]",226 resultStr: "[[-inf,0) (0,+inf]]",227 },228 {229 exprStr: "a = 1 or a = 3 or a = 4 or (a > 1 and (a = -1 or a = 5))",230 accessConds: "[or(or(eq(test.t.a, 1), eq(test.t.a, 3)), or(eq(test.t.a, 4), and(gt(test.t.a, 1), or(eq(test.t.a, -1), eq(test.t.a, 5)))))]",231 filterConds: "[]",232 resultStr: "[[1,1] [3,3] [4,4] [5,5]]",233 },234 {235 exprStr: "(a = 1 and b = 1) or (a = 2 and b = 2)",236 accessConds: "[or(eq(test.t.a, 1), eq(test.t.a, 2))]",237 filterConds: "[or(and(eq(test.t.a, 1), eq(test.t.b, 1)), and(eq(test.t.a, 2), eq(test.t.b, 2)))]",238 resultStr: "[[1,1] [2,2]]",239 },240 {241 exprStr: "a = 1 or a = 3 or a = 4 or (b > 1 and (a = -1 or a = 5))",242 accessConds: "[or(or(eq(test.t.a, 1), eq(test.t.a, 3)), or(eq(test.t.a, 4), or(eq(test.t.a, -1), eq(test.t.a, 5))))]",243 filterConds: "[or(or(or(eq(test.t.a, 1), eq(test.t.a, 3)), eq(test.t.a, 4)), and(gt(test.t.b, 1), or(eq(test.t.a, -1), eq(test.t.a, 5))))]",244 resultStr: "[[-1,-1] [1,1] [3,3] [4,4] [5,5]]",245 },246 {247 exprStr: "a in (1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 2, 3, 4, 4, 1, 2)",248 accessConds: "[in(test.t.a, 1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 2, 3, 4, 4, 1, 2)]",249 filterConds: "[]",250 resultStr: "[[1,1] [2,2] [3,3] [4,4]]",251 },252 {253 exprStr: "a not in (1, 2, 3)",254 accessConds: "[not(in(test.t.a, 1, 2, 3))]",255 filterConds: "[]",256 resultStr: "[[-inf,1) (3,+inf]]",257 },258 {259 exprStr: "a > 9223372036854775807",260 accessConds: "[gt(test.t.a, 9223372036854775807)]",261 filterConds: "[]",262 resultStr: "[]",263 },264 {265 exprStr: "a >= 9223372036854775807",266 accessConds: "[ge(test.t.a, 9223372036854775807)]",267 filterConds: "[]",268 resultStr: "[[9223372036854775807,+inf]]",269 },270 {271 exprStr: "a < -9223372036854775807",272 accessConds: "[lt(test.t.a, -9223372036854775807)]",273 filterConds: "[]",274 resultStr: "[[-inf,-9223372036854775807)]",275 },276 {277 exprStr: "a < -9223372036854775808",278 accessConds: "[lt(test.t.a, -9223372036854775808)]",279 filterConds: "[]",280 resultStr: "[]",281 },282 }283 for _, tt := range tests {284 sql := "select * from t where " + tt.exprStr285 ctx := testKit.Se.(sessionctx.Context)286 stmts, err := session.Parse(ctx, sql)287 c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprStr))288 c.Assert(stmts, HasLen, 1)289 is := domain.GetDomain(ctx).InfoSchema()290 err = plannercore.Preprocess(ctx, stmts[0], is)291 c.Assert(err, IsNil, Commentf("error %v, for resolve name, expr %s", err, tt.exprStr))292 p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)293 c.Assert(err, IsNil, Commentf("error %v, for build plan, expr %s", err, tt.exprStr))294 selection := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)295 conds := make([]expression.Expression, 0, len(selection.Conditions))296 for _, cond := range selection.Conditions {297 conds = append(conds, expression.PushDownNot(ctx, cond, false))298 }299 tbl := selection.Children()[0].(*plannercore.DataSource).TableInfo()300 col := expression.ColInfo2Col(selection.Schema().Columns, tbl.Columns[0])301 c.Assert(col, NotNil)302 var filter []expression.Expression303 conds, filter = ranger.DetachCondsForTableRange(ctx, conds, col)304 c.Assert(fmt.Sprintf("%s", conds), Equals, tt.accessConds, Commentf("wrong access conditions for expr: %s", tt.exprStr))305 c.Assert(fmt.Sprintf("%s", filter), Equals, tt.filterConds, Commentf("wrong filter conditions for expr: %s", tt.exprStr))306 result, err := ranger.BuildTableRange(conds, new(stmtctx.StatementContext), col.RetType)307 c.Assert(err, IsNil, Commentf("failed to build table range for expr %s", tt.exprStr))308 got := fmt.Sprintf("%v", result)309 c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s", tt.exprStr))310 }311}312func (s *testRangerSuite) TestIndexRange(c *C) {313 defer testleak.AfterTest(c)()314 dom, store, err := newDomainStoreWithBootstrap(c)315 defer func() {316 dom.Close()317 store.Close()318 }()319 c.Assert(err, IsNil)320 testKit := testkit.NewTestKit(c, store)321 testKit.MustExec("use test")322 testKit.MustExec("drop table if exists t")323 testKit.MustExec("create table t(a varchar(50), b int, c double, d varchar(10), e binary(10), index idx_ab(a(50), b), index idx_cb(c, a), index idx_d(d(2)), index idx_e(e(2)))")324 tests := []struct {325 indexPos int326 exprStr string327 accessConds string328 filterConds string329 resultStr string330 }{331 {332 indexPos: 0,333 exprStr: `a LIKE 'abc%'`,334 accessConds: `[like(test.t.a, abc%, 92)]`,335 filterConds: "[]",336 resultStr: "[[\"abc\",\"abd\")]",337 },338 {339 indexPos: 0,340 exprStr: "a LIKE 'abc_'",341 accessConds: "[like(test.t.a, abc_, 92)]",342 filterConds: "[like(test.t.a, abc_, 92)]",343 resultStr: "[(\"abc\",\"abd\")]",344 },345 {346 indexPos: 0,347 exprStr: "a LIKE 'abc'",348 accessConds: "[eq(test.t.a, abc)]",349 filterConds: "[]",350 resultStr: "[[\"abc\",\"abc\"]]",351 },352 {353 indexPos: 0,354 exprStr: `a LIKE "ab\_c"`,355 accessConds: "[eq(test.t.a, ab_c)]",356 filterConds: "[]",357 resultStr: "[[\"ab_c\",\"ab_c\"]]",358 },359 {360 indexPos: 0,361 exprStr: `a LIKE '%'`,362 accessConds: "[]",363 filterConds: `[like(test.t.a, %, 92)]`,364 resultStr: "[[NULL,+inf]]",365 },366 {367 indexPos: 0,368 exprStr: `a LIKE '\%a'`,369 accessConds: "[eq(test.t.a, %a)]",370 filterConds: "[]",371 resultStr: `[["%a","%a"]]`,372 },373 {374 indexPos: 0,375 exprStr: `a LIKE "\\"`,376 accessConds: "[eq(test.t.a, \\)]",377 filterConds: "[]",378 resultStr: "[[\"\\\",\"\\\"]]",379 },380 {381 indexPos: 0,382 exprStr: `a LIKE "\\\\a%"`,383 accessConds: `[like(test.t.a, \\a%, 92)]`,384 filterConds: "[]",385 resultStr: "[[\"\\a\",\"\\b\")]",386 },387 {388 indexPos: 0,389 exprStr: `a > NULL`,390 accessConds: "[gt(test.t.a, <nil>)]",391 filterConds: "[]",392 resultStr: `[]`,393 },394 {395 indexPos: 0,396 exprStr: `a = 'a' and b in (1, 2, 3)`,397 accessConds: "[eq(test.t.a, a) in(test.t.b, 1, 2, 3)]",398 filterConds: "[]",399 resultStr: "[[\"a\" 1,\"a\" 1] [\"a\" 2,\"a\" 2] [\"a\" 3,\"a\" 3]]",400 },401 {402 indexPos: 0,403 exprStr: `a = 'a' and b not in (1, 2, 3)`,404 accessConds: "[eq(test.t.a, a) not(in(test.t.b, 1, 2, 3))]",405 filterConds: "[]",406 resultStr: "[(\"a\" NULL,\"a\" 1) (\"a\" 3,\"a\" +inf]]",407 },408 {409 indexPos: 0,410 exprStr: `a in ('a') and b in ('1', 2.0, NULL)`,411 accessConds: "[in(test.t.a, a) in(test.t.b, 1, 2, <nil>)]",412 filterConds: "[]",413 resultStr: `[["a" 1,"a" 1] ["a" 2,"a" 2]]`,414 },415 {416 indexPos: 1,417 exprStr: `c in ('1.1', 1, 1.1) and a in ('1', 'a', NULL)`,418 accessConds: "[in(test.t.c, 1.1, 1, 1.1) in(test.t.a, 1, a, <nil>)]",419 filterConds: "[]",420 resultStr: "[[1 \"1\",1 \"1\"] [1 \"a\",1 \"a\"] [1.1 \"1\",1.1 \"1\"] [1.1 \"a\",1.1 \"a\"]]",421 },422 {423 indexPos: 1,424 exprStr: "c in (1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 2, 3, 4, 4, 1, 2)",425 accessConds: "[in(test.t.c, 1, 1, 1, 1, 1, 1, 2, 1, 2, 3, 2, 3, 4, 4, 1, 2)]",426 filterConds: "[]",427 resultStr: "[[1,1] [2,2] [3,3] [4,4]]",428 },429 {430 indexPos: 1,431 exprStr: "c not in (1, 2, 3)",432 accessConds: "[not(in(test.t.c, 1, 2, 3))]",433 filterConds: "[]",434 resultStr: "[(NULL,1) (1,2) (2,3) (3,+inf]]",435 },436 {437 indexPos: 1,438 exprStr: "c in (1, 2) and c in (1, 3)",439 accessConds: "[eq(test.t.c, 1)]",440 filterConds: "[]",441 resultStr: "[[1,1]]",442 },443 {444 indexPos: 1,445 exprStr: "c = 1 and c = 2",446 accessConds: "[]",447 filterConds: "[]",448 resultStr: "[]",449 },450 {451 indexPos: 0,452 exprStr: "a in (NULL)",453 accessConds: "[in(test.t.a, <nil>)]",454 filterConds: "[]",455 resultStr: "[]",456 },457 {458 indexPos: 0,459 exprStr: "a not in (NULL, '1', '2', '3')",460 accessConds: "[not(in(test.t.a, <nil>, 1, 2, 3))]",461 filterConds: "[]",462 resultStr: "[]",463 },464 {465 indexPos: 0,466 exprStr: "not (a not in (NULL, '1', '2', '3') and a > '2')",467 accessConds: "[or(in(test.t.a, <nil>, 1, 2, 3), le(test.t.a, 2))]",468 filterConds: "[]",469 resultStr: "[[-inf,\"2\"] [\"3\",\"3\"]]",470 },471 {472 indexPos: 0,473 exprStr: "not (a not in (NULL) and a > '2')",474 accessConds: "[or(in(test.t.a, <nil>), le(test.t.a, 2))]",475 filterConds: "[]",476 resultStr: "[[-inf,\"2\"]]",477 },478 {479 indexPos: 0,480 exprStr: "not (a not in (NULL) or a > '2')",481 accessConds: "[and(in(test.t.a, <nil>), le(test.t.a, 2))]",482 filterConds: "[]",483 resultStr: "[]",484 },485 {486 indexPos: 0,487 exprStr: "(a > 'b' and a < 'bbb') or (a < 'cb' and a > 'a')",488 accessConds: "[or(and(gt(test.t.a, b), lt(test.t.a, bbb)), and(lt(test.t.a, cb), gt(test.t.a, a)))]",489 filterConds: "[]",490 resultStr: "[(\"a\",\"cb\")]",491 },492 {493 indexPos: 0,494 exprStr: "(a > 'a' and a < 'b') or (a >= 'b' and a < 'c')",495 accessConds: "[or(and(gt(test.t.a, a), lt(test.t.a, b)), and(ge(test.t.a, b), lt(test.t.a, c)))]",496 filterConds: "[]",497 resultStr: "[(\"a\",\"c\")]",498 },499 {500 indexPos: 0,501 exprStr: "(a > 'a' and a < 'b' and b < 1) or (a >= 'b' and a < 'c')",502 accessConds: "[or(and(gt(test.t.a, a), lt(test.t.a, b)), and(ge(test.t.a, b), lt(test.t.a, c)))]",503 filterConds: "[or(and(and(gt(test.t.a, a), lt(test.t.a, b)), lt(test.t.b, 1)), and(ge(test.t.a, b), lt(test.t.a, c)))]",504 resultStr: "[(\"a\",\"c\")]",505 },506 {507 indexPos: 0,508 exprStr: "(a in ('a', 'b') and b < 1) or (a >= 'b' and a < 'c')",509 accessConds: "[or(and(in(test.t.a, a, b), lt(test.t.b, 1)), and(ge(test.t.a, b), lt(test.t.a, c)))]",510 filterConds: "[]",511 resultStr: `[["a" -inf,"a" 1) ["b","c")]`,512 },513 {514 indexPos: 0,515 exprStr: "(a > 'a') or (c > 1)",516 accessConds: "[]",517 filterConds: "[or(gt(test.t.a, a), gt(test.t.c, 1))]",518 resultStr: "[[NULL,+inf]]",519 },520 {521 indexPos: 2,522 exprStr: `d = "你好啊"`,523 accessConds: "[eq(test.t.d, 你好啊)]",524 filterConds: "[eq(test.t.d, 你好啊)]",525 resultStr: "[[\"你好\",\"你好\"]]",526 },527 {528 indexPos: 3,529 exprStr: `e = "你好啊"`,530 accessConds: "[eq(test.t.e, 你好啊)]",531 filterConds: "[eq(test.t.e, 你好啊)]",532 resultStr: "[[\"[228 189]\",\"[228 189]\"]]",533 },534 {535 indexPos: 2,536 exprStr: `d in ("你好啊")`,537 accessConds: "[in(test.t.d, 你好啊)]",538 filterConds: "[in(test.t.d, 你好啊)]",539 resultStr: "[[\"你好\",\"你好\"]]",540 },541 {542 indexPos: 2,543 exprStr: `d not in ("你好啊")`,544 accessConds: "[not(in(test.t.d, 你好啊))]",545 filterConds: "[not(in(test.t.d, 你好啊))]",546 resultStr: "[(NULL,+inf]]",547 },548 {549 indexPos: 2,550 exprStr: `d < "你好" || d > "你好"`,551 accessConds: "[or(lt(test.t.d, 你好), gt(test.t.d, 你好))]",552 filterConds: "[or(lt(test.t.d, 你好), gt(test.t.d, 你好))]",553 resultStr: "[[-inf,\"你好\") (\"你好\",+inf]]",554 },555 {556 indexPos: 2,557 exprStr: `not(d < "你好" || d > "你好")`,558 accessConds: "[and(ge(test.t.d, 你好), le(test.t.d, 你好))]",559 filterConds: "[and(ge(test.t.d, 你好), le(test.t.d, 你好))]",560 resultStr: "[[\"你好\",\"你好\"]]",561 },562 }563 for _, tt := range tests {564 sql := "select * from t where " + tt.exprStr565 ctx := testKit.Se.(sessionctx.Context)566 stmts, err := session.Parse(ctx, sql)567 c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprStr))568 c.Assert(stmts, HasLen, 1)569 is := domain.GetDomain(ctx).InfoSchema()570 err = plannercore.Preprocess(ctx, stmts[0], is)571 c.Assert(err, IsNil, Commentf("error %v, for resolve name, expr %s", err, tt.exprStr))572 p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)573 c.Assert(err, IsNil, Commentf("error %v, for build plan, expr %s", err, tt.exprStr))574 selection := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)575 tbl := selection.Children()[0].(*plannercore.DataSource).TableInfo()576 c.Assert(selection, NotNil, Commentf("expr:%v", tt.exprStr))577 conds := make([]expression.Expression, 0, len(selection.Conditions))578 for _, cond := range selection.Conditions {579 conds = append(conds, expression.PushDownNot(ctx, cond, false))580 }581 cols, lengths := expression.IndexInfo2Cols(selection.Schema().Columns, tbl.Indices[tt.indexPos])582 c.Assert(cols, NotNil)583 res, err := ranger.DetachCondAndBuildRangeForIndex(ctx, conds, cols, lengths)584 c.Assert(err, IsNil)585 c.Assert(fmt.Sprintf("%s", res.AccessConds), Equals, tt.accessConds, Commentf("wrong access conditions for expr: %s", tt.exprStr))586 c.Assert(fmt.Sprintf("%s", res.RemainedConds), Equals, tt.filterConds, Commentf("wrong filter conditions for expr: %s", tt.exprStr))587 got := fmt.Sprintf("%v", res.Ranges)588 c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s", tt.exprStr))589 }590}591// for issue #6661592func (s *testRangerSuite) TestIndexRangeForUnsignedInt(c *C) {593 defer testleak.AfterTest(c)()594 dom, store, err := newDomainStoreWithBootstrap(c)595 defer func() {596 dom.Close()597 store.Close()598 }()599 c.Assert(err, IsNil)600 testKit := testkit.NewTestKit(c, store)601 testKit.MustExec("use test")602 testKit.MustExec("drop table if exists t")603 testKit.MustExec("create table t (a smallint(5) unsigned,key (a) )")604 tests := []struct {605 indexPos int606 exprStr string607 accessConds string608 filterConds string609 resultStr string610 }{611 {612 indexPos: 0,613 exprStr: `a not in (0, 1, 2)`,614 accessConds: "[not(in(test.t.a, 0, 1, 2))]",615 filterConds: "[]",616 resultStr: `[(NULL,0) (2,+inf]]`,617 },618 {619 indexPos: 0,620 exprStr: `a not in (-1, 1, 2)`,621 accessConds: "[not(in(test.t.a, -1, 1, 2))]",622 filterConds: "[]",623 resultStr: `[(NULL,1) (2,+inf]]`,624 },625 {626 indexPos: 0,627 exprStr: `a not in (-2, -1, 1, 2)`,628 accessConds: "[not(in(test.t.a, -2, -1, 1, 2))]",629 filterConds: "[]",630 resultStr: `[(NULL,1) (2,+inf]]`,631 },632 {633 indexPos: 0,634 exprStr: `a not in (111)`,635 accessConds: "[not(in(test.t.a, 111))]",636 filterConds: "[]",637 resultStr: `[(NULL,111) (111,+inf]]`,638 },639 {640 indexPos: 0,641 exprStr: `a not in (1, 2, 9223372036854775810)`,642 accessConds: "[not(in(test.t.a, 1, 2, 9223372036854775810))]",643 filterConds: "[]",644 resultStr: `[(NULL,1) (2,9223372036854775810) (9223372036854775810,+inf]]`,645 },646 }647 for _, tt := range tests {648 sql := "select * from t where " + tt.exprStr649 ctx := testKit.Se.(sessionctx.Context)650 stmts, err := session.Parse(ctx, sql)651 c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprStr))652 c.Assert(stmts, HasLen, 1)653 is := domain.GetDomain(ctx).InfoSchema()654 err = plannercore.Preprocess(ctx, stmts[0], is)655 c.Assert(err, IsNil, Commentf("error %v, for resolve name, expr %s", err, tt.exprStr))656 p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)657 c.Assert(err, IsNil, Commentf("error %v, for build plan, expr %s", err, tt.exprStr))658 selection := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)659 tbl := selection.Children()[0].(*plannercore.DataSource).TableInfo()660 c.Assert(selection, NotNil, Commentf("expr:%v", tt.exprStr))661 conds := make([]expression.Expression, 0, len(selection.Conditions))662 for _, cond := range selection.Conditions {663 conds = append(conds, expression.PushDownNot(ctx, cond, false))664 }665 cols, lengths := expression.IndexInfo2Cols(selection.Schema().Columns, tbl.Indices[tt.indexPos])666 c.Assert(cols, NotNil)667 res, err := ranger.DetachCondAndBuildRangeForIndex(ctx, conds, cols, lengths)668 c.Assert(err, IsNil)669 c.Assert(fmt.Sprintf("%s", res.AccessConds), Equals, tt.accessConds, Commentf("wrong access conditions for expr: %s", tt.exprStr))670 c.Assert(fmt.Sprintf("%s", res.RemainedConds), Equals, tt.filterConds, Commentf("wrong filter conditions for expr: %s", tt.exprStr))671 got := fmt.Sprintf("%v", res.Ranges)672 c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s", tt.exprStr))673 }674}675func (s *testRangerSuite) TestColumnRange(c *C) {676 defer testleak.AfterTest(c)()677 dom, store, err := newDomainStoreWithBootstrap(c)678 defer func() {679 dom.Close()680 store.Close()681 }()682 c.Assert(err, IsNil)683 testKit := testkit.NewTestKit(c, store)684 testKit.MustExec("use test")685 testKit.MustExec("drop table if exists t")686 testKit.MustExec("create table t(a int, b double, c float(3, 2), d varchar(3), e bigint unsigned)")687 tests := []struct {688 colPos int689 exprStr string690 accessConds string691 filterConds string692 resultStr string693 }{694 {695 colPos: 0,696 exprStr: "a = 1 and b > 1",697 accessConds: "[eq(test.t.a, 1)]",698 filterConds: "[gt(test.t.b, 1)]",699 resultStr: "[[1,1]]",700 },701 {702 colPos: 1,703 exprStr: "b > 1",704 accessConds: "[gt(test.t.b, 1)]",705 filterConds: "[]",706 resultStr: "[(1,+inf]]",707 },708 {709 colPos: 0,710 exprStr: "1 = a",711 accessConds: "[eq(1, test.t.a)]",712 filterConds: "[]",713 resultStr: "[[1,1]]",714 },715 {716 colPos: 0,717 exprStr: "a != 1",718 accessConds: "[ne(test.t.a, 1)]",719 filterConds: "[]",720 resultStr: "[[-inf,1) (1,+inf]]",721 },722 {723 colPos: 0,724 exprStr: "1 != a",725 accessConds: "[ne(1, test.t.a)]",726 filterConds: "[]",727 resultStr: "[[-inf,1) (1,+inf]]",728 },729 {730 colPos: 0,731 exprStr: "a > 1",732 accessConds: "[gt(test.t.a, 1)]",733 filterConds: "[]",734 resultStr: "[(1,+inf]]",735 },736 {737 colPos: 0,738 exprStr: "1 < a",739 accessConds: "[lt(1, test.t.a)]",740 filterConds: "[]",741 resultStr: "[(1,+inf]]",742 },743 {744 colPos: 0,745 exprStr: "a >= 1",746 accessConds: "[ge(test.t.a, 1)]",747 filterConds: "[]",748 resultStr: "[[1,+inf]]",749 },750 {751 colPos: 0,752 exprStr: "1 <= a",753 accessConds: "[le(1, test.t.a)]",754 filterConds: "[]",755 resultStr: "[[1,+inf]]",756 },757 {758 colPos: 0,759 exprStr: "a < 1",760 accessConds: "[lt(test.t.a, 1)]",761 filterConds: "[]",762 resultStr: "[[-inf,1)]",763 },764 {765 colPos: 0,766 exprStr: "1 > a",767 accessConds: "[gt(1, test.t.a)]",768 filterConds: "[]",769 resultStr: "[[-inf,1)]",770 },771 {772 colPos: 0,773 exprStr: "a <= 1",774 accessConds: "[le(test.t.a, 1)]",775 filterConds: "[]",776 resultStr: "[[-inf,1]]",777 },778 {779 colPos: 0,780 exprStr: "1 >= a",781 accessConds: "[ge(1, test.t.a)]",782 filterConds: "[]",783 resultStr: "[[-inf,1]]",784 },785 {786 colPos: 0,787 exprStr: "(a)",788 accessConds: "[test.t.a]",789 filterConds: "[]",790 resultStr: "[[-inf,0) (0,+inf]]",791 },792 {793 colPos: 0,794 exprStr: "a in (1, 3, NULL, 2)",795 accessConds: "[in(test.t.a, 1, 3, <nil>, 2)]",796 filterConds: "[]",797 resultStr: "[[1,1] [2,2] [3,3]]",798 },799 {800 colPos: 0,801 exprStr: `a IN (8,8,81,45)`,802 accessConds: "[in(test.t.a, 8, 8, 81, 45)]",803 filterConds: "[]",804 resultStr: `[[8,8] [45,45] [81,81]]`,805 },806 {807 colPos: 0,808 exprStr: "a between 1 and 2",809 accessConds: "[ge(test.t.a, 1) le(test.t.a, 2)]",810 filterConds: "[]",811 resultStr: "[[1,2]]",812 },813 {814 colPos: 0,815 exprStr: "a not between 1 and 2",816 accessConds: "[or(lt(test.t.a, 1), gt(test.t.a, 2))]",817 filterConds: "[]",818 resultStr: "[[-inf,1) (2,+inf]]",819 },820 //{821 // `a > null` will be converted to `castAsString(a) > null` which can not be extracted as access condition.822 // exprStr: "a not between null and 0",823 // resultStr[(0,+inf]]824 //},825 {826 colPos: 0,827 exprStr: "a between 2 and 1",828 accessConds: "[ge(test.t.a, 2) le(test.t.a, 1)]",829 filterConds: "[]",830 resultStr: "[]",831 },832 {833 colPos: 0,834 exprStr: "a not between 2 and 1",835 accessConds: "[or(lt(test.t.a, 2), gt(test.t.a, 1))]",836 filterConds: "[]",837 resultStr: "[[-inf,+inf]]",838 },839 {840 colPos: 0,841 exprStr: "a IS NULL",842 accessConds: "[isnull(test.t.a)]",843 filterConds: "[]",844 resultStr: "[[NULL,NULL]]",845 },846 {847 colPos: 0,848 exprStr: "a IS NOT NULL",849 accessConds: "[not(isnull(test.t.a))]",850 filterConds: "[]",851 resultStr: "[[-inf,+inf]]",852 },853 {854 colPos: 0,855 exprStr: "a IS TRUE",856 accessConds: "[istrue(test.t.a)]",857 filterConds: "[]",858 resultStr: "[[-inf,0) (0,+inf]]",859 },860 {861 colPos: 0,862 exprStr: "a IS NOT TRUE",863 accessConds: "[not(istrue(test.t.a))]",864 filterConds: "[]",865 resultStr: "[[NULL,NULL] [0,0]]",866 },867 {868 colPos: 0,869 exprStr: "a IS FALSE",870 accessConds: "[isfalse(test.t.a)]",871 filterConds: "[]",872 resultStr: "[[0,0]]",873 },874 {875 colPos: 0,876 exprStr: "a IS NOT FALSE",877 accessConds: "[not(isfalse(test.t.a))]",878 filterConds: "[]",879 resultStr: "[[NULL,0) (0,+inf]]",880 },881 {882 colPos: 1,883 exprStr: `b in (1, '2.1')`,884 accessConds: "[in(test.t.b, 1, 2.1)]",885 filterConds: "[]",886 resultStr: "[[1,1] [2.1,2.1]]",887 },888 {889 colPos: 0,890 exprStr: `a > 9223372036854775807`,891 accessConds: "[gt(test.t.a, 9223372036854775807)]",892 filterConds: "[]",893 resultStr: "[(9223372036854775807,+inf]]",894 },895 {896 colPos: 2,897 exprStr: `c > 111.11111111`,898 accessConds: "[gt(test.t.c, 111.11111111)]",899 filterConds: "[]",900 resultStr: "[[111.111115,+inf]]",901 },902 {903 colPos: 3,904 exprStr: `d > 'aaaaaaaaaaaaaa'`,905 accessConds: "[gt(test.t.d, aaaaaaaaaaaaaa)]",906 filterConds: "[]",907 resultStr: "[(\"aaaaaaaaaaaaaa\",+inf]]",908 },909 {910 colPos: 4,911 exprStr: `e > 18446744073709500000`,912 accessConds: "[gt(test.t.e, 18446744073709500000)]",913 filterConds: "[]",914 resultStr: "[(18446744073709500000,+inf]]",915 },916 }917 for _, tt := range tests {918 sql := "select * from t where " + tt.exprStr919 ctx := testKit.Se.(sessionctx.Context)920 stmts, err := session.Parse(ctx, sql)921 c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprStr))922 c.Assert(stmts, HasLen, 1)923 is := domain.GetDomain(ctx).InfoSchema()924 err = plannercore.Preprocess(ctx, stmts[0], is)925 c.Assert(err, IsNil, Commentf("error %v, for resolve name, expr %s", err, tt.exprStr))926 p, err := plannercore.BuildLogicalPlan(ctx, stmts[0], is)927 c.Assert(err, IsNil, Commentf("error %v, for build plan, expr %s", err, tt.exprStr))928 sel := p.(plannercore.LogicalPlan).Children()[0].(*plannercore.LogicalSelection)929 ds, ok := sel.Children()[0].(*plannercore.DataSource)930 c.Assert(ok, IsTrue, Commentf("expr:%v", tt.exprStr))931 conds := make([]expression.Expression, 0, len(sel.Conditions))932 for _, cond := range sel.Conditions {933 conds = append(conds, expression.PushDownNot(ctx, cond, false))934 }935 col := expression.ColInfo2Col(sel.Schema().Columns, ds.TableInfo().Columns[tt.colPos])936 c.Assert(col, NotNil)937 conds = ranger.ExtractAccessConditionsForColumn(conds, col.UniqueID)938 c.Assert(fmt.Sprintf("%s", conds), Equals, tt.accessConds, Commentf("wrong access conditions for expr: %s", tt.exprStr))939 result, err := ranger.BuildColumnRange(conds, new(stmtctx.StatementContext), col.RetType)940 c.Assert(err, IsNil)941 got := fmt.Sprintf("%v", result)942 c.Assert(got, Equals, tt.resultStr, Commentf("different for expr %s, col: %v", tt.exprStr, col))943 }944}945func (s *testRangerSuite) TestIndexRangeElimininatedProjection(c *C) {946 defer testleak.AfterTest(c)()947 dom, store, err := newDomainStoreWithBootstrap(c)948 defer func() {949 dom.Close()950 store.Close()951 }()952 c.Assert(err, IsNil)953 testKit := testkit.NewTestKit(c, store)954 testKit.MustExec("use test")955 testKit.MustExec("drop table if exists t")956 testKit.MustExec("create table t(a int not null, b int not null, primary key(a,b))")957 testKit.MustExec("insert into t values(1,2)")958 testKit.MustExec("analyze table t")959 testKit.MustQuery("explain select * from (select * from t union all select ifnull(a,b), b from t) sub where a > 0").Check(testkit.Rows(960 "Union_11 2.00 root ",961 "├─IndexReader_14 1.00 root index:IndexScan_13",962 "│ └─IndexScan_13 1.00 cop table:t, index:a, b, range:(0,+inf], keep order:false",963 "└─IndexReader_17 1.00 root index:IndexScan_16",964 " └─IndexScan_16 1.00 cop table:t, index:a, b, range:(0,+inf], keep order:false",965 ))966 testKit.MustQuery("select * from (select * from t union all select ifnull(a,b), b from t) sub where a > 0").Check(testkit.Rows(967 "1 2",...

Full Screen

Full Screen

filter_test.go

Source:filter_test.go Github

copy

Full Screen

...10 "github.com/bitcoinsv/bsvd/wire"11 "github.com/bitcoinsv/bsvutil"12 "github.com/bitcoinsv/bsvutil/bloom"13)14// TestFilterLarge ensures a maximum sized filter can be created.15func TestFilterLarge(t *testing.T) {16 f := bloom.NewFilter(100000000, 0, 0.01, wire.BloomUpdateNone)17 if len(f.MsgFilterLoad().Filter) > wire.MaxFilterLoadFilterSize {18 t.Errorf("TestFilterLarge test failed: %d > %d",19 len(f.MsgFilterLoad().Filter), wire.MaxFilterLoadFilterSize)20 }21}22// TestFilterLoad ensures loading and unloading of a filter pass.23func TestFilterLoad(t *testing.T) {24 merkle := wire.MsgFilterLoad{}25 f := bloom.LoadFilter(&merkle)26 if !f.IsLoaded() {27 t.Errorf("TestFilterLoad IsLoaded test failed: want %v got %v",28 true, !f.IsLoaded())29 return30 }31 f.Unload()32 if f.IsLoaded() {33 t.Errorf("TestFilterLoad IsLoaded test failed: want %v got %v",34 f.IsLoaded(), false)35 return36 }37}38// TestFilterInsert ensures inserting data into the filter causes that data39// to be matched and the resulting serialized MsgFilterLoad is the expected40// value.41func TestFilterInsert(t *testing.T) {42 var tests = []struct {43 hex string44 insert bool45 }{46 {"99108ad8ed9bb6274d3980bab5a85c048f0950c8", true},47 {"19108ad8ed9bb6274d3980bab5a85c048f0950c8", false},48 {"b5a2c786d9ef4658287ced5914b37a1b4aa32eee", true},49 {"b9300670b4c5366e95b2699e8b18bc75e5f729c5", true},50 }51 f := bloom.NewFilter(3, 0, 0.01, wire.BloomUpdateAll)52 for i, test := range tests {53 data, err := hex.DecodeString(test.hex)54 if err != nil {55 t.Errorf("TestFilterInsert DecodeString failed: %v\n", err)56 return57 }58 if test.insert {59 f.Add(data)60 }61 result := f.Matches(data)62 if test.insert != result {63 t.Errorf("TestFilterInsert Matches test #%d failure: got %v want %v\n",64 i, result, test.insert)65 return66 }67 }68 want, err := hex.DecodeString("03614e9b050000000000000001")69 if err != nil {70 t.Errorf("TestFilterInsert DecodeString failed: %v\n", err)71 return72 }73 got := bytes.NewBuffer(nil)74 err = f.MsgFilterLoad().BsvEncode(got, wire.ProtocolVersion, wire.LatestEncoding)75 if err != nil {76 t.Errorf("TestFilterInsert BsvDecode failed: %v\n", err)77 return78 }79 if !bytes.Equal(got.Bytes(), want) {80 t.Errorf("TestFilterInsert failure: got %v want %v\n",81 got.Bytes(), want)82 return83 }84}85// TestFilterFPRange checks that new filters made with out of range86// false positive targets result in either max or min false positive rates.87func TestFilterFPRange(t *testing.T) {88 tests := []struct {89 name string90 hash string91 want string92 filter *bloom.Filter93 }{94 {95 name: "fprates > 1 should be clipped at 1",96 hash: "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041",97 want: "00000000000000000001",98 filter: bloom.NewFilter(1, 0, 20.9999999769, wire.BloomUpdateAll),99 },100 {101 name: "fprates less than 1e-9 should be clipped at min",102 hash: "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041",103 want: "0566d97a91a91b0000000000000001",104 filter: bloom.NewFilter(1, 0, 0, wire.BloomUpdateAll),105 },106 {107 name: "negative fprates should be clipped at min",108 hash: "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041",109 want: "0566d97a91a91b0000000000000001",110 filter: bloom.NewFilter(1, 0, -1, wire.BloomUpdateAll),111 },112 }113 for _, test := range tests {114 // Convert test input to appropriate types.115 hash, err := chainhash.NewHashFromStr(test.hash)116 if err != nil {117 t.Errorf("NewHashFromStr unexpected error: %v", err)118 continue119 }120 want, err := hex.DecodeString(test.want)121 if err != nil {122 t.Errorf("DecodeString unexpected error: %v\n", err)123 continue124 }125 // Add the test hash to the bloom filter and ensure the126 // filter serializes to the expected bytes.127 f := test.filter128 f.AddHash(hash)129 got := bytes.NewBuffer(nil)130 err = f.MsgFilterLoad().BsvEncode(got, wire.ProtocolVersion, wire.LatestEncoding)131 if err != nil {132 t.Errorf("BsvDecode unexpected error: %v\n", err)133 continue134 }135 if !bytes.Equal(got.Bytes(), want) {136 t.Errorf("serialized filter mismatch: got %x want %x\n",137 got.Bytes(), want)138 continue139 }140 }141}142// TestFilterInsert ensures inserting data into the filter with a tweak causes143// that data to be matched and the resulting serialized MsgFilterLoad is the144// expected value.145func TestFilterInsertWithTweak(t *testing.T) {146 var tests = []struct {147 hex string148 insert bool149 }{150 {"99108ad8ed9bb6274d3980bab5a85c048f0950c8", true},151 {"19108ad8ed9bb6274d3980bab5a85c048f0950c8", false},152 {"b5a2c786d9ef4658287ced5914b37a1b4aa32eee", true},153 {"b9300670b4c5366e95b2699e8b18bc75e5f729c5", true},154 }155 f := bloom.NewFilter(3, 2147483649, 0.01, wire.BloomUpdateAll)156 for i, test := range tests {157 data, err := hex.DecodeString(test.hex)158 if err != nil {159 t.Errorf("TestFilterInsertWithTweak DecodeString failed: %v\n", err)160 return161 }162 if test.insert {163 f.Add(data)164 }165 result := f.Matches(data)166 if test.insert != result {167 t.Errorf("TestFilterInsertWithTweak Matches test #%d failure: got %v want %v\n",168 i, result, test.insert)169 return170 }171 }172 want, err := hex.DecodeString("03ce4299050000000100008001")173 if err != nil {174 t.Errorf("TestFilterInsertWithTweak DecodeString failed: %v\n", err)175 return176 }177 got := bytes.NewBuffer(nil)178 err = f.MsgFilterLoad().BsvEncode(got, wire.ProtocolVersion, wire.LatestEncoding)179 if err != nil {180 t.Errorf("TestFilterInsertWithTweak BsvDecode failed: %v\n", err)181 return182 }183 if !bytes.Equal(got.Bytes(), want) {184 t.Errorf("TestFilterInsertWithTweak failure: got %v want %v\n",185 got.Bytes(), want)186 return187 }188}189// TestFilterInsertKey ensures inserting public keys and addresses works as190// expected.191func TestFilterInsertKey(t *testing.T) {192 secret := "5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C"193 wif, err := bsvutil.DecodeWIF(secret)194 if err != nil {195 t.Errorf("TestFilterInsertKey DecodeWIF failed: %v", err)196 return197 }198 f := bloom.NewFilter(2, 0, 0.001, wire.BloomUpdateAll)199 f.Add(wif.SerializePubKey())200 f.Add(bsvutil.Hash160(wif.SerializePubKey()))201 want, err := hex.DecodeString("038fc16b080000000000000001")202 if err != nil {203 t.Errorf("TestFilterInsertWithTweak DecodeString failed: %v\n", err)204 return205 }206 got := bytes.NewBuffer(nil)207 err = f.MsgFilterLoad().BsvEncode(got, wire.ProtocolVersion, wire.LatestEncoding)208 if err != nil {209 t.Errorf("TestFilterInsertWithTweak BsvDecode failed: %v\n", err)210 return211 }212 if !bytes.Equal(got.Bytes(), want) {213 t.Errorf("TestFilterInsertWithTweak failure: got %v want %v\n",214 got.Bytes(), want)215 return216 }217}218func TestFilterBloomMatch(t *testing.T) {219 str := "01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e" +220 "88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7" +221 "c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d2" +222 "7d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee" +223 "51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5e" +224 "eef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c33" +225 "9ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3" +226 "bc6e2754dbcff1988ac2f15de00000000001976a914a266436d296554760" +227 "8b9e15d9032a7b9d64fa43188ac00000000"228 strBytes, err := hex.DecodeString(str)229 if err != nil {230 t.Errorf("TestFilterBloomMatch DecodeString failure: %v", err)231 return232 }233 tx, err := bsvutil.NewTxFromBytes(strBytes)234 if err != nil {235 t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err)236 return237 }238 spendingTxBytes := []byte{0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f,239 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6,240 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27,241 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f,242 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30,243 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce,244 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57,245 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0,246 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c,247 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00,248 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e,249 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27,250 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01,251 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10,252 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9,253 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5,254 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff,255 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf,256 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9,257 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb,258 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b,259 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76,260 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07,261 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0,262 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8,263 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14,264 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51,265 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70,266 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00}267 spendingTx, err := bsvutil.NewTxFromBytes(spendingTxBytes)268 if err != nil {269 t.Errorf("TestFilterBloomMatch NewTxFromBytes failure: %v", err)270 return271 }272 f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)273 inputStr := "b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"274 hash, err := chainhash.NewHashFromStr(inputStr)275 if err != nil {276 t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)277 return278 }279 f.AddHash(hash)280 if !f.MatchTxAndUpdate(tx) {281 t.Errorf("TestFilterBloomMatch didn't match hash %s", inputStr)282 }283 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)284 inputStr = "6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4"285 hashBytes, err := hex.DecodeString(inputStr)286 if err != nil {287 t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)288 return289 }290 f.Add(hashBytes)291 if !f.MatchTxAndUpdate(tx) {292 t.Errorf("TestFilterBloomMatch didn't match hash %s", inputStr)293 }294 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)295 inputStr = "30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065" +296 "f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643" +297 "ac4cb7cb3c462aced7f14711a01"298 hashBytes, err = hex.DecodeString(inputStr)299 if err != nil {300 t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)301 return302 }303 f.Add(hashBytes)304 if !f.MatchTxAndUpdate(tx) {305 t.Errorf("TestFilterBloomMatch didn't match input signature %s", inputStr)306 }307 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)308 inputStr = "046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95" +309 "c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe" +310 "76036c339"311 hashBytes, err = hex.DecodeString(inputStr)312 if err != nil {313 t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)314 return315 }316 f.Add(hashBytes)317 if !f.MatchTxAndUpdate(tx) {318 t.Errorf("TestFilterBloomMatch didn't match input pubkey %s", inputStr)319 }320 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)321 inputStr = "04943fdd508053c75000106d3bc6e2754dbcff19"322 hashBytes, err = hex.DecodeString(inputStr)323 if err != nil {324 t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)325 return326 }327 f.Add(hashBytes)328 if !f.MatchTxAndUpdate(tx) {329 t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr)330 }331 if !f.MatchTxAndUpdate(spendingTx) {332 t.Errorf("TestFilterBloomMatch spendingTx didn't match output address %s", inputStr)333 }334 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)335 inputStr = "a266436d2965547608b9e15d9032a7b9d64fa431"336 hashBytes, err = hex.DecodeString(inputStr)337 if err != nil {338 t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)339 return340 }341 f.Add(hashBytes)342 if !f.MatchTxAndUpdate(tx) {343 t.Errorf("TestFilterBloomMatch didn't match output address %s", inputStr)344 }345 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)346 inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"347 hash, err = chainhash.NewHashFromStr(inputStr)348 if err != nil {349 t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)350 return351 }352 outpoint := wire.NewOutPoint(hash, 0)353 f.AddOutPoint(outpoint)354 if !f.MatchTxAndUpdate(tx) {355 t.Errorf("TestFilterBloomMatch didn't match outpoint %s", inputStr)356 }357 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)358 inputStr = "00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436"359 hash, err = chainhash.NewHashFromStr(inputStr)360 if err != nil {361 t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)362 return363 }364 f.AddHash(hash)365 if f.MatchTxAndUpdate(tx) {366 t.Errorf("TestFilterBloomMatch matched hash %s", inputStr)367 }368 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)369 inputStr = "0000006d2965547608b9e15d9032a7b9d64fa431"370 hashBytes, err = hex.DecodeString(inputStr)371 if err != nil {372 t.Errorf("TestFilterBloomMatch DecodeString failed: %v\n", err)373 return374 }375 f.Add(hashBytes)376 if f.MatchTxAndUpdate(tx) {377 t.Errorf("TestFilterBloomMatch matched address %s", inputStr)378 }379 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)380 inputStr = "90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"381 hash, err = chainhash.NewHashFromStr(inputStr)382 if err != nil {383 t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)384 return385 }386 outpoint = wire.NewOutPoint(hash, 1)387 f.AddOutPoint(outpoint)388 if f.MatchTxAndUpdate(tx) {389 t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr)390 }391 f = bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)392 inputStr = "000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"393 hash, err = chainhash.NewHashFromStr(inputStr)394 if err != nil {395 t.Errorf("TestFilterBloomMatch NewHashFromStr failed: %v\n", err)396 return397 }398 outpoint = wire.NewOutPoint(hash, 0)399 f.AddOutPoint(outpoint)400 if f.MatchTxAndUpdate(tx) {401 t.Errorf("TestFilterBloomMatch matched outpoint %s", inputStr)402 }403}404func TestFilterInsertUpdateNone(t *testing.T) {405 f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateNone)406 // Add the generation pubkey407 inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" +408 "876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" +409 "2252247d97a46a91"410 inputBytes, err := hex.DecodeString(inputStr)411 if err != nil {412 t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err)413 return414 }415 f.Add(inputBytes)416 // Add the output address for the 4th transaction417 inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21"418 inputBytes, err = hex.DecodeString(inputStr)419 if err != nil {420 t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err)421 return422 }423 f.Add(inputBytes)424 inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"425 hash, err := chainhash.NewHashFromStr(inputStr)426 if err != nil {427 t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err)428 return429 }430 outpoint := wire.NewOutPoint(hash, 0)431 if f.MatchesOutPoint(outpoint) {432 t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)433 return434 }435 inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"436 hash, err = chainhash.NewHashFromStr(inputStr)437 if err != nil {438 t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err)439 return440 }441 outpoint = wire.NewOutPoint(hash, 0)442 if f.MatchesOutPoint(outpoint) {443 t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)444 return445 }446}447func TestFilterInsertP2PubKeyOnly(t *testing.T) {448 blockStr := "0100000082bb869cf3a793432a66e826e05a6fc37469f8efb7421dc" +449 "880670100000000007f16c5962e8bd963659c793ce370d95f093bc7e367" +450 "117b3c30c1f8fdd0d9728776381b4d4c86041b554b85290701000000010" +451 "00000000000000000000000000000000000000000000000000000000000" +452 "0000ffffffff07044c86041b0136ffffffff0100f2052a0100000043410" +453 "4eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c876f2" +454 "c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a22522" +455 "47d97a46a91ac000000000100000001bcad20a6a29827d1424f08989255" +456 "120bf7f3e9e3cdaaa6bb31b0737fe048724300000000494830450220356" +457 "e834b046cadc0f8ebb5a8a017b02de59c86305403dad52cd77b55af062e" +458 "a10221009253cd6c119d4729b77c978e1e2aa19f5ea6e0e52b3f16e32fa" +459 "608cd5bab753901ffffffff02008d380c010000001976a9142b4b8072ec" +460 "bba129b6453c63e129e643207249ca88ac0065cd1d000000001976a9141" +461 "b8dd13b994bcfc787b32aeadf58ccb3615cbd5488ac0000000001000000" +462 "03fdacf9b3eb077412e7a968d2e4f11b9a9dee312d666187ed77ee7d26a" +463 "f16cb0b000000008c493046022100ea1608e70911ca0de5af51ba57ad23" +464 "b9a51db8d28f82c53563c56a05c20f5a87022100a8bdc8b4a8acc8634c6" +465 "b420410150775eb7f2474f5615f7fccd65af30f310fbf01410465fdf49e" +466 "29b06b9a1582287b6279014f834edc317695d125ef623c1cc3aaece245b" +467 "d69fcad7508666e9c74a49dc9056d5fc14338ef38118dc4afae5fe2c585" +468 "caffffffff309e1913634ecb50f3c4f83e96e70b2df071b497b8973a3e7" +469 "5429df397b5af83000000004948304502202bdb79c596a9ffc24e96f438" +470 "6199aba386e9bc7b6071516e2b51dda942b3a1ed022100c53a857e76b72" +471 "4fc14d45311eac5019650d415c3abb5428f3aae16d8e69bec2301ffffff" +472 "ff2089e33491695080c9edc18a428f7d834db5b6d372df13ce2b1b0e0cb" +473 "cb1e6c10000000049483045022100d4ce67c5896ee251c810ac1ff9cecc" +474 "d328b497c8f553ab6e08431e7d40bad6b5022033119c0c2b7d792d31f11" +475 "87779c7bd95aefd93d90a715586d73801d9b47471c601ffffffff010071" +476 "4460030000001976a914c7b55141d097ea5df7a0ed330cf794376e53ec8" +477 "d88ac0000000001000000045bf0e214aa4069a3e792ecee1e1bf0c1d397" +478 "cde8dd08138f4b72a00681743447000000008b48304502200c45de8c4f3" +479 "e2c1821f2fc878cba97b1e6f8807d94930713aa1c86a67b9bf1e4022100" +480 "8581abfef2e30f957815fc89978423746b2086375ca8ecf359c85c2a5b7" +481 "c88ad01410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf64852" +482 "61c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270efb1d" +483 "3ae37079b794a92d7ec95ffffffffd669f7d7958d40fc59d2253d88e0f2" +484 "48e29b599c80bbcec344a83dda5f9aa72c000000008a473044022078124" +485 "c8beeaa825f9e0b30bff96e564dd859432f2d0cb3b72d3d5d93d38d7e93" +486 "0220691d233b6c0f995be5acb03d70a7f7a65b6bc9bdd426260f38a1346" +487 "669507a3601410462bb73f76ca0994fcb8b4271e6fb7561f5c0f9ca0cf6" +488 "485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f4d87270e" +489 "fb1d3ae37079b794a92d7ec95fffffffff878af0d93f5229a68166cf051" +490 "fd372bb7a537232946e0a46f53636b4dafdaa4000000008c49304602210" +491 "0c717d1714551663f69c3c5759bdbb3a0fcd3fab023abc0e522fe6440de" +492 "35d8290221008d9cbe25bffc44af2b18e81c58eb37293fd7fe1c2e7b46f" +493 "c37ee8c96c50ab1e201410462bb73f76ca0994fcb8b4271e6fb7561f5c0" +494 "f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018ffd6238f" +495 "4d87270efb1d3ae37079b794a92d7ec95ffffffff27f2b668859cd7f2f8" +496 "94aa0fd2d9e60963bcd07c88973f425f999b8cbfd7a1e2000000008c493" +497 "046022100e00847147cbf517bcc2f502f3ddc6d284358d102ed20d47a8a" +498 "a788a62f0db780022100d17b2d6fa84dcaf1c95d88d7e7c30385aecf415" +499 "588d749afd3ec81f6022cecd701410462bb73f76ca0994fcb8b4271e6fb" +500 "7561f5c0f9ca0cf6485261c4a0dc894f4ab844c6cdfb97cd0b60ffb5018" +501 "ffd6238f4d87270efb1d3ae37079b794a92d7ec95ffffffff0100c817a8" +502 "040000001976a914b6efd80d99179f4f4ff6f4dd0a007d018c385d2188a" +503 "c000000000100000001834537b2f1ce8ef9373a258e10545ce5a50b758d" +504 "f616cd4356e0032554ebd3c4000000008b483045022100e68f422dd7c34" +505 "fdce11eeb4509ddae38201773dd62f284e8aa9d96f85099d0b002202243" +506 "bd399ff96b649a0fad05fa759d6a882f0af8c90cf7632c2840c29070aec" +507 "20141045e58067e815c2f464c6a2a15f987758374203895710c2d452442" +508 "e28496ff38ba8f5fd901dc20e29e88477167fe4fc299bf818fd0d9e1632" +509 "d467b2a3d9503b1aaffffffff0280d7e636030000001976a914f34c3e10" +510 "eb387efe872acb614c89e78bfca7815d88ac404b4c00000000001976a91" +511 "4a84e272933aaf87e1715d7786c51dfaeb5b65a6f88ac00000000010000" +512 "000143ac81c8e6f6ef307dfe17f3d906d999e23e0189fda838c5510d850" +513 "927e03ae7000000008c4930460221009c87c344760a64cb8ae6685a3eec" +514 "2c1ac1bed5b88c87de51acd0e124f266c16602210082d07c037359c3a25" +515 "7b5c63ebd90f5a5edf97b2ac1c434b08ca998839f346dd40141040ba7e5" +516 "21fa7946d12edbb1d1e95a15c34bd4398195e86433c92b431cd315f455f" +517 "e30032ede69cad9d1e1ed6c3c4ec0dbfced53438c625462afb792dcb098" +518 "544bffffffff0240420f00000000001976a9144676d1b820d63ec272f19" +519 "00d59d43bc6463d96f888ac40420f00000000001976a914648d04341d00" +520 "d7968b3405c034adc38d4d8fb9bd88ac00000000010000000248cc91750" +521 "1ea5c55f4a8d2009c0567c40cfe037c2e71af017d0a452ff705e3f10000" +522 "00008b483045022100bf5fdc86dc5f08a5d5c8e43a8c9d5b1ed8c65562e" +523 "280007b52b133021acd9acc02205e325d613e555f772802bf413d36ba80" +524 "7892ed1a690a77811d3033b3de226e0a01410429fa713b124484cb2bd7b" +525 "5557b2c0b9df7b2b1fee61825eadc5ae6c37a9920d38bfccdc7dc3cb0c4" +526 "7d7b173dbc9db8d37db0a33ae487982c59c6f8606e9d1791ffffffff41e" +527 "d70551dd7e841883ab8f0b16bf04176b7d1480e4f0af9f3d4c3595768d0" +528 "68000000008b4830450221008513ad65187b903aed1102d1d0c47688127" +529 "658c51106753fed0151ce9c16b80902201432b9ebcb87bd04ceb2de6603" +530 "5fbbaf4bf8b00d1cfe41f1a1f7338f9ad79d210141049d4cf80125bf50b" +531 "e1709f718c07ad15d0fc612b7da1f5570dddc35f2a352f0f27c978b0682" +532 "0edca9ef982c35fda2d255afba340068c5035552368bc7200c1488fffff" +533 "fff0100093d00000000001976a9148edb68822f1ad580b043c7b3df2e40" +534 "0f8699eb4888ac00000000"535 blockBytes, err := hex.DecodeString(blockStr)536 if err != nil {537 t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err)538 return539 }540 block, err := bsvutil.NewBlockFromBytes(blockBytes)541 if err != nil {542 t.Errorf("TestFilterInsertP2PubKeyOnly NewBlockFromBytes failed: %v", err)543 return544 }545 f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateP2PubkeyOnly)546 // Generation pubkey547 inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" +548 "876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" +549 "2252247d97a46a91"550 inputBytes, err := hex.DecodeString(inputStr)551 if err != nil {552 t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err)553 return554 }555 f.Add(inputBytes)556 // Output address of 4th transaction557 inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21"558 inputBytes, err = hex.DecodeString(inputStr)559 if err != nil {560 t.Errorf("TestFilterInsertP2PubKeyOnly DecodeString failed: %v", err)561 return562 }563 f.Add(inputBytes)564 // Ignore return value -- this is just used to update the filter.565 _, _ = bloom.NewMerkleBlock(block, f)566 // We should match the generation pubkey567 inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"568 hash, err := chainhash.NewHashFromStr(inputStr)569 if err != nil {570 t.Errorf("TestMerkleBlockP2PubKeyOnly NewHashFromStr failed: %v", err)571 return572 }573 outpoint := wire.NewOutPoint(hash, 0)574 if !f.MatchesOutPoint(outpoint) {575 t.Errorf("TestMerkleBlockP2PubKeyOnly didn't match the generation "+576 "outpoint %s", inputStr)577 return578 }579 // We should not match the 4th transaction, which is not p2pk580 inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"581 hash, err = chainhash.NewHashFromStr(inputStr)582 if err != nil {583 t.Errorf("TestMerkleBlockP2PubKeyOnly NewHashFromStr failed: %v", err)584 return585 }586 outpoint = wire.NewOutPoint(hash, 0)587 if f.MatchesOutPoint(outpoint) {588 t.Errorf("TestMerkleBlockP2PubKeyOnly matched outpoint %s", inputStr)589 return590 }591}592func TestFilterReload(t *testing.T) {593 f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)594 bFilter := bloom.LoadFilter(f.MsgFilterLoad())595 if bFilter.MsgFilterLoad() == nil {596 t.Errorf("TestFilterReload LoadFilter test failed")597 return598 }599 bFilter.Reload(nil)600 if bFilter.MsgFilterLoad() != nil {601 t.Errorf("TestFilterReload Reload test failed")602 }603}...

Full Screen

Full Screen

matcher_test.go

Source:matcher_test.go Github

copy

Full Screen

...22 "time"23 "github.com/ethereum/go-ethereum/common"24)25const testSectionSize = 409626// Tests that wildcard filter rules (nil) can be specified and are handled well.27func TestMatcherWildcards(t *testing.T) {28 matcher := NewMatcher(testSectionSize, [][][]byte{29 {common.Address{}.Bytes(), common.Address{0x01}.Bytes()}, // Default address is not a wildcard30 {common.Hash{}.Bytes(), common.Hash{0x01}.Bytes()}, // Default hash is not a wildcard31 {common.Hash{0x01}.Bytes()}, // Plain rule, sanity check32 {common.Hash{0x01}.Bytes(), nil}, // Wildcard suffix, drop rule33 {nil, common.Hash{0x01}.Bytes()}, // Wildcard prefix, drop rule34 {nil, nil}, // Wildcard combo, drop rule35 {}, // Inited wildcard rule, drop rule36 nil, // Proper wildcard rule, drop rule37 })38 if len(matcher.filters) != 3 {39 t.Fatalf("filter system size mismatch: have %d, want %d", len(matcher.filters), 3)40 }41 if len(matcher.filters[0]) != 2 {42 t.Fatalf("address clause size mismatch: have %d, want %d", len(matcher.filters[0]), 2)43 }44 if len(matcher.filters[1]) != 2 {45 t.Fatalf("combo topic clause size mismatch: have %d, want %d", len(matcher.filters[1]), 2)46 }47 if len(matcher.filters[2]) != 1 {48 t.Fatalf("singletone topic clause size mismatch: have %d, want %d", len(matcher.filters[2]), 1)49 }50}51// Tests the matcher pipeline on a single continuous workflow without interrupts.52func TestMatcherContinuous(t *testing.T) {53 testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 0, 100000, false, 75)54 testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 0, 100000, false, 81)55 testMatcherDiffBatches(t, [][]bloomIndexes{{{4, 8, 11}, {7, 8, 17}}, {{9, 9, 12}, {15, 20, 13}}, {{18, 15, 15}, {12, 10, 4}}}, 0, 10000, false, 36)56}57// Tests the matcher pipeline on a constantly interrupted and resumed work pattern58// with the aim of ensuring data items are requested only once.59func TestMatcherIntermittent(t *testing.T) {60 testMatcherDiffBatches(t, [][]bloomIndexes{{{10, 20, 30}}}, 0, 100000, true, 75)61 testMatcherDiffBatches(t, [][]bloomIndexes{{{32, 3125, 100}}, {{40, 50, 10}}}, 0, 100000, true, 81)62 testMatcherDiffBatches(t, [][]bloomIndexes{{{4, 8, 11}, {7, 8, 17}}, {{9, 9, 12}, {15, 20, 13}}, {{18, 15, 15}, {12, 10, 4}}}, 0, 10000, true, 36)63}64// Tests the matcher pipeline on random input to hopefully catch anomalies.65func TestMatcherRandom(t *testing.T) {66 for i := 0; i < 10; i++ {67 testMatcherBothModes(t, makeRandomIndexes([]int{1}, 50), 0, 10000, 0)68 testMatcherBothModes(t, makeRandomIndexes([]int{3}, 50), 0, 10000, 0)69 testMatcherBothModes(t, makeRandomIndexes([]int{2, 2, 2}, 20), 0, 10000, 0)70 testMatcherBothModes(t, makeRandomIndexes([]int{5, 5, 5}, 50), 0, 10000, 0)71 testMatcherBothModes(t, makeRandomIndexes([]int{4, 4, 4}, 20), 0, 10000, 0)72 }73}74// Tests that the matcher can properly find matches if the starting block is75// shifter from a multiple of 8. This is needed to cover an optimisation with76// bitset matching https://github.com/ethereum/go-ethereum/issues/15309.77func TestMatcherShifted(t *testing.T) {78 // Block 0 always matches in the tests, skip ahead of first 8 blocks with the79 // start to get a potential zero byte in the matcher bitset.80 // To keep the second bitset byte zero, the filter must only match for the first81 // time in block 16, so doing an all-16 bit filter should suffice.82 // To keep the starting block non divisible by 8, block number 9 is the first83 // that would introduce a shift and not match block 0.84 testMatcherBothModes(t, [][]bloomIndexes{{{16, 16, 16}}}, 9, 64, 0)85}86// Tests that matching on everything doesn't crash (special case internally).87func TestWildcardMatcher(t *testing.T) {88 testMatcherBothModes(t, nil, 0, 10000, 0)89}90// makeRandomIndexes generates a random filter system, composed on multiple filter91// criteria, each having one bloom list component for the address and arbitrarily92// many topic bloom list components.93func makeRandomIndexes(lengths []int, max int) [][]bloomIndexes {94 res := make([][]bloomIndexes, len(lengths))95 for i, topics := range lengths {96 res[i] = make([]bloomIndexes, topics)97 for j := 0; j < topics; j++ {98 for k := 0; k < len(res[i][j]); k++ {99 res[i][j][k] = uint(rand.Intn(max-1) + 2)100 }101 }...

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2type Filter interface {3 Test(string) bool4}5type FilterFunc func(string) bool6func (f FilterFunc) Test(s string) bool {7 return f(s)8}9func (f FilterChain) Test(s string) bool {10 for _, filter := range f {11 if !filter.Test(s) {12 }13 }14}15func main() {16 filters = append(filters, FilterFunc(func(s string) bool {17 return len(s) > 518 }))19 filters = append(filters, FilterFunc(func(s string) bool {20 }))21 filters = append(filters, FilterFunc(func(s string) bool {22 }))23 filters = append(filters, FilterFunc(func(s string) bool {24 }))25 filters = append(filters, FilterFunc(func(s string) bool {26 }))27 filters = append(filters, FilterFunc(func(s string) bool {28 }))29 fmt.Println(filters.Test("hello"))30}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1filter := NewFilter()2filter.Test()3filter := NewFilter()4filter.Test()5filter := NewFilter()6filter.Test()7filter := NewFilter()8filter.Test()9filter := NewFilter()10filter.Test()11filter := NewFilter()12filter.Test()13filter := NewFilter()14filter.Test()15filter := NewFilter()16filter.Test()17filter := NewFilter()18filter.Test()19filter := NewFilter()20filter.Test()21filter := NewFilter()22filter.Test()23filter := NewFilter()24filter.Test()25filter := NewFilter()26filter.Test()27filter := NewFilter()28filter.Test()29filter := NewFilter()30filter.Test()31filter := NewFilter()32filter.Test()33filter := NewFilter()34filter.Test()35filter := NewFilter()36filter.Test()37filter := NewFilter()38filter.Test()39filter := NewFilter()40filter.Test()

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var a = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}4 var b = filter.Test(a)5 fmt.Println(b)6}7func Test(a []int) []int {8 for _, v := range a {9 if v%2 == 0 {10 b = append(b, v)11 }12 }13}14func Sum(a []int) int {15 return sum(a)16}17func sum(a []int) int {18 for _, v := range a {19 }20}21import (22func main() {23 var a = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}24 var b = sum.Sum(a)25 fmt.Println(b)26}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var list = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}4 var result = filter.Test(list, func(i int) bool {5 })6 fmt.Println(result)7}8func Test(list []int, callback func(int) bool) []int {9 for _, v := range list {10 if callback(v) {11 result = append(result, v)12 }13 }14}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1func main() {2 filter := Filter{}3 filter.Test()4}5func main() {6 filter := Filter{}7 filter.Test()8}9func main() {10 filter := Filter{}11 filter.Test()12}13func main() {14 filter := Filter{}15 filter.Test()16}17func main() {18 filter := Filter{}19 filter.Test()20}21func main() {22 filter := Filter{}23 filter.Test()24}25func main() {26 filter := Filter{}27 filter.Test()28}29func main() {30 filter := Filter{}31 filter.Test()32}33func main() {34 filter := Filter{}35 filter.Test()36}37func main() {38 filter := Filter{}39 filter.Test()40}41func main() {42 filter := Filter{}43 filter.Test()44}45func main() {46 filter := Filter{}47 filter.Test()48}49func main() {50 filter := Filter{}51 filter.Test()52}53func main() {54 filter := Filter{}55 filter.Test()56}57func main() {58 filter := Filter{}59 filter.Test()60}61func main() {62 filter := Filter{}63 filter.Test()64}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1func main() {2 f := filter.New()3 f.Test()4}5func main() {6 f := filter.New()7 f.Test()8}9func main() {10 f := filter.New()11 f.Test()12}13func main() {14 f := filter.New()15 f.Test()16}17func main() {18 f := filter.New()19 f.Test()20}21func main() {22 f := filter.New()23 f.Test()24}25func main() {26 f := filter.New()27 f.Test()28}29func main() {30 f := filter.New()31 f.Test()32}33func main() {34 f := filter.New()35 f.Test()36}37func main() {38 f := filter.New()39 f.Test()40}41func main() {42 f := filter.New()43 f.Test()44}45func main() {46 f := filter.New()47 f.Test()48}49func main() {50 f := filter.New()51 f.Test()52}53func main() {54 f := filter.New()55 f.Test()56}57func main() {58 f := filter.New()59 f.Test()60}61func main() {62 f := filter.New()63 f.Test()64}65func main() {66 f := filter.New()67 f.Test()68}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f := filter{}4 f.Test()5}6import (7func main() {8 f := filter{}9 f.Test()10}11import (12func main() {13 f := filter{}14 f.Test()15}16import (17func main() {18 f := filter{}19 f.Test()20}21import (22func main() {23 f := filter{}24 f.Test()25}26import (27func main() {28 f := filter{}29 f.Test()30}31import (32func main() {33 f := filter{}34 f.Test()35}36import (37func main() {38 f := filter{}39 f.Test()40}41import (42func main() {43 f := filter{}44 f.Test()45}46import (47func main() {48 f := filter{}49 f.Test()50}51import (52func main() {53 f := filter{}54 f.Test()55}56import (57func main() {58 f := filter{}59 f.Test()60}61import (62func main() {63 f := filter{}64 f.Test()65}66import

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 data := []string{"one", "two", "three"}4 fmt.Println(filter.Test(data))5}6func Test(data []string) []string {7}8 /usr/local/go/src/filter (from $GOROOT)9 /home/username/go/src/filter (from $GOPATH)

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f1 := filter.NewLengthFilter(3)4 f2 := filter.NewLengthFilter(5)5 f3 := filter.NewLengthFilter(7)6 f1.SetNext(f2)7 f2.SetNext(f3)8 f1.Test("abc")9 f1.Test("abcdef")10 f1.Test("abcdefgh")11}

Full Screen

Full Screen

Test

Using AI Code Generation

copy

Full Screen

1if (filter.Test("Hello")) {2 Console.WriteLine("Hello is a valid string");3}4else {5 Console.WriteLine("Hello is not a valid string");6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful