go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/starlark/testdata/tuple.star (about)

     1  # Tests of Starlark 'tuple'
     2  
     3  load("assert.star", "assert")
     4  
     5  # literal
     6  assert.eq((), ())
     7  assert.eq((1), 1)
     8  assert.eq((1,), (1,))
     9  assert.ne((1), (1,))
    10  assert.eq((1, 2), (1, 2))
    11  assert.eq((1, 2, 3, 4, 5), (1, 2, 3, 4, 5))
    12  assert.ne((1, 2, 3), (1, 2, 4))
    13  
    14  # truth
    15  assert.true((False,))
    16  assert.true((False, False))
    17  assert.true(not ())
    18  
    19  # indexing, x[i]
    20  assert.eq(("a", "b")[0], "a")
    21  assert.eq(("a", "b")[1], "b")
    22  
    23  # slicing, x[i:j]
    24  assert.eq("abcd"[0:4:1], "abcd")
    25  assert.eq("abcd"[::2], "ac")
    26  assert.eq("abcd"[1::2], "bd")
    27  assert.eq("abcd"[4:0:-1], "dcb")
    28  banana = tuple("banana".elems())
    29  assert.eq(banana[7::-2], tuple("aaa".elems()))
    30  assert.eq(banana[6::-2], tuple("aaa".elems()))
    31  assert.eq(banana[5::-2], tuple("aaa".elems()))
    32  assert.eq(banana[4::-2], tuple("nnb".elems()))
    33  
    34  # tuple
    35  assert.eq(tuple(), ())
    36  assert.eq(tuple("abc".elems()), ("a", "b", "c"))
    37  assert.eq(tuple(["a", "b", "c"]), ("a", "b", "c"))
    38  assert.eq(tuple([1]), (1,))
    39  assert.fails(lambda: tuple(1), "got int, want iterable")
    40  
    41  # tuple * int,  int * tuple
    42  abc = tuple("abc".elems())
    43  assert.eq(abc * 0, ())
    44  assert.eq(abc * -1, ())
    45  assert.eq(abc * 1, abc)
    46  assert.eq(abc * 3, ("a", "b", "c", "a", "b", "c", "a", "b", "c"))
    47  assert.eq(0 * abc, ())
    48  assert.eq(-1 * abc, ())
    49  assert.eq(1 * abc, abc)
    50  assert.eq(3 * abc, ("a", "b", "c", "a", "b", "c", "a", "b", "c"))
    51  assert.fails(lambda: abc * (1000000 * 1000000), "repeat count 1000000000000 too large")
    52  assert.fails(lambda: abc * 1000000 * 1000000, "excessive repeat \\(3000000 \\* 1000000 elements")
    53  
    54  # TODO(adonovan): test use of tuple as sequence
    55  # (for loop, comprehension, library functions).