Best Gauge code snippet using result.Test
ranger_test.go
Source:ranger_test.go
...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",...
find_test.go
Source:find_test.go
...10// We can derive the textual results from the indexed results, the non-submatch11// results from the submatched results, the single results from the 'all' results,12// and the byte results from the string results. Therefore the table includes13// only the FindAllStringSubmatchIndex result.14type FindTest struct {15 pat string16 text string17 matches [][]int18}19func (t FindTest) String() string {20 return fmt.Sprintf("pat: %#q text: %#q", t.pat, t.text)21}22var findTests = []FindTest{23 {``, ``, build(1, 0, 0)},24 {`^abcdefg`, "abcdefg", build(1, 0, 7)},25 {`a+`, "baaab", build(1, 1, 4)},26 {"abcd..", "abcdef", build(1, 0, 6)},27 {`a`, "a", build(1, 0, 1)},28 {`x`, "y", nil},29 {`b`, "abc", build(1, 1, 2)},30 {`.`, "a", build(1, 0, 1)},31 {`.*`, "abcdef", build(1, 0, 6)},32 {`^`, "abcde", build(1, 0, 0)},33 {`$`, "abcde", build(1, 5, 5)},34 {`^abcd$`, "abcd", build(1, 0, 4)},35 {`^bcd'`, "abcdef", nil},36 {`^abcd$`, "abcde", nil},37 {`a+`, "baaab", build(1, 1, 4)},38 {`a*`, "baaab", build(3, 0, 0, 1, 4, 5, 5)},39 {`[a-z]+`, "abcd", build(1, 0, 4)},40 {`[^a-z]+`, "ab1234cd", build(1, 2, 6)},41 {`[a\-\]z]+`, "az]-bcz", build(2, 0, 4, 6, 7)},42 {`[^\n]+`, "abcd\n", build(1, 0, 4)},43 {`[æ¥æ¬èª]+`, "æ¥æ¬èªæ¥æ¬èª", build(1, 0, 18)},44 {`æ¥æ¬èª+`, "æ¥æ¬èª", build(1, 0, 9)},45 {`æ¥æ¬èª+`, "æ¥æ¬èªèªèªèª", build(1, 0, 18)},46 {`()`, "", build(1, 0, 0, 0, 0)},47 {`(a)`, "a", build(1, 0, 1, 0, 1)},48 {`(.)(.)`, "æ¥a", build(1, 0, 4, 0, 3, 3, 4)},49 {`(.*)`, "", build(1, 0, 0, 0, 0)},50 {`(.*)`, "abcd", build(1, 0, 4, 0, 4)},51 {`(..)(..)`, "abcd", build(1, 0, 4, 0, 2, 2, 4)},52 {`(([^xyz]*)(d))`, "abcd", build(1, 0, 4, 0, 4, 0, 3, 3, 4)},53 {`((a|b|c)*(d))`, "abcd", build(1, 0, 4, 0, 4, 2, 3, 3, 4)},54 {`(((a|b|c)*)(d))`, "abcd", build(1, 0, 4, 0, 4, 0, 3, 2, 3, 3, 4)},55 {`\a\b\f\n\r\t\v`, "\a\b\f\n\r\t\v", build(1, 0, 7)},56 {`[\a\b\f\n\r\t\v]+`, "\a\b\f\n\r\t\v", build(1, 0, 7)},57 {`a*(|(b))c*`, "aacc", build(1, 0, 4, 2, 2, -1, -1)},58 {`(.*).*`, "ab", build(1, 0, 2, 0, 2)},59 {`[.]`, ".", build(1, 0, 1)},60 {`/$`, "/abc/", build(1, 4, 5)},61 {`/$`, "/abc", nil},62 // multiple matches63 {`.`, "abc", build(3, 0, 1, 1, 2, 2, 3)},64 {`(.)`, "abc", build(3, 0, 1, 0, 1, 1, 2, 1, 2, 2, 3, 2, 3)},65 {`.(.)`, "abcd", build(2, 0, 2, 1, 2, 2, 4, 3, 4)},66 {`ab*`, "abbaab", build(3, 0, 3, 3, 4, 4, 6)},67 {`a(b*)`, "abbaab", build(3, 0, 3, 1, 3, 3, 4, 4, 4, 4, 6, 5, 6)},68 // fixed bugs69 {`ab$`, "cab", build(1, 1, 3)},70 {`axxb$`, "axxcb", nil},71 {`data`, "daXY data", build(1, 5, 9)},72 {`da(.)a$`, "daXY data", build(1, 5, 9, 7, 8)},73 {`zx+`, "zzx", build(1, 1, 3)},74 // can backslash-escape any punctuation75 {`\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~`,76 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, build(1, 0, 31)},77 {`[\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\{\|\}\~]+`,78 `!"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, build(1, 0, 31)},79 {"\\`", "`", build(1, 0, 1)},80 {"[\\`]+", "`", build(1, 0, 1)},81 // long set of matches (longer than startSize)82 {83 ".",84 "qwertyuiopasdfghjklzxcvbnm1234567890",85 build(36, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10,86 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20,87 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30,88 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36),89 },90}91// build is a helper to construct a [][]int by extracting n sequences from x.92// This represents n matches with len(x)/n submatches each.93func build(n int, x ...int) [][]int {94 ret := make([][]int, n)95 runLength := len(x) / n96 j := 097 for i := range ret {98 ret[i] = make([]int, runLength)99 copy(ret[i], x[j:])100 j += runLength101 if j > len(x) {102 panic("invalid build entry")103 }104 }105 return ret106}107// First the simple cases.108func TestFind(t *testing.T) {109 for _, test := range findTests {110 re := MustCompile(test.pat)111 if re.String() != test.pat {112 t.Errorf("String() = `%s`; should be `%s`", re.String(), test.pat)113 }114 result := re.Find([]byte(test.text))115 switch {116 case len(test.matches) == 0 && len(result) == 0:117 // ok118 case test.matches == nil && result != nil:119 t.Errorf("expected no match; got one: %s", test)120 case test.matches != nil && result == nil:121 t.Errorf("expected match; got none: %s", test)122 case test.matches != nil && result != nil:123 expect := test.text[test.matches[0][0]:test.matches[0][1]]124 if expect != string(result) {125 t.Errorf("expected %q got %q: %s", expect, result, test)126 }127 }128 }129}130func TestFindString(t *testing.T) {131 for _, test := range findTests {132 result := MustCompile(test.pat).FindString(test.text)133 switch {134 case len(test.matches) == 0 && len(result) == 0:135 // ok136 case test.matches == nil && result != "":137 t.Errorf("expected no match; got one: %s", test)138 case test.matches != nil && result == "":139 // Tricky because an empty result has two meanings: no match or empty match.140 if test.matches[0][0] != test.matches[0][1] {141 t.Errorf("expected match; got none: %s", test)142 }143 case test.matches != nil && result != "":144 expect := test.text[test.matches[0][0]:test.matches[0][1]]145 if expect != result {146 t.Errorf("expected %q got %q: %s", expect, result, test)147 }148 }149 }150}151func testFindIndex(test *FindTest, result []int, t *testing.T) {152 switch {153 case len(test.matches) == 0 && len(result) == 0:154 // ok155 case test.matches == nil && result != nil:156 t.Errorf("expected no match; got one: %s", test)157 case test.matches != nil && result == nil:158 t.Errorf("expected match; got none: %s", test)159 case test.matches != nil && result != nil:160 expect := test.matches[0]161 if expect[0] != result[0] || expect[1] != result[1] {162 t.Errorf("expected %v got %v: %s", expect, result, test)163 }164 }165}166func TestFindIndex(t *testing.T) {167 for _, test := range findTests {168 testFindIndex(&test, MustCompile(test.pat).FindIndex([]byte(test.text)), t)169 }170}171func TestFindStringIndex(t *testing.T) {172 for _, test := range findTests {173 testFindIndex(&test, MustCompile(test.pat).FindStringIndex(test.text), t)174 }175}176// Now come the simple All cases.177func TestFindAll(t *testing.T) {178 for _, test := range findTests {179 result := MustCompile(test.pat).FindAll([]byte(test.text), -1)180 switch {181 case test.matches == nil && result == nil:182 // ok183 case test.matches == nil && result != nil:184 t.Errorf("expected no match; got one: %s", test)185 case test.matches != nil && result == nil:186 t.Errorf("expected match; got none: %s", test)187 case test.matches != nil && result != nil:188 if len(test.matches) != len(result) {189 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)190 continue191 }192 for k, e := range test.matches {193 expect := test.text[e[0]:e[1]]194 if expect != string(result[k]) {195 t.Errorf("match %d: expected %q got %q: %s", k, expect, result[k], test)196 }197 }198 }199 }200}201func TestFindAllString(t *testing.T) {202 for _, test := range findTests {203 result := MustCompile(test.pat).FindAllString(test.text, -1)204 switch {205 case test.matches == nil && result == nil:206 // ok207 case test.matches == nil && result != nil:208 t.Errorf("expected no match; got one: %s", test)209 case test.matches != nil && result == nil:210 t.Errorf("expected match; got none: %s", test)211 case test.matches != nil && result != nil:212 if len(test.matches) != len(result) {213 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)214 continue215 }216 for k, e := range test.matches {217 expect := test.text[e[0]:e[1]]218 if expect != result[k] {219 t.Errorf("expected %q got %q: %s", expect, result, test)220 }221 }222 }223 }224}225func testFindAllIndex(test *FindTest, result [][]int, t *testing.T) {226 switch {227 case test.matches == nil && result == nil:228 // ok229 case test.matches == nil && result != nil:230 t.Errorf("expected no match; got one: %s", test)231 case test.matches != nil && result == nil:232 t.Errorf("expected match; got none: %s", test)233 case test.matches != nil && result != nil:234 if len(test.matches) != len(result) {235 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)236 return237 }238 for k, e := range test.matches {239 if e[0] != result[k][0] || e[1] != result[k][1] {240 t.Errorf("match %d: expected %v got %v: %s", k, e, result[k], test)241 }242 }243 }244}245func TestFindAllIndex(t *testing.T) {246 for _, test := range findTests {247 testFindAllIndex(&test, MustCompile(test.pat).FindAllIndex([]byte(test.text), -1), t)248 }249}250func TestFindAllStringIndex(t *testing.T) {251 for _, test := range findTests {252 testFindAllIndex(&test, MustCompile(test.pat).FindAllStringIndex(test.text, -1), t)253 }254}255// Now come the Submatch cases.256func testSubmatchBytes(test *FindTest, n int, submatches []int, result [][]byte, t *testing.T) {257 if len(submatches) != len(result)*2 {258 t.Errorf("match %d: expected %d submatches; got %d: %s", n, len(submatches)/2, len(result), test)259 return260 }261 for k := 0; k < len(submatches); k += 2 {262 if submatches[k] == -1 {263 if result[k/2] != nil {264 t.Errorf("match %d: expected nil got %q: %s", n, result, test)265 }266 continue267 }268 expect := test.text[submatches[k]:submatches[k+1]]269 if expect != string(result[k/2]) {270 t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test)271 return272 }273 }274}275func TestFindSubmatch(t *testing.T) {276 for _, test := range findTests {277 result := MustCompile(test.pat).FindSubmatch([]byte(test.text))278 switch {279 case test.matches == nil && result == nil:280 // ok281 case test.matches == nil && result != nil:282 t.Errorf("expected no match; got one: %s", test)283 case test.matches != nil && result == nil:284 t.Errorf("expected match; got none: %s", test)285 case test.matches != nil && result != nil:286 testSubmatchBytes(&test, 0, test.matches[0], result, t)287 }288 }289}290func testSubmatchString(test *FindTest, n int, submatches []int, result []string, t *testing.T) {291 if len(submatches) != len(result)*2 {292 t.Errorf("match %d: expected %d submatches; got %d: %s", n, len(submatches)/2, len(result), test)293 return294 }295 for k := 0; k < len(submatches); k += 2 {296 if submatches[k] == -1 {297 if result[k/2] != "" {298 t.Errorf("match %d: expected nil got %q: %s", n, result, test)299 }300 continue301 }302 expect := test.text[submatches[k]:submatches[k+1]]303 if expect != result[k/2] {304 t.Errorf("match %d: expected %q got %q: %s", n, expect, result, test)305 return306 }307 }308}309func TestFindStringSubmatch(t *testing.T) {310 for _, test := range findTests {311 result := MustCompile(test.pat).FindStringSubmatch(test.text)312 switch {313 case test.matches == nil && result == nil:314 // ok315 case test.matches == nil && result != nil:316 t.Errorf("expected no match; got one: %s", test)317 case test.matches != nil && result == nil:318 t.Errorf("expected match; got none: %s", test)319 case test.matches != nil && result != nil:320 testSubmatchString(&test, 0, test.matches[0], result, t)321 }322 }323}324func testSubmatchIndices(test *FindTest, n int, expect, result []int, t *testing.T) {325 if len(expect) != len(result) {326 t.Errorf("match %d: expected %d matches; got %d: %s", n, len(expect)/2, len(result)/2, test)327 return328 }329 for k, e := range expect {330 if e != result[k] {331 t.Errorf("match %d: submatch error: expected %v got %v: %s", n, expect, result, test)332 }333 }334}335func testFindSubmatchIndex(test *FindTest, result []int, t *testing.T) {336 switch {337 case test.matches == nil && result == nil:338 // ok339 case test.matches == nil && result != nil:340 t.Errorf("expected no match; got one: %s", test)341 case test.matches != nil && result == nil:342 t.Errorf("expected match; got none: %s", test)343 case test.matches != nil && result != nil:344 testSubmatchIndices(test, 0, test.matches[0], result, t)345 }346}347func TestFindSubmatchIndex(t *testing.T) {348 for _, test := range findTests {349 testFindSubmatchIndex(&test, MustCompile(test.pat).FindSubmatchIndex([]byte(test.text)), t)350 }351}352func TestFindStringSubmatchndex(t *testing.T) {353 for _, test := range findTests {354 testFindSubmatchIndex(&test, MustCompile(test.pat).FindStringSubmatchIndex(test.text), t)355 }356}357// Now come the monster AllSubmatch cases.358func TestFindAllSubmatch(t *testing.T) {359 for _, test := range findTests {360 result := MustCompile(test.pat).FindAllSubmatch([]byte(test.text), -1)361 switch {362 case test.matches == nil && result == nil:363 // ok364 case test.matches == nil && result != nil:365 t.Errorf("expected no match; got one: %s", test)366 case test.matches != nil && result == nil:367 t.Errorf("expected match; got none: %s", test)368 case len(test.matches) != len(result):369 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)370 case test.matches != nil && result != nil:371 for k, match := range test.matches {372 testSubmatchBytes(&test, k, match, result[k], t)373 }374 }375 }376}377func TestFindAllStringSubmatch(t *testing.T) {378 for _, test := range findTests {379 result := MustCompile(test.pat).FindAllStringSubmatch(test.text, -1)380 switch {381 case test.matches == nil && result == nil:382 // ok383 case test.matches == nil && result != nil:384 t.Errorf("expected no match; got one: %s", test)385 case test.matches != nil && result == nil:386 t.Errorf("expected match; got none: %s", test)387 case len(test.matches) != len(result):388 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)389 case test.matches != nil && result != nil:390 for k, match := range test.matches {391 testSubmatchString(&test, k, match, result[k], t)392 }393 }394 }395}396func testFindAllSubmatchIndex(test *FindTest, result [][]int, t *testing.T) {397 switch {398 case test.matches == nil && result == nil:399 // ok400 case test.matches == nil && result != nil:401 t.Errorf("expected no match; got one: %s", test)402 case test.matches != nil && result == nil:403 t.Errorf("expected match; got none: %s", test)404 case len(test.matches) != len(result):405 t.Errorf("expected %d matches; got %d: %s", len(test.matches), len(result), test)406 case test.matches != nil && result != nil:407 for k, match := range test.matches {408 testSubmatchIndices(test, k, match, result[k], t)409 }410 }411}412func TestFindAllSubmatchIndex(t *testing.T) {413 for _, test := range findTests {414 testFindAllSubmatchIndex(&test, MustCompile(test.pat).FindAllSubmatchIndex([]byte(test.text), -1), t)415 }416}417func TestFindAllStringSubmatchndex(t *testing.T) {418 for _, test := range findTests {419 testFindAllSubmatchIndex(&test, MustCompile(test.pat).FindAllStringSubmatchIndex(test.text, -1), t)420 }421}...
Test
Using AI Code Generation
1import (2func main() {3 result := testing.Benchmark(func(b *testing.B) {4 for i := 0; i < b.N; i++ {5 }6 })7 fmt.Println("N: ", result.N)8 fmt.Println("T: ", result.T)9 fmt.Println("Bytes: ", result.Bytes)10 fmt.Println("AllocedBytes: ", result.AllocedBytes)11 fmt.Println("Allocs: ", result.Allocs)12 fmt.Println("MemString: ", result.MemString())13 fmt.Println("String: ", result.String())14}15import (16func main() {17 result := testing.Benchmark(func(b *testing.B) {
Test
Using AI Code Generation
1func Test() {2 result := Result{}3 result.Test()4}5func Test() {6 result := Result{}7 result.Test()8}9func Test() {10 result := Result{}11 result.Test()12}13func Test() {14 result := Result{}15 result.Test()16}17func Test() {18 result := Result{}19 result.Test()20}21func Test() {22 result := Result{}23 result.Test()24}25func Test() {26 result := Result{}27 result.Test()28}29func Test() {30 result := Result{}31 result.Test()32}33func Test() {34 result := Result{}35 result.Test()36}37func Test() {38 result := Result{}39 result.Test()40}41func Test() {42 result := Result{}43 result.Test()44}45func Test() {46 result := Result{}47 result.Test()48}49func Test() {50 result := Result{}51 result.Test()52}53func Test() {54 result := Result{}55 result.Test()56}57func Test() {58 result := Result{}59 result.Test()60}61func Test() {62 result := Result{}63 result.Test()64}
Test
Using AI Code Generation
1import (2type Result struct {3}4func (r Result) Test() {5 fmt.Println("Name is", r.name)6 fmt.Println("Age is", r.age)7}8func main() {9 r := Result{"Raj", 25}10 r.Test()11}12import (13type Result struct {14}15func (r *Result) Test() {16 fmt.Println("Name is", r.name)17 fmt.Println("Age is", r.age)18}19func main() {20 r := Result{"Raj", 25}21 r.Test()22}23import (24type Result struct {25}26func (r *Result) Test() {27 fmt.Println("Name is", r.name)28 fmt.Println("Age is", r.age)29}30func main() {31 r := Result{"Raj", 25}32 r.Test()33}34import (35type Result struct {36}37func (r *Result) Test() {38 fmt.Println("Name is", r.name)
Test
Using AI Code Generation
1import "fmt"2func main() {3 result := Result{}4 result.Test()5 fmt.Println("Hello, playground")6}7import "fmt"8func main() {9 result := Result{}10 result.Test()11 fmt.Println("Hello, playground")12}
Test
Using AI Code Generation
1import (2func TestResult(t *testing.T) {3 fmt.Println("Result of Test")4}5import (6func TestResult(t *testing.T) {7 fmt.Println("Result of Test")8}9import (10func TestResult(t *testing.T) {11 fmt.Println("Result of Test")12}13import (14func TestResult(t *testing.T) {15 fmt.Println("Result of Test")16}17import (18func TestResult(t *testing.T) {19 fmt.Println("Result of Test")20}21import (22func TestResult(t *testing.T) {23 fmt.Println("Result of Test")24}25import (26func TestResult(t *testing.T) {27 fmt.Println("Result of Test")28}29import (30func TestResult(t *testing.T) {31 fmt.Println("Result of Test")32}33import (34func TestResult(t *testing.T) {35 fmt.Println("Result of Test")36}37import (38func TestResult(t *testing.T) {39 fmt.Println("Result of Test")40}41import (
Test
Using AI Code Generation
1import (2func Test(t *testing.T) {3 fmt.Println("Test")4}5func main() {6 fmt.Println("main")7}8--- PASS: Test (0.00s)9--- PASS: Test (0.00s)10--- PASS: Test (0.00s)11--- PASS: Test (0.00s)12total: (statements) 100.0%
Test
Using AI Code Generation
1import (2func Test(t *testing.T) {3fmt.Println("Test")4}5func main() {6}7import (8func Test(t *testing.T) {9fmt.Println("Test")10}11import (12func Test(t *testing.T) {13if 1 == 1 {14t.Log("Test")15} else {16t.Error("Test")17}18}19func main() {20}21--- PASS: Test (0.00s)22import (23func Test(t *testing.T) {24if 1 == 1 {25t.Log("Test")26} else {27t.Error("Test")28}29}30--- PASS: Test (0.00s)31In this example, we have created two files. One is 2.go and the other is 2_test.go. The first file contains the main function and the second file contains the Test function. When we run the program, the output is --- PASS: Test (0.00s) PASS ok _/home/abc/1 0.001s. Here, the Test function is not the main function. The Test function is a testing function. The Test function is used to test the code. The Test function is used to test the expected output with the actual output. The Test function is used to check the code’s correctness. The Test
Test
Using AI Code Generation
1import (2func main() {3 slice := []int{1, 2, 3}4 value := reflect.ValueOf(slice)5 typ := reflect.TypeOf(slice)6 result := reflect.MakeSlice(typ, 0, 0)7 resultValue := reflect.ValueOf(result)8 resultValue.MethodByName("Test").Call([]reflect.Value{value})9 fmt.Println(result)10}11import (12func main() {13 slice := []int{1, 2, 3}14 value := reflect.ValueOf(slice)15 typ := reflect.TypeOf(slice)16 result := reflect.MakeSlice(typ, 0, 0)17 resultValue := reflect.ValueOf(result)18 resultValue.MethodByName("Test").Call([]reflect.Value{value})19 fmt.Println(result)20}21import (22func main() {23 slice := []int{1, 2, 3}24 value := reflect.ValueOf(slice)25 typ := reflect.TypeOf(slice)26 result := reflect.MakeSlice(typ, 0, 0)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!