os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/lindex.test
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
# Commands covered:  lindex
sl@0
     2
#
sl@0
     3
# This file contains a collection of tests for one or more of the Tcl
sl@0
     4
# built-in commands.  Sourcing this file into Tcl runs the tests and
sl@0
     5
# generates output for errors.  No output means no errors were found.
sl@0
     6
#
sl@0
     7
# Copyright (c) 1991-1993 The Regents of the University of California.
sl@0
     8
# Copyright (c) 1994 Sun Microsystems, Inc.
sl@0
     9
# Copyright (c) 1998-1999 by Scriptics Corporation.
sl@0
    10
# Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.
sl@0
    11
#
sl@0
    12
# See the file "license.terms" for information on usage and redistribution
sl@0
    13
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    14
#
sl@0
    15
# RCS: @(#) $Id: lindex.test,v 1.10 2002/04/19 13:08:56 dkf Exp $
sl@0
    16
sl@0
    17
if {[lsearch [namespace children] ::tcltest] == -1} {
sl@0
    18
    package require tcltest
sl@0
    19
    namespace import -force ::tcltest::*
sl@0
    20
}
sl@0
    21
sl@0
    22
set lindex lindex
sl@0
    23
set minus -
sl@0
    24
sl@0
    25
# Tests of Tcl_LindexObjCmd, NOT COMPILED
sl@0
    26
sl@0
    27
test lindex-1.1 {wrong # args} {
sl@0
    28
    list [catch {eval $lindex} result] $result
sl@0
    29
} "1 {wrong # args: should be \"lindex list ?index...?\"}"
sl@0
    30
sl@0
    31
# Indices that are lists or convertible to lists
sl@0
    32
sl@0
    33
test lindex-2.1 {empty index list} {
sl@0
    34
    set x {}
sl@0
    35
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    36
} {{a b c} {a b c}}
sl@0
    37
sl@0
    38
test lindex-2.2 {singleton index list} {
sl@0
    39
    set x { 1 }
sl@0
    40
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    41
} {b b}
sl@0
    42
sl@0
    43
test lindex-2.3 {multiple indices in list} {
sl@0
    44
    set x {1 2}
sl@0
    45
    list [eval [list $lindex {{a b c} {d e f}} $x]] \
sl@0
    46
	[eval [list $lindex {{a b c} {d e f}} $x]]
sl@0
    47
} {f f}
sl@0
    48
sl@0
    49
test lindex-2.4 {malformed index list} {
sl@0
    50
    set x \{
sl@0
    51
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
    52
} {1 bad\ index\ \"\{\":\ must\ be\ integer\ or\ end?-integer?}
sl@0
    53
sl@0
    54
# Indices that are integers or convertible to integers
sl@0
    55
sl@0
    56
test lindex-3.1 {integer -1} {
sl@0
    57
    set x ${minus}1
sl@0
    58
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    59
} {{} {}}
sl@0
    60
sl@0
    61
test lindex-3.2 {integer 0} {
sl@0
    62
    set x [string range 00 0 0]
sl@0
    63
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    64
} {a a}
sl@0
    65
sl@0
    66
test lindex-3.3 {integer 2} {
sl@0
    67
    set x [string range 22 0 0]
sl@0
    68
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    69
} {c c}
sl@0
    70
sl@0
    71
test lindex-3.4 {integer 3} {
sl@0
    72
    set x [string range 33 0 0]
sl@0
    73
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    74
} {{} {}}
sl@0
    75
sl@0
    76
test lindex-3.5 {bad octal} {
sl@0
    77
    set x 08
sl@0
    78
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
    79
} "1 {bad index \"08\": must be integer or end?-integer? (looks like invalid octal number)}"
sl@0
    80
sl@0
    81
test lindex-3.6 {bad octal} {
sl@0
    82
    set x -09
sl@0
    83
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
    84
} "1 {bad index \"-09\": must be integer or end?-integer? (looks like invalid octal number)}"
sl@0
    85
sl@0
    86
test lindex-3.7 {indexes don't shimmer wide ints} {
sl@0
    87
    set x [expr {(wide(1)<<31) - 2}]
sl@0
    88
    list $x [lindex {1 2 3} $x] [incr x] [incr x]
sl@0
    89
} {2147483646 {} 2147483647 2147483648}
sl@0
    90
sl@0
    91
# Indices relative to end
sl@0
    92
sl@0
    93
test lindex-4.1 {index = end} {
sl@0
    94
    set x end
sl@0
    95
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
    96
} {c c}
sl@0
    97
sl@0
    98
test lindex-4.2 {index = end--1} {
sl@0
    99
    set x end--1
sl@0
   100
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
   101
} {{} {}}
sl@0
   102
sl@0
   103
test lindex-4.3 {index = end-0} {
sl@0
   104
    set x end-0
sl@0
   105
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
   106
} {c c}
sl@0
   107
sl@0
   108
test lindex-4.4 {index = end-2} {
sl@0
   109
    set x end-2
sl@0
   110
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
   111
} {a a}
sl@0
   112
sl@0
   113
test lindex-4.5 {index = end-3} {
sl@0
   114
    set x end-3
sl@0
   115
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
   116
} {{} {}}
sl@0
   117
sl@0
   118
test lindex-4.6 {bad octal} {
sl@0
   119
    set x end-08
sl@0
   120
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
   121
} "1 {bad index \"end-08\": must be integer or end?-integer? (looks like invalid octal number)}"
sl@0
   122
sl@0
   123
test lindex-4.7 {bad octal} {
sl@0
   124
    set x end--09
sl@0
   125
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
   126
} "1 {bad index \"end--09\": must be integer or end?-integer?}"
sl@0
   127
sl@0
   128
test lindex-4.8 {bad integer, not octal} {
sl@0
   129
    set x end-0a2
sl@0
   130
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
   131
} "1 {bad index \"end-0a2\": must be integer or end?-integer?}"
sl@0
   132
sl@0
   133
test lindex-4.9 {incomplete end} {
sl@0
   134
    set x en
sl@0
   135
    list [eval [list $lindex {a b c} $x]] [eval [list $lindex {a b c} $x]]
sl@0
   136
} {c c}
sl@0
   137
sl@0
   138
test lindex-4.10 {incomplete end-} {
sl@0
   139
    set x end-
sl@0
   140
    list [catch { eval [list $lindex {a b c} $x] } result] $result
sl@0
   141
} "1 {bad index \"end-\": must be integer or end?-integer?}"
sl@0
   142
sl@0
   143
test lindex-5.1 {bad second index} {
sl@0
   144
    list [catch { eval [list $lindex {a b c} 0 0a2] } result] $result
sl@0
   145
} "1 {bad index \"0a2\": must be integer or end?-integer?}"
sl@0
   146
sl@0
   147
test lindex-5.2 {good second index} {
sl@0
   148
    eval [list $lindex {{a b c} {d e f} {g h i}} 1 2]
sl@0
   149
} f
sl@0
   150
sl@0
   151
test lindex-5.3 {three indices} {
sl@0
   152
    eval [list $lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1]
sl@0
   153
} f
sl@0
   154
test lindex-6.1 {error conditions in parsing list} {
sl@0
   155
    list [catch {eval [list $lindex "a \{" 2]} msg] $msg
sl@0
   156
} {1 {unmatched open brace in list}}
sl@0
   157
test lindex-6.2 {error conditions in parsing list} {
sl@0
   158
    list [catch {eval [list $lindex {a {b c}d e} 2]} msg] $msg
sl@0
   159
} {1 {list element in braces followed by "d" instead of space}}
sl@0
   160
test lindex-6.3 {error conditions in parsing list} {
sl@0
   161
    list [catch {eval [list $lindex {a "b c"def ghi} 2]} msg] $msg
sl@0
   162
} {1 {list element in quotes followed by "def" instead of space}}
sl@0
   163
sl@0
   164
test lindex-7.1 {quoted elements} {
sl@0
   165
    eval [list $lindex {a "b c" d} 1]
sl@0
   166
} {b c}
sl@0
   167
test lindex-7.2 {quoted elements} {
sl@0
   168
    eval [list $lindex {"{}" b c} 0]
sl@0
   169
} {{}}
sl@0
   170
test lindex-7.3 {quoted elements} {
sl@0
   171
    eval [list $lindex {ab "c d \" x" y} 1]
sl@0
   172
} {c d " x}
sl@0
   173
test lindex-7.4 {quoted elements} {
sl@0
   174
    lindex {a b {c d "e} {f g"}} 2
sl@0
   175
} {c d "e}
sl@0
   176
sl@0
   177
test lindex-8.1 {data reuse} {
sl@0
   178
    set x 0
sl@0
   179
    eval [list $lindex $x $x]
sl@0
   180
} {0}
sl@0
   181
sl@0
   182
test lindex-8.2 {data reuse} {
sl@0
   183
    set a 0
sl@0
   184
    eval [list $lindex $a $a $a]
sl@0
   185
} 0
sl@0
   186
test lindex-8.3 {data reuse} {
sl@0
   187
    set a 1
sl@0
   188
    eval [list $lindex $a $a $a]
sl@0
   189
} {}
sl@0
   190
sl@0
   191
test lindex-8.4 {data reuse} {
sl@0
   192
    set x [list 0 0]
sl@0
   193
    eval [list $lindex $x $x]
sl@0
   194
} {0}
sl@0
   195
sl@0
   196
test lindex-8.5 {data reuse} {
sl@0
   197
    set x 0
sl@0
   198
    eval [list $lindex $x [list $x $x]]
sl@0
   199
} {0}
sl@0
   200
sl@0
   201
test lindex-8.6 {data reuse} {
sl@0
   202
    set x [list 1 1]
sl@0
   203
    eval [list $lindex $x $x]
sl@0
   204
} {}
sl@0
   205
sl@0
   206
test lindex-8.7 {data reuse} {
sl@0
   207
    set x 1
sl@0
   208
    eval [list lindex $x [list $x $x]]
sl@0
   209
} {}
sl@0
   210
sl@0
   211
#----------------------------------------------------------------------
sl@0
   212
sl@0
   213
# Compilation tests for lindex
sl@0
   214
sl@0
   215
test lindex-9.1 {wrong # args} {
sl@0
   216
    list [catch {lindex} result] $result
sl@0
   217
} "1 {wrong # args: should be \"lindex list ?index...?\"}"
sl@0
   218
sl@0
   219
# Indices that are lists or convertible to lists
sl@0
   220
sl@0
   221
test lindex-10.1 {empty index list} {
sl@0
   222
    set x {}
sl@0
   223
    catch {
sl@0
   224
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   225
    } result
sl@0
   226
    set result
sl@0
   227
} {{a b c} {a b c}}
sl@0
   228
sl@0
   229
test lindex-10.2 {singleton index list} {
sl@0
   230
    set x { 1 }
sl@0
   231
    catch {
sl@0
   232
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   233
    } result
sl@0
   234
    set result
sl@0
   235
} {b b}
sl@0
   236
sl@0
   237
test lindex-10.3 {multiple indices in list} {
sl@0
   238
    set x {1 2}
sl@0
   239
    catch {
sl@0
   240
	list [lindex {{a b c} {d e f}} $x] [lindex {{a b c} {d e f}} $x]
sl@0
   241
    } result
sl@0
   242
    set result
sl@0
   243
} {f f}
sl@0
   244
sl@0
   245
test lindex-10.4 {malformed index list} {
sl@0
   246
    set x \{
sl@0
   247
    list [catch { lindex {a b c} $x } result] $result
sl@0
   248
} {1 bad\ index\ \"\{\":\ must\ be\ integer\ or\ end?-integer?}
sl@0
   249
sl@0
   250
# Indices that are integers or convertible to integers
sl@0
   251
sl@0
   252
test lindex-11.1 {integer -1} {
sl@0
   253
    set x ${minus}1
sl@0
   254
    catch {
sl@0
   255
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   256
    } result
sl@0
   257
    set result
sl@0
   258
} {{} {}}
sl@0
   259
sl@0
   260
test lindex-11.2 {integer 0} {
sl@0
   261
    set x [string range 00 0 0]
sl@0
   262
    catch {
sl@0
   263
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   264
    } result
sl@0
   265
    set result
sl@0
   266
} {a a}
sl@0
   267
sl@0
   268
test lindex-11.3 {integer 2} {
sl@0
   269
    set x [string range 22 0 0]
sl@0
   270
    catch {
sl@0
   271
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   272
    } result
sl@0
   273
    set result
sl@0
   274
} {c c}
sl@0
   275
sl@0
   276
test lindex-11.4 {integer 3} {
sl@0
   277
    set x [string range 33 0 0]
sl@0
   278
    catch {
sl@0
   279
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   280
    } result
sl@0
   281
    set result
sl@0
   282
} {{} {}}
sl@0
   283
sl@0
   284
test lindex-11.5 {bad octal} {
sl@0
   285
    set x 08
sl@0
   286
    list [catch { lindex {a b c} $x } result] $result
sl@0
   287
} "1 {bad index \"08\": must be integer or end?-integer? (looks like invalid octal number)}"
sl@0
   288
sl@0
   289
test lindex-11.6 {bad octal} {
sl@0
   290
    set x -09
sl@0
   291
    list [catch { lindex {a b c} $x } result] $result
sl@0
   292
} "1 {bad index \"-09\": must be integer or end?-integer? (looks like invalid octal number)}"
sl@0
   293
sl@0
   294
# Indices relative to end
sl@0
   295
sl@0
   296
test lindex-12.1 {index = end} {
sl@0
   297
    set x end
sl@0
   298
    catch {
sl@0
   299
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   300
    } result
sl@0
   301
    set result
sl@0
   302
} {c c}
sl@0
   303
sl@0
   304
test lindex-12.2 {index = end--1} {
sl@0
   305
    set x end--1
sl@0
   306
    catch {
sl@0
   307
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   308
    } result
sl@0
   309
    set result
sl@0
   310
} {{} {}}
sl@0
   311
sl@0
   312
test lindex-12.3 {index = end-0} {
sl@0
   313
    set x end-0
sl@0
   314
    catch {
sl@0
   315
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   316
    } result
sl@0
   317
    set result
sl@0
   318
} {c c}
sl@0
   319
sl@0
   320
test lindex-12.4 {index = end-2} {
sl@0
   321
    set x end-2
sl@0
   322
    catch {
sl@0
   323
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   324
    } result
sl@0
   325
    set result
sl@0
   326
} {a a}
sl@0
   327
sl@0
   328
test lindex-12.5 {index = end-3} {
sl@0
   329
    set x end-3
sl@0
   330
    catch {
sl@0
   331
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   332
    } result
sl@0
   333
    set result
sl@0
   334
} {{} {}}
sl@0
   335
sl@0
   336
test lindex-12.6 {bad octal} {
sl@0
   337
    set x end-08
sl@0
   338
    list [catch { lindex {a b c} $x } result] $result
sl@0
   339
} "1 {bad index \"end-08\": must be integer or end?-integer? (looks like invalid octal number)}"
sl@0
   340
sl@0
   341
test lindex-12.7 {bad octal} {
sl@0
   342
    set x end--09
sl@0
   343
    list [catch { lindex {a b c} $x } result] $result
sl@0
   344
} "1 {bad index \"end--09\": must be integer or end?-integer?}"
sl@0
   345
sl@0
   346
test lindex-12.8 {bad integer, not octal} {
sl@0
   347
    set x end-0a2
sl@0
   348
    list [catch { lindex {a b c} $x } result] $result
sl@0
   349
} "1 {bad index \"end-0a2\": must be integer or end?-integer?}"
sl@0
   350
sl@0
   351
test lindex-12.9 {incomplete end} {
sl@0
   352
    set x en
sl@0
   353
    catch {
sl@0
   354
	list [lindex {a b c} $x] [lindex {a b c} $x]
sl@0
   355
    } result
sl@0
   356
    set result
sl@0
   357
} {c c}
sl@0
   358
sl@0
   359
test lindex-12.10 {incomplete end-} {
sl@0
   360
    set x end-
sl@0
   361
    list [catch { lindex {a b c} $x } result] $result
sl@0
   362
} "1 {bad index \"end-\": must be integer or end?-integer?}"
sl@0
   363
sl@0
   364
test lindex-13.1 {bad second index} {
sl@0
   365
    list [catch { lindex {a b c} 0 0a2 } result] $result
sl@0
   366
} "1 {bad index \"0a2\": must be integer or end?-integer?}"
sl@0
   367
sl@0
   368
test lindex-13.2 {good second index} {
sl@0
   369
    catch {
sl@0
   370
	lindex {{a b c} {d e f} {g h i}} 1 2
sl@0
   371
    } result
sl@0
   372
    set result
sl@0
   373
} f
sl@0
   374
sl@0
   375
test lindex-13.3 {three indices} {
sl@0
   376
    catch {
sl@0
   377
	lindex {{{a b} {c d}} {{e f} {g h}}} 1 0 1
sl@0
   378
    } result
sl@0
   379
    set result
sl@0
   380
} f
sl@0
   381
sl@0
   382
test lindex-14.1 {error conditions in parsing list} {
sl@0
   383
    list [catch { lindex "a \{" 2 } msg] $msg
sl@0
   384
} {1 {unmatched open brace in list}}
sl@0
   385
test lindex-14.2 {error conditions in parsing list} {
sl@0
   386
    list [catch { lindex {a {b c}d e} 2 } msg] $msg
sl@0
   387
} {1 {list element in braces followed by "d" instead of space}}
sl@0
   388
test lindex-14.3 {error conditions in parsing list} {
sl@0
   389
    list [catch { lindex {a "b c"def ghi} 2 } msg] $msg
sl@0
   390
} {1 {list element in quotes followed by "def" instead of space}}
sl@0
   391
sl@0
   392
test lindex-15.1 {quoted elements} {
sl@0
   393
    catch {
sl@0
   394
	lindex {a "b c" d} 1
sl@0
   395
    } result
sl@0
   396
    set result
sl@0
   397
} {b c}
sl@0
   398
test lindex-15.2 {quoted elements} {
sl@0
   399
    catch {
sl@0
   400
	lindex {"{}" b c} 0
sl@0
   401
    } result
sl@0
   402
    set result
sl@0
   403
} {{}}
sl@0
   404
test lindex-15.3 {quoted elements} {
sl@0
   405
    catch {
sl@0
   406
	lindex {ab "c d \" x" y} 1
sl@0
   407
    } result
sl@0
   408
    set result
sl@0
   409
} {c d " x}
sl@0
   410
test lindex-15.4 {quoted elements} {
sl@0
   411
    catch {
sl@0
   412
	lindex {a b {c d "e} {f g"}} 2
sl@0
   413
    } result
sl@0
   414
    set result
sl@0
   415
} {c d "e}
sl@0
   416
sl@0
   417
test lindex-16.1 {data reuse} {
sl@0
   418
    set x 0
sl@0
   419
    catch {
sl@0
   420
	lindex $x $x
sl@0
   421
    } result
sl@0
   422
    set result
sl@0
   423
} {0}
sl@0
   424
sl@0
   425
test lindex-16.2 {data reuse} {
sl@0
   426
    set a 0
sl@0
   427
    catch {
sl@0
   428
	lindex $a $a $a
sl@0
   429
    } result
sl@0
   430
    set result
sl@0
   431
} 0
sl@0
   432
test lindex-16.3 {data reuse} {
sl@0
   433
    set a 1
sl@0
   434
    catch {
sl@0
   435
	lindex $a $a $a
sl@0
   436
    } result
sl@0
   437
    set result
sl@0
   438
} {}
sl@0
   439
sl@0
   440
test lindex-16.4 {data reuse} {
sl@0
   441
    set x [list 0 0]
sl@0
   442
    catch {
sl@0
   443
	lindex $x $x
sl@0
   444
    } result
sl@0
   445
    set result
sl@0
   446
} {0}
sl@0
   447
sl@0
   448
test lindex-16.5 {data reuse} {
sl@0
   449
    set x 0
sl@0
   450
    catch {
sl@0
   451
	lindex $x [list $x $x]
sl@0
   452
    } result
sl@0
   453
    set result
sl@0
   454
} {0}
sl@0
   455
sl@0
   456
test lindex-16.6 {data reuse} {
sl@0
   457
    set x [list 1 1]
sl@0
   458
    catch {
sl@0
   459
	lindex $x $x
sl@0
   460
    } result
sl@0
   461
    set result
sl@0
   462
} {}
sl@0
   463
sl@0
   464
test lindex-16.7 {data reuse} {
sl@0
   465
    set x 1
sl@0
   466
    catch {
sl@0
   467
	lindex $x [list $x $x]
sl@0
   468
    } result
sl@0
   469
    set result
sl@0
   470
} {}
sl@0
   471
sl@0
   472
catch { unset lindex}
sl@0
   473
catch { unset minus }
sl@0
   474
sl@0
   475
# cleanup
sl@0
   476
::tcltest::cleanupTests
sl@0
   477
return