xref: /sqlite-3.40.0/test/prefixes.test (revision 9c039d9f)
1*9c039d9fSdan# 2018-01-15
2*9c039d9fSdan#
3*9c039d9fSdan# The author disclaims copyright to this source code.  In place of
4*9c039d9fSdan# a legal notice, here is a blessing:
5*9c039d9fSdan#
6*9c039d9fSdan#    May you do good and not evil.
7*9c039d9fSdan#    May you find forgiveness for yourself and forgive others.
8*9c039d9fSdan#    May you share freely, never taking more than you give.
9*9c039d9fSdan#
10*9c039d9fSdan#***********************************************************************
11*9c039d9fSdan# This file implements regression tests for SQLite library.  The
12*9c039d9fSdan# focus of this file is prefixes.c extension
13*9c039d9fSdan#
14*9c039d9fSdan
15*9c039d9fSdanset testdir [file dirname $argv0]
16*9c039d9fSdansource $testdir/tester.tcl
17*9c039d9fSdanset testprefix unionvtab
18*9c039d9fSdan
19*9c039d9fSdanifcapable !vtab {
20*9c039d9fSdan  finish_test
21*9c039d9fSdan  return
22*9c039d9fSdan}
23*9c039d9fSdan
24*9c039d9fSdanload_static_extension db prefixes
25*9c039d9fSdan
26*9c039d9fSdanforeach {tn zLeft zRight expected} {
27*9c039d9fSdan  1 abcdxxx abcyy    3
28*9c039d9fSdan  2 abcdxxx bcyyy    0
29*9c039d9fSdan  3 abcdxxx ab       2
30*9c039d9fSdan  4 ab      abcd     2
31*9c039d9fSdan
32*9c039d9fSdan  5 "xyz\u1234xz" "xyz\u1234xy" 5
33*9c039d9fSdan  6 "xyz\u1234"   "xyz\u1234xy" 4
34*9c039d9fSdan  7 "xyz\u1234"   "xyz\u1234"   4
35*9c039d9fSdan  8 "xyz\u1234xy" "xyz\u1234"   4
36*9c039d9fSdan  9 "xyz\u1234xy" "xyz\u1233"   3
37*9c039d9fSdan 10 "xyz\u1234xy" "xyz\u1235"   3
38*9c039d9fSdan} {
39*9c039d9fSdan  do_execsql_test 1.$tn { SELECT prefix_length($zLeft, $zRight) } $expected
40*9c039d9fSdan}
41*9c039d9fSdan
42*9c039d9fSdan
43*9c039d9fSdando_execsql_test 2.0 {
44*9c039d9fSdan  CREATE TABLE t1(k TEXT UNIQUE, v INTEGER);
45*9c039d9fSdan  INSERT INTO t1 VALUES
46*9c039d9fSdan    ('aback', 1),
47*9c039d9fSdan    ('abaft', 2),
48*9c039d9fSdan    ('abandon', 3),
49*9c039d9fSdan    ('abandoned', 4),
50*9c039d9fSdan    ('abandoning', 5),
51*9c039d9fSdan    ('abandonment', 6),
52*9c039d9fSdan    ('abandons', 7),
53*9c039d9fSdan    ('abase', 8),
54*9c039d9fSdan    ('abased', 9),
55*9c039d9fSdan    ('abasement', 10),
56*9c039d9fSdan    ('abasements', 11),
57*9c039d9fSdan    ('abases', 12),
58*9c039d9fSdan    ('abash', 13),
59*9c039d9fSdan    ('abashed', 14),
60*9c039d9fSdan    ('abashes', 15),
61*9c039d9fSdan    ('abashing', 16),
62*9c039d9fSdan    ('abasing', 17),
63*9c039d9fSdan    ('abate', 18),
64*9c039d9fSdan    ('abated', 19),
65*9c039d9fSdan    ('abatement', 20),
66*9c039d9fSdan    ('abatements', 21);
67*9c039d9fSdan}
68*9c039d9fSdan
69*9c039d9fSdanforeach {tn INPUT expected} {
70*9c039d9fSdan  1 abatementt   abatement
71*9c039d9fSdan  2 abashet      abash
72*9c039d9fSdan  3 abandonio    abandon
73*9c039d9fSdan  4 abasemenu    abase
74*9c039d9fSdan} {
75*9c039d9fSdan  do_execsql_test 2.$tn {
76*9c039d9fSdan    WITH finder(str) AS (
77*9c039d9fSdan      SELECT (SELECT max(k) FROM t1 WHERE k<=$INPUT)
78*9c039d9fSdan        UNION ALL
79*9c039d9fSdan        SELECT (
80*9c039d9fSdan          SELECT max(k) FROM t1
81*9c039d9fSdan          WHERE k<=substr($INPUT, 1, prefix_length(finder.str, $INPUT))
82*9c039d9fSdan        ) FROM finder WHERE length(finder.str)>0
83*9c039d9fSdan      )
84*9c039d9fSdan    SELECT str FROM finder WHERE length(str)==prefix_length(str, $INPUT) LIMIT 1
85*9c039d9fSdan  } $expected
86*9c039d9fSdan}
87*9c039d9fSdan
88*9c039d9fSdanfinish_test
89