/ Hex Artifact Content
Login

Artifact 95a6e1b3415dec831522419b351c323ac5b1df668eade1477a66161320a24e6d:


0000: 23 20 32 30 30 39 20 44 65 63 65 6d 62 65 72 20  # 2009 December 
0010: 30 33 0a 23 0a 23 20 20 20 20 4d 61 79 20 79 6f  03.#.#    May yo
0020: 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
0030: 74 20 65 76 69 6c 2e 0a 23 20 20 20 20 4d 61 79  t evil..#    May
0040: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
0050: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
0060: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
0070: 74 68 65 72 73 2e 0a 23 20 20 20 20 4d 61 79 20  thers..#    May 
0080: 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
0090: 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
00a0: 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
00b0: 65 2e 0a 23 0a 23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..#.#**********
00c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
00d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
00e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
00f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 23 0a  *************.#.
0100: 23 20 54 68 65 20 74 65 73 74 73 20 69 6e 20 74  # The tests in t
0110: 68 69 73 20 66 69 6c 65 20 61 72 65 20 73 74 72  his file are str
0120: 75 63 74 75 72 61 6c 20 63 6f 76 65 72 61 67 65  uctural coverage
0130: 20 74 65 73 74 73 20 66 6f 72 20 46 54 53 33 2e   tests for FTS3.
0140: 0a 23 0a 0a 73 65 74 20 74 65 73 74 64 69 72 20  .#..set testdir 
0150: 5b 66 69 6c 65 20 64 69 72 6e 61 6d 65 20 24 61  [file dirname $a
0160: 72 67 76 30 5d 0a 73 6f 75 72 63 65 20 24 74 65  rgv0].source $te
0170: 73 74 64 69 72 2f 74 65 73 74 65 72 2e 74 63 6c  stdir/tester.tcl
0180: 0a 0a 23 20 49 66 20 74 68 69 73 20 62 75 69 6c  ..# If this buil
0190: 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  d does not inclu
01a0: 64 65 20 46 54 53 33 2c 20 73 6b 69 70 20 74 68  de FTS3, skip th
01b0: 65 20 74 65 73 74 73 20 69 6e 20 74 68 69 73 20  e tests in this 
01c0: 66 69 6c 65 2e 0a 23 0a 69 66 63 61 70 61 62 6c  file..#.ifcapabl
01d0: 65 20 21 66 74 73 33 20 7b 20 66 69 6e 69 73 68  e !fts3 { finish
01e0: 5f 74 65 73 74 20 3b 20 72 65 74 75 72 6e 20 7d  _test ; return }
01f0: 0a 73 6f 75 72 63 65 20 24 74 65 73 74 64 69 72  .source $testdir
0200: 2f 66 74 73 33 5f 63 6f 6d 6d 6f 6e 2e 74 63 6c  /fts3_common.tcl
0210: 0a 73 6f 75 72 63 65 20 24 74 65 73 74 64 69 72  .source $testdir
0220: 2f 6d 61 6c 6c 6f 63 5f 63 6f 6d 6d 6f 6e 2e 74  /malloc_common.t
0230: 63 6c 0a 0a 73 65 74 20 44 4f 5f 4d 41 4c 4c 4f  cl..set DO_MALLO
0240: 43 5f 54 45 53 54 20 30 0a 73 65 74 20 74 65 73  C_TEST 0.set tes
0250: 74 70 72 65 66 69 78 20 66 74 73 33 63 6f 76 0a  tprefix fts3cov.
0260: 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .#--------------
0270: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0280: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0290: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 57  ------------.# W
02b0: 68 65 6e 20 69 74 20 66 69 72 73 74 20 6e 65 65  hen it first nee
02c0: 64 73 20 74 6f 20 72 65 61 64 20 61 20 62 6c 6f  ds to read a blo
02d0: 63 6b 20 66 72 6f 6d 20 74 68 65 20 25 5f 73 65  ck from the %_se
02e0: 67 6d 65 6e 74 73 20 74 61 62 6c 65 2c 20 74 68  gments table, th
02f0: 65 20 46 54 53 33 20 0a 23 20 6d 6f 64 75 6c 65  e FTS3 .# module
0300: 20 63 6f 6d 70 69 6c 65 73 20 61 6e 20 53 51 4c   compiles an SQL
0310: 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74   statement for t
0320: 68 61 74 20 70 75 72 70 6f 73 65 2e 20 54 68 65  hat purpose. The
0330: 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 0a 23   statement is .#
0340: 20 73 74 6f 72 65 64 20 61 6e 64 20 72 65 75 73   stored and reus
0350: 65 64 20 65 61 63 68 20 73 75 62 73 65 71 75 65  ed each subseque
0360: 6e 74 20 74 69 6d 65 20 61 20 62 6c 6f 63 6b 20  nt time a block 
0370: 69 73 20 72 65 61 64 2e 20 54 68 69 73 20 74 65  is read. This te
0380: 73 74 20 63 61 73 65 20 0a 23 20 74 65 73 74 73  st case .# tests
0390: 20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20   the effects of 
03a0: 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63  an OOM error occ
03b0: 75 72 69 6e 67 20 77 68 69 6c 65 20 63 6f 6d 70  uring while comp
03c0: 69 6c 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d  iling the statem
03d0: 65 6e 74 2e 0a 23 0a 23 20 53 69 6d 69 6c 61 72  ent..#.# Similar
03e0: 6c 79 2c 20 77 68 65 6e 20 46 54 53 33 20 66 69  ly, when FTS3 fi
03f0: 72 73 74 20 6e 65 65 64 73 20 74 6f 20 73 63 61  rst needs to sca
0400: 6e 20 74 68 72 6f 75 67 68 20 61 20 73 65 74 20  n through a set 
0410: 6f 66 20 73 65 67 6d 65 6e 74 20 6c 65 61 76 65  of segment leave
0420: 73 0a 23 20 74 6f 20 66 69 6e 64 20 61 20 73 65  s.# to find a se
0430: 74 20 6f 66 20 64 6f 63 75 6d 65 6e 74 73 20 74  t of documents t
0440: 68 61 74 20 6d 61 74 63 68 65 73 20 61 20 74 65  hat matches a te
0450: 72 6d 2c 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  rm, it allocates
0460: 20 61 20 73 74 72 69 6e 67 0a 23 20 63 6f 6e 74   a string.# cont
0470: 61 69 6e 69 6e 67 20 74 68 65 20 74 65 78 74 20  aining the text 
0480: 6f 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20  of the required 
0490: 53 51 4c 2c 20 61 6e 64 20 63 6f 6d 70 69 6c 65  SQL, and compile
04a0: 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 0a 23  s one or more .#
04b0: 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 74   statements to t
04c0: 72 61 76 65 72 73 65 20 74 68 65 20 6c 65 61 76  raverse the leav
04d0: 65 73 2e 20 54 68 69 73 20 74 65 73 74 20 63 61  es. This test ca
04e0: 73 65 20 74 65 73 74 73 20 74 68 61 74 20 4f 4f  se tests that OO
04f0: 4d 20 65 72 72 6f 72 73 0a 23 20 74 68 61 74 20  M errors.# that 
0500: 6f 63 63 75 72 20 77 68 69 6c 65 20 61 6c 6c 6f  occur while allo
0510: 63 61 74 69 6e 67 20 74 68 69 73 20 73 74 72 69  cating this stri
0520: 6e 67 20 61 6e 64 20 73 74 61 74 65 6d 65 6e 74  ng and statement
0530: 20 61 72 65 20 68 61 6e 64 6c 65 64 20 63 6f 72   are handled cor
0540: 72 65 63 74 6c 79 0a 23 20 61 6c 73 6f 2e 0a 23  rectly.# also..#
0550: 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76  .do_test fts3cov
0560: 2d 31 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  -1.1 {.  execsql
0570: 20 7b 20 0a 20 20 20 20 43 52 45 41 54 45 20 56   { .    CREATE V
0580: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 20  IRTUAL TABLE t1 
0590: 55 53 49 4e 47 20 66 74 73 33 28 78 29 3b 0a 20  USING fts3(x);. 
05a0: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
05b0: 31 28 74 31 29 20 56 41 4c 55 45 53 28 27 6e 6f  1(t1) VALUES('no
05c0: 64 65 73 69 7a 65 3d 32 34 27 29 3b 0a 20 20 20  desize=24');.   
05d0: 20 42 45 47 49 4e 3b 0a 20 20 20 20 20 20 49 4e   BEGIN;.      IN
05e0: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
05f0: 55 45 53 28 27 49 73 20 74 68 65 20 6e 69 67 68  UES('Is the nigh
0600: 74 20 63 68 69 6c 6c 79 20 61 6e 64 20 64 61 72  t chilly and dar
0610: 6b 3f 27 29 3b 0a 20 20 20 20 20 20 49 4e 53 45  k?');.      INSE
0620: 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45  RT INTO t1 VALUE
0630: 53 28 27 54 68 65 20 6e 69 67 68 74 20 69 73 20  S('The night is 
0640: 63 68 69 6c 6c 79 2c 20 62 75 74 20 6e 6f 74 20  chilly, but not 
0650: 64 61 72 6b 2e 27 29 3b 0a 20 20 20 20 20 20 49  dark.');.      I
0660: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
0670: 4c 55 45 53 28 27 54 68 65 20 74 68 69 6e 20 67  LUES('The thin g
0680: 72 61 79 20 63 6c 6f 75 64 20 69 73 20 73 70 72  ray cloud is spr
0690: 65 61 64 20 6f 6e 20 68 69 67 68 2c 27 29 3b 0a  ead on high,');.
06a0: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
06b0: 4f 20 74 31 20 56 41 4c 55 45 53 28 27 49 74 20  O t1 VALUES('It 
06c0: 63 6f 76 65 72 73 20 62 75 74 20 6e 6f 74 20 68  covers but not h
06d0: 69 64 65 73 20 74 68 65 20 73 6b 79 2e 27 29 3b  ides the sky.');
06e0: 0a 20 20 20 20 43 4f 4d 4d 49 54 3b 0a 20 20 20  .    COMMIT;.   
06f0: 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29   SELECT count(*)
0700: 3e 30 20 46 52 4f 4d 20 74 31 5f 73 65 67 6d 65  >0 FROM t1_segme
0710: 6e 74 73 3b 0a 20 20 7d 0a 7d 20 7b 31 7d 0a 0a  nts;.  }.} {1}..
0720: 73 65 74 20 44 4f 5f 4d 41 4c 4c 4f 43 5f 54 45  set DO_MALLOC_TE
0730: 53 54 20 31 0a 64 6f 5f 72 65 73 74 61 72 74 5f  ST 1.do_restart_
0740: 73 65 6c 65 63 74 5f 74 65 73 74 20 66 74 73 33  select_test fts3
0750: 63 6f 76 2d 31 2e 32 20 7b 0a 20 20 53 45 4c 45  cov-1.2 {.  SELE
0760: 43 54 20 64 6f 63 69 64 20 46 52 4f 4d 20 74 31  CT docid FROM t1
0770: 20 57 48 45 52 45 20 74 31 20 4d 41 54 43 48 20   WHERE t1 MATCH 
0780: 27 63 68 69 6c 6c 79 27 3b 0a 7d 20 7b 31 20 32  'chilly';.} {1 2
0790: 7d 0a 73 65 74 20 44 4f 5f 4d 41 4c 4c 4f 43 5f  }.set DO_MALLOC_
07a0: 54 45 53 54 20 30 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  TEST 0..#-------
07b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
07c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
07d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
07e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
07f0: 2d 2d 2d 0a 23 20 57 68 65 6e 20 71 75 65 72 79  ---.# When query
0800: 69 6e 67 20 74 68 65 20 66 75 6c 6c 2d 74 65 78  ing the full-tex
0810: 74 20 69 6e 64 65 78 2c 20 69 66 20 61 6e 20 65  t index, if an e
0820: 78 70 65 63 74 65 64 20 69 6e 74 65 72 6e 61 6c  xpected internal
0830: 20 6e 6f 64 65 20 62 6c 6f 63 6b 20 69 73 20 0a   node block is .
0840: 23 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 74  # missing from t
0850: 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61  he %_segments ta
0860: 62 6c 65 2c 20 6f 72 20 69 66 20 61 20 4e 55 4c  ble, or if a NUL
0870: 4c 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  L value is store
0880: 64 20 69 6e 20 74 68 65 20 0a 23 20 25 5f 73 65  d in the .# %_se
0890: 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 73  gments table ins
08a0: 74 65 61 64 20 6f 66 20 61 20 62 69 6e 61 72 79  tead of a binary
08b0: 20 62 6c 6f 62 2c 20 64 61 74 61 62 61 73 65 20   blob, database 
08c0: 63 6f 72 72 75 70 74 69 6f 6e 20 73 68 6f 75 6c  corruption shoul
08d0: 64 20 62 65 20 0a 23 20 72 65 70 6f 72 74 65 64  d be .# reported
08e0: 2e 0a 23 0a 23 20 45 76 65 6e 20 77 69 74 68 20  ..#.# Even with 
08f0: 74 69 6e 79 20 32 34 20 62 79 74 65 20 6e 6f 64  tiny 24 byte nod
0900: 65 73 2c 20 69 74 20 74 61 6b 65 73 20 61 20 66  es, it takes a f
0910: 61 69 72 20 62 69 74 20 6f 66 20 64 61 74 61 20  air bit of data 
0920: 74 6f 20 70 72 6f 64 75 63 65 20 61 0a 23 20 73  to produce a.# s
0930: 65 67 6d 65 6e 74 20 62 2d 74 72 65 65 20 74 68  egment b-tree th
0940: 61 74 20 75 73 65 73 20 74 68 65 20 25 5f 73 65  at uses the %_se
0950: 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 74 6f 20  gments table to 
0960: 73 74 6f 72 65 20 69 6e 74 65 72 6e 61 6c 20 6e  store internal n
0970: 6f 64 65 73 2e 20 0a 23 0a 64 6f 5f 74 65 73 74  odes. .#.do_test
0980: 20 66 74 73 33 63 6f 76 2d 32 2e 31 20 7b 0a 20   fts3cov-2.1 {. 
0990: 20 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49   execsql {.    I
09a0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 28 74 31  NSERT INTO t1(t1
09b0: 29 20 56 41 4c 55 45 53 28 27 6e 6f 64 65 73 69  ) VALUES('nodesi
09c0: 7a 65 3d 32 34 27 29 3b 0a 20 20 20 20 42 45 47  ze=24');.    BEG
09d0: 49 4e 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54  IN;.      INSERT
09e0: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
09f0: 27 54 68 65 20 6d 6f 6f 6e 20 69 73 20 62 65 68  'The moon is beh
0a00: 69 6e 64 2c 20 61 6e 64 20 61 74 20 74 68 65 20  ind, and at the 
0a10: 66 75 6c 6c 3b 27 29 3b 0a 20 20 20 20 20 20 49  full;');.      I
0a20: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
0a30: 4c 55 45 53 28 27 41 6e 64 20 79 65 74 20 73 68  LUES('And yet sh
0a40: 65 20 6c 6f 6f 6b 73 20 62 6f 74 68 20 73 6d 61  e looks both sma
0a50: 6c 6c 20 61 6e 64 20 64 75 6c 6c 2e 27 29 3b 0a  ll and dull.');.
0a60: 20 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54        INSERT INT
0a70: 4f 20 74 31 20 56 41 4c 55 45 53 28 27 54 68 65  O t1 VALUES('The
0a80: 20 6e 69 67 68 74 20 69 73 20 63 68 69 6c 6c 2c   night is chill,
0a90: 20 74 68 65 20 63 6c 6f 75 64 20 69 73 20 67 72   the cloud is gr
0aa0: 61 79 3a 27 29 3b 0a 20 20 20 20 20 20 49 4e 53  ay:');.      INS
0ab0: 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
0ac0: 45 53 28 27 27 27 54 20 69 73 20 61 20 6d 6f 6e  ES('''T is a mon
0ad0: 74 68 20 62 65 66 6f 72 65 20 74 68 65 20 6d 6f  th before the mo
0ae0: 6e 74 68 20 6f 66 20 4d 61 79 2c 27 29 3b 0a 20  nth of May,');. 
0af0: 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
0b00: 20 74 31 20 56 41 4c 55 45 53 28 27 41 6e 64 20   t1 VALUES('And 
0b10: 74 68 65 20 53 70 72 69 6e 67 20 63 6f 6d 65 73  the Spring comes
0b20: 20 73 6c 6f 77 6c 79 20 75 70 20 74 68 69 73 20   slowly up this 
0b30: 77 61 79 2e 27 29 3b 0a 20 20 20 20 20 20 49 4e  way.');.      IN
0b40: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
0b50: 55 45 53 28 27 54 68 65 20 6c 6f 76 65 6c 79 20  UES('The lovely 
0b60: 6c 61 64 79 2c 20 43 68 72 69 73 74 61 62 65 6c  lady, Christabel
0b70: 2c 27 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52  ,');.      INSER
0b80: 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53  T INTO t1 VALUES
0b90: 28 27 57 68 6f 6d 20 68 65 72 20 66 61 74 68 65  ('Whom her fathe
0ba0: 72 20 6c 6f 76 65 73 20 73 6f 20 77 65 6c 6c 2c  r loves so well,
0bb0: 27 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54  ');.      INSERT
0bc0: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
0bd0: 27 57 68 61 74 20 6d 61 6b 65 73 20 68 65 72 20  'What makes her 
0be0: 69 6e 20 74 68 65 20 77 6f 6f 64 20 73 6f 20 6c  in the wood so l
0bf0: 61 74 65 2c 27 29 3b 0a 20 20 20 20 20 20 49 4e  ate,');.      IN
0c00: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
0c10: 55 45 53 28 27 41 20 66 75 72 6c 6f 6e 67 20 66  UES('A furlong f
0c20: 72 6f 6d 20 74 68 65 20 63 61 73 74 6c 65 20 67  rom the castle g
0c30: 61 74 65 3f 27 29 3b 0a 20 20 20 20 20 20 49 4e  ate?');.      IN
0c40: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
0c50: 55 45 53 28 27 53 68 65 20 68 61 64 20 64 72 65  UES('She had dre
0c60: 61 6d 73 20 61 6c 6c 20 79 65 73 74 65 72 6e 69  ams all yesterni
0c70: 67 68 74 27 29 3b 0a 20 20 20 20 20 20 49 4e 53  ght');.      INS
0c80: 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c 55  ERT INTO t1 VALU
0c90: 45 53 28 27 4f 66 20 68 65 72 20 6f 77 6e 20 62  ES('Of her own b
0ca0: 65 74 72 6f 74 68 65 64 20 6b 6e 69 67 68 74 3b  etrothed knight;
0cb0: 27 29 3b 0a 20 20 20 20 20 20 49 4e 53 45 52 54  ');.      INSERT
0cc0: 20 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28   INTO t1 VALUES(
0cd0: 27 41 6e 64 20 73 68 65 20 69 6e 20 74 68 65 20  'And she in the 
0ce0: 6d 69 64 6e 69 67 68 74 20 77 6f 6f 64 20 77 69  midnight wood wi
0cf0: 6c 6c 20 70 72 61 79 27 29 3b 0a 20 20 20 20 20  ll pray');.     
0d00: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20   INSERT INTO t1 
0d10: 56 41 4c 55 45 53 28 27 46 6f 72 20 74 68 65 20  VALUES('For the 
0d20: 77 65 61 6c 20 6f 66 20 68 65 72 20 6c 6f 76 65  weal of her love
0d30: 72 20 74 68 61 74 27 27 73 20 66 61 72 20 61 77  r that''s far aw
0d40: 61 79 2e 27 29 3b 0a 20 20 20 20 43 4f 4d 4d 49  ay.');.    COMMI
0d50: 54 3b 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c  T;.  }.  execsql
0d60: 20 7b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e   {.    INSERT IN
0d70: 54 4f 20 74 31 28 74 31 29 20 56 41 4c 55 45 53  TO t1(t1) VALUES
0d80: 28 27 6f 70 74 69 6d 69 7a 65 27 29 3b 0a 20 20  ('optimize');.  
0d90: 20 20 53 45 4c 45 43 54 20 73 75 62 73 74 72 28    SELECT substr(
0da0: 68 65 78 28 72 6f 6f 74 29 2c 20 31 2c 20 32 29  hex(root), 1, 2)
0db0: 20 46 52 4f 4d 20 74 31 5f 73 65 67 64 69 72 3b   FROM t1_segdir;
0dc0: 0a 20 20 7d 0a 7d 20 7b 30 33 7d 0a 0a 23 20 54  .  }.} {03}..# T
0dd0: 65 73 74 20 74 68 65 20 22 6d 69 73 73 69 6e 67  est the "missing
0de0: 20 65 6e 74 72 79 22 20 63 61 73 65 3a 0a 73 71   entry" case:.sq
0df0: 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 20  lite3_db_config 
0e00: 64 62 20 44 45 46 45 4e 53 49 56 45 20 30 0a 64  db DEFENSIVE 0.d
0e10: 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 32  o_test fts3cov-2
0e20: 2e 32 20 7b 0a 20 20 73 65 74 20 72 6f 6f 74 20  .2 {.  set root 
0e30: 5b 64 62 20 6f 6e 65 20 7b 53 45 4c 45 43 54 20  [db one {SELECT 
0e40: 72 6f 6f 74 20 46 52 4f 4d 20 74 31 5f 73 65 67  root FROM t1_seg
0e50: 64 69 72 7d 5d 0a 20 20 72 65 61 64 5f 66 74 73  dir}].  read_fts
0e60: 33 76 61 72 69 6e 74 20 5b 73 74 72 69 6e 67 20  3varint [string 
0e70: 72 61 6e 67 65 20 24 72 6f 6f 74 20 31 20 65 6e  range $root 1 en
0e80: 64 5d 20 6c 65 66 74 5f 63 68 69 6c 64 0a 20 20  d] left_child.  
0e90: 65 78 65 63 73 71 6c 20 7b 20 44 45 4c 45 54 45  execsql { DELETE
0ea0: 20 46 52 4f 4d 20 74 31 5f 73 65 67 6d 65 6e 74   FROM t1_segment
0eb0: 73 20 57 48 45 52 45 20 62 6c 6f 63 6b 69 64 20  s WHERE blockid 
0ec0: 3d 20 24 6c 65 66 74 5f 63 68 69 6c 64 20 7d 0a  = $left_child }.
0ed0: 7d 20 7b 7d 0a 64 6f 5f 65 72 72 6f 72 5f 74 65  } {}.do_error_te
0ee0: 73 74 20 66 74 73 33 63 6f 76 2d 32 2e 33 20 7b  st fts3cov-2.3 {
0ef0: 0a 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  .  SELECT * FROM
0f00: 20 74 31 20 57 48 45 52 45 20 74 31 20 4d 41 54   t1 WHERE t1 MAT
0f10: 43 48 20 27 63 2a 27 0a 7d 20 7b 53 51 4c 20 6c  CH 'c*'.} {SQL l
0f20: 6f 67 69 63 20 65 72 72 6f 72 7d 0a 0a 23 20 54  ogic error}..# T
0f30: 65 73 74 20 74 68 65 20 22 72 65 70 6c 61 63 65  est the "replace
0f40: 64 20 77 69 74 68 20 4e 55 4c 4c 22 20 63 61 73  d with NULL" cas
0f50: 65 3a 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63  e:.do_test fts3c
0f60: 6f 76 2d 32 2e 34 20 7b 0a 20 20 65 78 65 63 73  ov-2.4 {.  execs
0f70: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
0f80: 20 74 31 5f 73 65 67 6d 65 6e 74 73 20 56 41 4c   t1_segments VAL
0f90: 55 45 53 28 24 6c 65 66 74 5f 63 68 69 6c 64 2c  UES($left_child,
0fa0: 20 4e 55 4c 4c 29 20 7d 0a 7d 20 7b 7d 0a 73 71   NULL) }.} {}.sq
0fb0: 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 20  lite3_db_config 
0fc0: 64 62 20 44 45 46 45 4e 53 49 56 45 20 31 0a 64  db DEFENSIVE 1.d
0fd0: 6f 5f 65 72 72 6f 72 5f 74 65 73 74 20 66 74 73  o_error_test fts
0fe0: 33 63 6f 76 2d 32 2e 35 20 7b 0a 20 20 53 45 4c  3cov-2.5 {.  SEL
0ff0: 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48  ECT * FROM t1 WH
1000: 45 52 45 20 74 31 20 4d 41 54 43 48 20 27 63 6c  ERE t1 MATCH 'cl
1010: 6f 75 64 27 0a 7d 20 7b 53 51 4c 20 6c 6f 67 69  oud'.} {SQL logi
1020: 63 20 65 72 72 6f 72 7d 0a 0a 23 2d 2d 2d 2d 2d  c error}..#-----
1030: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1040: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1050: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1060: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1070: 2d 2d 2d 2d 2d 0a 23 20 54 68 65 20 66 6f 6c 6c  -----.# The foll
1080: 6f 77 69 6e 67 20 74 65 73 74 73 20 61 72 65 20  owing tests are 
1090: 74 6f 20 74 65 73 74 20 74 68 65 20 65 66 66 65  to test the effe
10a0: 63 74 73 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72  cts of OOM error
10b0: 73 20 77 68 69 6c 65 20 73 74 6f 72 69 6e 67 0a  s while storing.
10c0: 23 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 70  # terms in the p
10d0: 65 6e 64 69 6e 67 2d 68 61 73 68 20 74 61 62 6c  ending-hash tabl
10e0: 65 2e 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  e. Specifically,
10f0: 20 77 68 69 6c 65 20 63 72 65 61 74 69 6e 67 20   while creating 
1100: 64 6f 63 6c 69 73 74 0a 23 20 62 6c 6f 62 73 20  doclist.# blobs 
1110: 74 6f 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20  to store in the 
1120: 74 61 62 6c 65 2e 20 4d 6f 72 65 20 73 70 65 63  table. More spec
1130: 69 66 69 63 61 6c 6c 79 2c 20 74 6f 20 74 65 73  ifically, to tes
1140: 74 20 4f 4f 4d 20 65 72 72 6f 72 73 20 77 68 69  t OOM errors whi
1150: 6c 65 0a 23 20 61 70 70 65 6e 64 69 6e 67 20 63  le.# appending c
1160: 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 74 6f  olumn numbers to
1170: 20 64 6f 63 6c 69 73 74 73 2e 20 46 6f 72 20 65   doclists. For e
1180: 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 64 6f 63  xample, if a doc
1190: 6c 69 73 74 20 63 6f 6e 73 69 73 74 73 0a 23 20  list consists.# 
11a0: 6f 66 3a 0a 23 0a 23 20 20 20 3c 64 6f 63 69 64  of:.#.#   <docid
11b0: 3e 20 3c 63 6f 6c 75 6d 6e 20 30 20 6f 66 66 73  > <column 0 offs
11c0: 65 74 2d 6c 69 73 74 3e 20 30 78 30 31 20 3c 63  et-list> 0x01 <c
11d0: 6f 6c 75 6d 6e 20 4e 3e 20 3c 63 6f 6c 75 6d 6e  olumn N> <column
11e0: 20 4e 20 6f 66 66 73 65 74 2d 6c 69 73 74 3e 0a   N offset-list>.
11f0: 23 0a 23 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  #.# The followin
1200: 67 20 74 65 73 74 73 20 63 68 65 63 6b 20 74 68  g tests check th
1210: 61 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  at malloc errors
1220: 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69   encountered whi
1230: 6c 65 20 61 70 70 65 6e 64 69 6e 67 0a 23 20 74  le appending.# t
1240: 68 65 20 22 30 78 30 31 20 3c 63 6f 6c 75 6d 6e  he "0x01 <column
1250: 20 4e 3e 22 20 64 61 74 61 20 74 6f 20 74 68 65   N>" data to the
1260: 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 72 6f   dynamically gro
1270: 77 61 62 6c 65 20 62 6c 6f 62 20 75 73 65 64 20  wable blob used 
1280: 74 6f 20 0a 23 20 61 63 63 75 6d 75 6c 61 74 65  to .# accumulate
1290: 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20   the doclist in 
12a0: 6d 65 6d 6f 72 79 20 61 72 65 20 68 61 6e 64 6c  memory are handl
12b0: 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 23 0a  ed correctly..#.
12c0: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
12d0: 33 2e 31 20 7b 0a 20 20 73 65 74 20 63 6f 6c 73  3.1 {.  set cols
12e0: 20 5b 6c 69 73 74 5d 0a 20 20 73 65 74 20 76 61   [list].  set va
12f0: 6c 73 20 5b 6c 69 73 74 5d 0a 20 20 66 6f 72 20  ls [list].  for 
1300: 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c 20  {set i 0} {$i < 
1310: 31 32 30 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a  120} {incr i} {.
1320: 20 20 20 20 6c 61 70 70 65 6e 64 20 63 6f 6c 73      lappend cols
1330: 20 22 63 6f 6c 24 69 22 0a 20 20 20 20 6c 61 70   "col$i".    lap
1340: 70 65 6e 64 20 76 61 6c 73 20 22 27 77 6f 72 64  pend vals "'word
1350: 27 22 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c  '".  }.  execsql
1360: 20 22 43 52 45 41 54 45 20 56 49 52 54 55 41 4c   "CREATE VIRTUAL
1370: 20 54 41 42 4c 45 20 74 32 20 55 53 49 4e 47 20   TABLE t2 USING 
1380: 66 74 73 33 28 5b 6a 6f 69 6e 20 24 63 6f 6c 73  fts3([join $cols
1390: 20 2c 5d 29 22 0a 7d 20 7b 7d 0a 73 65 74 20 44   ,])".} {}.set D
13a0: 4f 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 31 20  O_MALLOC_TEST 1 
13b0: 0a 64 6f 5f 77 72 69 74 65 5f 74 65 73 74 20 66  .do_write_test f
13c0: 74 73 33 63 6f 76 2d 33 2e 32 20 74 32 5f 63 6f  ts3cov-3.2 t2_co
13d0: 6e 74 65 6e 74 20 22 0a 20 20 49 4e 53 45 52 54  ntent ".  INSERT
13e0: 20 49 4e 54 4f 20 74 32 28 64 6f 63 69 64 2c 20   INTO t2(docid, 
13f0: 5b 6a 6f 69 6e 20 24 63 6f 6c 73 20 2c 5d 29 20  [join $cols ,]) 
1400: 56 41 4c 55 45 53 28 31 2c 20 5b 6a 6f 69 6e 20  VALUES(1, [join 
1410: 24 76 61 6c 73 20 2c 5d 29 0a 22 0a 64 6f 5f 77  $vals ,]).".do_w
1420: 72 69 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f  rite_test fts3co
1430: 76 2d 33 2e 33 20 74 32 5f 63 6f 6e 74 65 6e 74  v-3.3 t2_content
1440: 20 22 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f   ".  INSERT INTO
1450: 20 74 32 28 64 6f 63 69 64 2c 20 5b 6a 6f 69 6e   t2(docid, [join
1460: 20 24 63 6f 6c 73 20 2c 5d 29 20 56 41 4c 55 45   $cols ,]) VALUE
1470: 53 28 32 30 30 2c 20 5b 6a 6f 69 6e 20 24 76 61  S(200, [join $va
1480: 6c 73 20 2c 5d 29 0a 22 0a 64 6f 5f 77 72 69 74  ls ,]).".do_writ
1490: 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 33  e_test fts3cov-3
14a0: 2e 34 20 74 32 5f 63 6f 6e 74 65 6e 74 20 22 0a  .4 t2_content ".
14b0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32    INSERT INTO t2
14c0: 28 64 6f 63 69 64 2c 20 5b 6a 6f 69 6e 20 24 63  (docid, [join $c
14d0: 6f 6c 73 20 2c 5d 29 20 56 41 4c 55 45 53 28 36  ols ,]) VALUES(6
14e0: 30 30 30 30 2c 20 5b 6a 6f 69 6e 20 24 76 61 6c  0000, [join $val
14f0: 73 20 2c 5d 29 0a 22 0a 0a 23 2d 2d 2d 2d 2d 2d  s ,])."..#------
1500: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1510: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1520: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1530: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1540: 2d 2d 2d 0a 23 20 49 66 20 74 6f 6f 20 6d 75 63  ---.# If too muc
1550: 68 20 64 61 74 61 20 61 63 63 75 6d 75 6c 61 74  h data accumulat
1560: 65 73 20 69 6e 20 74 68 65 20 70 65 6e 64 69 6e  es in the pendin
1570: 67 2d 74 65 72 6d 73 20 68 61 73 68 20 74 61 62  g-terms hash tab
1580: 6c 65 2c 20 69 74 20 69 73 0a 23 20 66 6c 75 73  le, it is.# flus
1590: 68 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62  hed to the datab
15a0: 61 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ase automaticall
15b0: 79 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 74  y, even if the t
15c0: 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e  ransaction has n
15d0: 6f 74 0a 23 20 66 69 6e 69 73 68 65 64 2e 20 54  ot.# finished. T
15e0: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73  he following tes
15f0: 74 73 20 63 68 65 63 6b 20 74 68 65 20 65 66 66  ts check the eff
1600: 65 63 74 73 20 6f 66 20 65 6e 63 6f 75 6e 74 65  ects of encounte
1610: 72 69 6e 67 20 61 6e 20 4f 4f 4d 20 0a 23 20 77  ring an OOM .# w
1620: 68 69 6c 65 20 64 6f 69 6e 67 20 74 68 69 73 2e  hile doing this.
1630: 0a 23 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63  .#.do_test fts3c
1640: 6f 76 2d 34 2e 31 20 7b 0a 20 20 65 78 65 63 73  ov-4.1 {.  execs
1650: 71 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20  ql {.    CREATE 
1660: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 33  VIRTUAL TABLE t3
1670: 20 55 53 49 4e 47 20 66 74 73 33 28 78 29 3b 0a   USING fts3(x);.
1680: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
1690: 74 33 28 74 33 29 20 56 41 4c 55 45 53 28 27 6e  t3(t3) VALUES('n
16a0: 6f 64 65 73 69 7a 65 3d 32 34 27 29 3b 0a 20 20  odesize=24');.  
16b0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 33    INSERT INTO t3
16c0: 28 74 33 29 20 56 41 4c 55 45 53 28 27 6d 61 78  (t3) VALUES('max
16d0: 70 65 6e 64 69 6e 67 3d 31 30 30 27 29 3b 0a 20  pending=100');. 
16e0: 20 7d 0a 7d 20 7b 7d 0a 73 65 74 20 44 4f 5f 4d   }.} {}.set DO_M
16f0: 41 4c 4c 4f 43 5f 54 45 53 54 20 31 20 0a 64 6f  ALLOC_TEST 1 .do
1700: 5f 77 72 69 74 65 5f 74 65 73 74 20 66 74 73 33  _write_test fts3
1710: 63 6f 76 2d 34 2e 32 20 74 33 5f 63 6f 6e 74 65  cov-4.2 t3_conte
1720: 6e 74 20 7b 0a 20 20 49 4e 53 45 52 54 20 49 4e  nt {.  INSERT IN
1730: 54 4f 20 74 33 28 64 6f 63 69 64 2c 20 78 29 0a  TO t3(docid, x).
1740: 20 20 20 20 53 45 4c 45 43 54 20 31 2c 20 27 54      SELECT 1, 'T
1750: 68 65 6e 20 43 68 72 69 73 74 61 62 65 6c 20 73  hen Christabel s
1760: 74 72 65 74 63 68 65 64 20 66 6f 72 74 68 20 68  tretched forth h
1770: 65 72 20 68 61 6e 64 2c 27 20 55 4e 49 4f 4e 20  er hand,' UNION 
1780: 41 4c 4c 0a 20 20 20 20 53 45 4c 45 43 54 20 33  ALL.    SELECT 3
1790: 2c 20 27 41 6e 64 20 63 6f 6d 66 6f 72 74 65 64  , 'And comforted
17a0: 20 66 61 69 72 20 47 65 72 61 6c 64 69 6e 65 3a   fair Geraldine:
17b0: 27 20 20 20 20 20 20 20 20 20 20 20 20 20 55 4e  '             UN
17c0: 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c 45  ION ALL.    SELE
17d0: 43 54 20 34 2c 20 27 27 27 4f 20 77 65 6c 6c 2c  CT 4, '''O well,
17e0: 20 62 72 69 67 68 74 20 64 61 6d 65 2c 20 6d 61   bright dame, ma
17f0: 79 20 79 6f 75 20 63 6f 6d 6d 61 6e 64 27 20 20  y you command'  
1800: 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20    UNION ALL.    
1810: 53 45 4c 45 43 54 20 35 2c 20 27 54 68 65 20 73  SELECT 5, 'The s
1820: 65 72 76 69 63 65 20 6f 66 20 53 69 72 20 4c 65  ervice of Sir Le
1830: 6f 6c 69 6e 65 3b 27 20 20 20 20 20 20 20 20 20  oline;'         
1840: 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a        UNION ALL.
1850: 20 20 20 20 53 45 4c 45 43 54 20 32 2c 20 27 41      SELECT 2, 'A
1860: 6e 64 20 67 6c 61 64 6c 79 20 6f 75 72 20 73 74  nd gladly our st
1870: 6f 75 74 20 63 68 69 76 61 6c 72 79 27 20 20 20  out chivalry'   
1880: 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20            UNION 
1890: 41 4c 4c 0a 20 20 20 20 53 45 4c 45 43 54 20 37  ALL.    SELECT 7
18a0: 2c 20 27 57 69 6c 6c 20 68 65 20 73 65 6e 64 20  , 'Will he send 
18b0: 66 6f 72 74 68 2c 20 61 6e 64 20 66 72 69 65 6e  forth, and frien
18c0: 64 73 20 77 69 74 68 61 6c 2c 27 20 20 20 55 4e  ds withal,'   UN
18d0: 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c 45  ION ALL.    SELE
18e0: 43 54 20 38 2c 20 27 54 6f 20 67 75 69 64 65 20  CT 8, 'To guide 
18f0: 61 6e 64 20 67 75 61 72 64 20 79 6f 75 20 73 61  and guard you sa
1900: 66 65 20 61 6e 64 20 66 72 65 65 27 20 20 20 20  fe and free'    
1910: 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20    UNION ALL.    
1920: 53 45 4c 45 43 54 20 36 2c 20 27 48 6f 6d 65 20  SELECT 6, 'Home 
1930: 74 6f 20 79 6f 75 72 20 6e 6f 62 6c 65 20 66 61  to your noble fa
1940: 74 68 65 72 27 27 73 20 68 61 6c 6c 2e 27 27 27  ther''s hall.'''
1950: 0a 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .}..#-----------
1960: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1970: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1980: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1990: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
19a0: 20 57 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 74   When building t
19b0: 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 72 65 65  he internal tree
19c0: 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65   structure for e
19d0: 61 63 68 20 73 65 67 6d 65 6e 74 20 62 2d 74 72  ach segment b-tr
19e0: 65 65 2c 20 46 54 53 33 0a 23 20 61 73 73 75 6d  ee, FTS3.# assum
19f0: 65 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74  es that the cont
1a00: 65 6e 74 20 6f 66 20 65 61 63 68 20 69 6e 74 65  ent of each inte
1a10: 72 6e 61 6c 20 6e 6f 64 65 20 77 69 6c 6c 20 62  rnal node will b
1a20: 65 20 6c 65 73 73 20 74 68 61 6e 0a 23 20 24 6e  e less than.# $n
1a30: 6f 64 65 73 69 7a 65 20 62 79 74 65 73 2c 20 77  odesize bytes, w
1a40: 68 65 72 65 20 24 6e 6f 64 65 73 69 7a 65 20 69  here $nodesize i
1a50: 73 20 74 68 65 20 61 64 76 69 73 6f 72 79 20 6e  s the advisory n
1a60: 6f 64 65 20 73 69 7a 65 2e 20 49 66 20 74 68 69  ode size. If thi
1a70: 73 20 74 75 72 6e 73 0a 23 20 6f 75 74 20 74 6f  s turns.# out to
1a80: 20 62 65 20 75 6e 74 72 75 65 2c 20 74 68 65 6e   be untrue, then
1a90: 20 61 6e 20 65 78 74 72 61 20 62 75 66 66 65 72   an extra buffer
1aa0: 20 6d 75 73 74 20 62 65 20 6d 61 6c 6c 6f 63 27   must be malloc'
1ab0: 64 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e  d for each term.
1ac0: 0a 23 20 54 68 69 73 20 74 65 73 74 20 63 61 73  .# This test cas
1ad0: 65 20 74 65 73 74 73 20 74 68 65 73 65 20 70 61  e tests these pa
1ae0: 74 68 73 20 61 6e 64 20 74 68 65 20 65 66 66 65  ths and the effe
1af0: 63 74 73 20 6f 66 20 73 61 69 64 20 6d 61 6c 6c  cts of said mall
1b00: 6f 63 73 20 66 61 69 6c 69 6e 67 0a 23 20 62 79  ocs failing.# by
1b10: 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 73 65 72   inserting inser
1b20: 74 20 61 20 64 6f 63 75 6d 65 6e 74 20 77 69 74  t a document wit
1b30: 68 20 73 6f 6d 65 20 66 61 69 72 6c 79 20 6c 61  h some fairly la
1b40: 72 67 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 61  rge terms into a
1b50: 0a 23 20 66 75 6c 6c 2d 74 65 78 74 20 74 61 62  .# full-text tab
1b60: 6c 65 20 77 69 74 68 20 61 20 76 65 72 79 20 73  le with a very s
1b70: 6d 61 6c 6c 20 6e 6f 64 65 2d 73 69 7a 65 2e 20  mall node-size. 
1b80: 0a 23 0a 23 20 54 65 73 74 20 74 68 69 73 20 68  .#.# Test this h
1b90: 61 6e 64 6c 69 6e 67 20 6f 66 20 6c 61 72 67 65  andling of large
1ba0: 20 74 65 72 6d 73 20 69 6e 20 74 68 72 65 65 20   terms in three 
1bb0: 63 6f 6e 74 65 78 74 73 3a 0a 23 0a 23 20 20 20  contexts:.#.#   
1bc0: 31 2e 20 57 68 65 6e 20 66 6c 75 73 68 69 6e 67  1. When flushing
1bd0: 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72   the pending-ter
1be0: 6d 73 20 74 61 62 6c 65 2e 0a 23 20 20 20 32 2e  ms table..#   2.
1bf0: 20 57 68 65 6e 20 6f 70 74 69 6d 69 7a 69 6e 67   When optimizing
1c00: 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
1c10: 75 72 65 73 20 75 73 69 6e 67 20 74 68 65 20 49  ures using the I
1c20: 4e 53 45 52 54 20 73 79 6e 74 61 78 2e 20 0a 23  NSERT syntax. .#
1c30: 20 20 20 32 2e 20 57 68 65 6e 20 6f 70 74 69 6d     2. When optim
1c40: 69 7a 69 6e 67 20 74 68 65 20 64 61 74 61 20 73  izing the data s
1c50: 74 72 75 63 74 75 72 65 73 20 75 73 69 6e 67 20  tructures using 
1c60: 74 68 65 20 64 65 70 72 65 63 61 74 65 64 20 53  the deprecated S
1c70: 45 4c 45 43 54 20 73 79 6e 74 61 78 2e 20 0a 23  ELECT syntax. .#
1c80: 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76  .do_test fts3cov
1c90: 2d 35 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  -5.1 {.  execsql
1ca0: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 56 49   {.    CREATE VI
1cb0: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 34 20 55  RTUAL TABLE t4 U
1cc0: 53 49 4e 47 20 66 74 73 33 28 78 29 3b 0a 20 20  SING fts3(x);.  
1cd0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 34    INSERT INTO t4
1ce0: 28 74 34 29 20 56 41 4c 55 45 53 28 27 6e 6f 64  (t4) VALUES('nod
1cf0: 65 73 69 7a 65 3d 32 34 27 29 3b 0a 20 20 7d 0a  esize=24');.  }.
1d00: 7d 20 7b 7d 0a 73 65 74 20 44 4f 5f 4d 41 4c 4c  } {}.set DO_MALL
1d10: 4f 43 5f 54 45 53 54 20 31 0a 0a 23 20 54 65 73  OC_TEST 1..# Tes
1d20: 74 20 77 68 65 6e 20 66 6c 75 73 68 69 6e 67 20  t when flushing 
1d30: 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 74 61  pending-terms ta
1d40: 62 6c 65 2e 0a 64 6f 5f 77 72 69 74 65 5f 74 65  ble..do_write_te
1d50: 73 74 20 66 74 73 33 63 6f 76 2d 35 2e 32 20 74  st fts3cov-5.2 t
1d60: 34 5f 63 6f 6e 74 65 6e 74 20 7b 0a 20 20 49 4e  4_content {.  IN
1d70: 53 45 52 54 20 49 4e 54 4f 20 74 34 0a 20 20 20  SERT INTO t4.   
1d80: 20 53 45 4c 45 43 54 20 27 49 74 69 73 61 6e 61   SELECT 'Itisana
1d90: 6e 63 69 65 6e 74 4d 61 72 69 6e 65 72 41 6e 64  ncientMarinerAnd
1da0: 68 65 73 74 6f 70 70 65 74 68 6f 6e 65 6f 66 74  hestoppethoneoft
1db0: 68 72 65 65 41 41 27 20 55 4e 49 4f 4e 20 41 4c  hreeAA' UNION AL
1dc0: 4c 0a 20 20 20 20 53 45 4c 45 43 54 20 27 49 74  L.    SELECT 'It
1dd0: 69 73 61 6e 61 6e 63 69 65 6e 74 4d 61 72 69 6e  isanancientMarin
1de0: 65 72 41 6e 64 68 65 73 74 6f 70 70 65 74 68 6f  erAndhestoppetho
1df0: 6e 65 6f 66 74 68 72 65 65 42 42 27 20 55 4e 49  neofthreeBB' UNI
1e00: 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c 45 43  ON ALL.    SELEC
1e10: 54 20 27 49 74 69 73 61 6e 61 6e 63 69 65 6e 74  T 'Itisanancient
1e20: 4d 61 72 69 6e 65 72 41 6e 64 68 65 73 74 6f 70  MarinerAndhestop
1e30: 70 65 74 68 6f 6e 65 6f 66 74 68 72 65 65 43 43  pethoneofthreeCC
1e40: 27 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20  ' UNION ALL.    
1e50: 53 45 4c 45 43 54 20 27 42 79 74 68 79 6c 6f 6e  SELECT 'Bythylon
1e60: 67 67 72 65 79 62 65 61 72 64 61 6e 64 67 6c 69  ggreybeardandgli
1e70: 74 74 65 72 69 6e 67 65 79 65 4e 6f 77 77 68 65  tteringeyeNowwhe
1e80: 72 65 66 6f 72 65 73 74 6f 70 70 73 74 41 41 27  reforestoppstAA'
1e90: 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53   UNION ALL.    S
1ea0: 45 4c 45 43 54 20 27 42 79 74 68 79 6c 6f 6e 67  ELECT 'Bythylong
1eb0: 67 72 65 79 62 65 61 72 64 61 6e 64 67 6c 69 74  greybeardandglit
1ec0: 74 65 72 69 6e 67 65 79 65 4e 6f 77 77 68 65 72  teringeyeNowwher
1ed0: 65 66 6f 72 65 73 74 6f 70 70 73 74 42 42 27 20  eforestoppstBB' 
1ee0: 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45  UNION ALL.    SE
1ef0: 4c 45 43 54 20 27 42 79 74 68 79 6c 6f 6e 67 67  LECT 'Bythylongg
1f00: 72 65 79 62 65 61 72 64 61 6e 64 67 6c 69 74 74  reybeardandglitt
1f10: 65 72 69 6e 67 65 79 65 4e 6f 77 77 68 65 72 65  eringeyeNowwhere
1f20: 66 6f 72 65 73 74 6f 70 70 73 74 43 43 27 0a 7d  forestoppstCC'.}
1f30: 0a 0a 23 20 54 65 73 74 20 77 68 65 6e 20 6f 70  ..# Test when op
1f40: 74 69 6d 69 7a 69 6e 67 20 76 69 61 20 49 4e 53  timizing via INS
1f50: 45 52 54 2e 0a 64 6f 5f 74 65 73 74 20 66 74 73  ERT..do_test fts
1f60: 33 63 6f 76 2d 35 2e 33 20 7b 20 65 78 65 63 73  3cov-5.3 { execs
1f70: 71 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f  ql { INSERT INTO
1f80: 20 74 34 20 56 41 4c 55 45 53 28 27 65 78 74 72   t4 VALUES('extr
1f90: 61 21 27 29 20 7d 20 7d 20 7b 7d 0a 64 6f 5f 77  a!') } } {}.do_w
1fa0: 72 69 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f  rite_test fts3co
1fb0: 76 2d 35 2e 32 20 74 34 5f 73 65 67 6d 65 6e 74  v-5.2 t4_segment
1fc0: 73 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  s { INSERT INTO 
1fd0: 74 34 28 74 34 29 20 56 41 4c 55 45 53 28 27 6f  t4(t4) VALUES('o
1fe0: 70 74 69 6d 69 7a 65 27 29 20 7d 0a 0a 23 20 54  ptimize') }..# T
1ff0: 65 73 74 20 77 68 65 6e 20 6f 70 74 69 6d 69 7a  est when optimiz
2000: 69 6e 67 20 76 69 61 20 53 45 4c 45 43 54 2e 0a  ing via SELECT..
2010: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
2020: 35 2e 35 20 7b 20 65 78 65 63 73 71 6c 20 7b 20  5.5 { execsql { 
2030: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 34 20 56  INSERT INTO t4 V
2040: 41 4c 55 45 53 28 27 6d 6f 72 65 20 65 78 74 72  ALUES('more extr
2050: 61 21 27 29 20 7d 20 7d 20 7b 7d 0a 64 6f 5f 77  a!') } } {}.do_w
2060: 72 69 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f  rite_test fts3co
2070: 76 2d 35 2e 36 20 74 34 5f 73 65 67 6d 65 6e 74  v-5.6 t4_segment
2080: 73 20 7b 0a 20 20 53 45 4c 45 43 54 20 2a 20 46  s {.  SELECT * F
2090: 52 4f 4d 20 28 53 45 4c 45 43 54 20 6f 70 74 69  ROM (SELECT opti
20a0: 6d 69 7a 65 28 74 34 29 20 46 52 4f 4d 20 74 34  mize(t4) FROM t4
20b0: 20 4c 49 4d 49 54 20 31 29 0a 20 20 45 58 43 45   LIMIT 1).  EXCE
20c0: 50 54 20 53 45 4c 45 43 54 20 27 49 6e 64 65 78  PT SELECT 'Index
20d0: 20 6f 70 74 69 6d 69 7a 65 64 27 0a 7d 0a 0a 23   optimized'.}..#
20e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2100: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 57 68 65 6e  ---------.# When
2130: 20 6d 65 72 67 69 6e 67 20 61 6c 6c 20 73 65 67   merging all seg
2140: 6d 65 6e 74 73 20 61 74 20 61 20 67 69 76 65 6e  ments at a given
2150: 20 6c 65 76 65 6c 20 74 6f 20 63 72 65 61 74 65   level to create
2160: 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e   a single segmen
2170: 74 0a 23 20 61 74 20 6c 65 76 65 6c 2b 31 2c 20  t.# at level+1, 
2180: 46 54 53 33 20 72 75 6e 73 20 61 20 71 75 65 72  FTS3 runs a quer
2190: 79 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 23  y of the form:.#
21a0: 0a 23 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e  .#   SELECT coun
21b0: 74 28 2a 29 20 46 52 4f 4d 20 25 5f 73 65 67 64  t(*) FROM %_segd
21c0: 69 72 20 57 48 45 52 45 20 6c 65 76 65 6c 20 3d  ir WHERE level =
21d0: 20 3f 0a 23 0a 23 20 54 68 65 20 71 75 65 72 79   ?.#.# The query
21e0: 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 68 65   is compiled the
21f0: 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
2200: 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65   operation is re
2210: 71 75 69 72 65 64 20 61 6e 64 0a 23 20 72 65 75  quired and.# reu
2220: 73 65 64 20 74 68 65 72 65 61 66 74 65 72 2e 20  sed thereafter. 
2230: 54 68 69 73 20 74 65 73 74 20 61 69 6d 73 20 74  This test aims t
2240: 6f 20 74 65 73 74 20 74 68 65 20 65 66 66 65 63  o test the effec
2250: 74 73 20 6f 66 20 61 6e 20 4f 4f 4d 20 77 68 69  ts of an OOM whi
2260: 6c 65 0a 23 20 70 72 65 70 61 72 69 6e 67 20 61  le.# preparing a
2270: 6e 64 20 65 78 65 63 75 74 69 6e 67 20 74 68 69  nd executing thi
2280: 73 20 71 75 65 72 79 20 66 6f 72 20 74 68 65 20  s query for the 
2290: 66 69 72 73 74 20 74 69 6d 65 2e 0a 23 0a 23 20  first time..#.# 
22a0: 54 68 65 6e 2c 20 6b 65 65 70 20 69 6e 73 65 72  Then, keep inser
22b0: 74 69 6e 67 20 72 6f 77 73 20 69 6e 74 6f 20 74  ting rows into t
22c0: 68 65 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74  he table so that
22d0: 20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20   the effects of 
22e0: 61 6e 20 4f 4f 4d 0a 23 20 77 68 69 6c 65 20 72  an OOM.# while r
22f0: 65 2d 65 78 65 63 75 74 69 6e 67 20 74 68 65 20  e-executing the 
2300: 73 61 6d 65 20 71 75 65 72 79 20 63 61 6e 20 61  same query can a
2310: 6c 73 6f 20 62 65 20 74 65 73 74 65 64 2e 0a 23  lso be tested..#
2320: 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76  .do_test fts3cov
2330: 2d 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  -6.1 {.  execsql
2340: 20 7b 20 43 52 45 41 54 45 20 56 49 52 54 55 41   { CREATE VIRTUA
2350: 4c 20 54 41 42 4c 45 20 74 35 20 55 53 49 4e 47  L TABLE t5 USING
2360: 20 66 74 73 33 28 78 29 20 7d 0a 20 20 66 6f 72   fts3(x) }.  for
2370: 20 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 3c 31   {set i 0} {$i<1
2380: 36 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 20 65 78  6} {incr i} { ex
2390: 65 63 73 71 6c 20 22 49 4e 53 45 52 54 20 49 4e  ecsql "INSERT IN
23a0: 54 4f 20 74 35 20 56 41 4c 55 45 53 28 27 74 65  TO t5 VALUES('te
23b0: 72 6d 24 69 27 29 22 20 7d 0a 20 20 65 78 65 63  rm$i')" }.  exec
23c0: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f 75  sql { SELECT cou
23d0: 6e 74 28 2a 29 20 46 52 4f 4d 20 74 35 5f 73 65  nt(*) FROM t5_se
23e0: 67 64 69 72 20 7d 0a 7d 20 7b 31 36 7d 0a 0a 23  gdir }.} {16}..#
23f0: 20 46 69 72 73 74 20 74 69 6d 65 2e 0a 64 62 20   First time..db 
2400: 63 6c 6f 73 65 0a 73 71 6c 69 74 65 33 20 64 62  close.sqlite3 db
2410: 20 74 65 73 74 2e 64 62 0a 64 6f 5f 77 72 69 74   test.db.do_writ
2420: 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 36  e_test fts3cov-6
2430: 2e 32 20 74 35 5f 63 6f 6e 74 65 6e 74 20 7b 0a  .2 t5_content {.
2440: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 35    INSERT INTO t5
2450: 20 56 41 4c 55 45 53 28 27 73 65 67 6d 65 6e 74   VALUES('segment
2460: 20 6e 75 6d 62 65 72 20 31 36 21 27 29 3b 0a 7d   number 16!');.}
2470: 0a 0a 23 20 53 65 63 6f 6e 64 20 74 69 6d 65 2e  ..# Second time.
2480: 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76  .do_test fts3cov
2490: 2d 36 2e 33 20 7b 0a 20 20 66 6f 72 20 7b 73 65  -6.3 {.  for {se
24a0: 74 20 69 20 31 7d 20 7b 24 69 3c 31 36 7d 20 7b  t i 1} {$i<16} {
24b0: 69 6e 63 72 20 69 7d 20 7b 20 65 78 65 63 73 71  incr i} { execsq
24c0: 6c 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74  l "INSERT INTO t
24d0: 35 20 56 41 4c 55 45 53 28 27 74 65 72 6d 24 69  5 VALUES('term$i
24e0: 27 29 22 20 7d 0a 20 20 65 78 65 63 73 71 6c 20  ')" }.  execsql 
24f0: 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a  { SELECT count(*
2500: 29 20 46 52 4f 4d 20 74 35 5f 73 65 67 64 69 72  ) FROM t5_segdir
2510: 20 7d 0a 7d 20 7b 31 37 7d 0a 64 6f 5f 77 72 69   }.} {17}.do_wri
2520: 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  te_test fts3cov-
2530: 36 2e 34 20 74 35 5f 63 6f 6e 74 65 6e 74 20 7b  6.4 t5_content {
2540: 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74  .  INSERT INTO t
2550: 35 20 56 41 4c 55 45 53 28 27 73 65 67 6d 65 6e  5 VALUES('segmen
2560: 74 20 6e 75 6d 62 65 72 20 31 36 21 27 29 3b 0a  t number 16!');.
2570: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
2580: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2590: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
25c0: 55 70 64 61 74 65 20 74 68 65 20 64 6f 63 69 64  Update the docid
25d0: 20 6f 66 20 61 20 72 6f 77 2e 20 54 65 73 74 20   of a row. Test 
25e0: 74 68 69 73 20 69 6e 20 74 77 6f 20 73 63 65 6e  this in two scen
25f0: 61 72 69 6f 73 3a 0a 23 0a 23 20 20 20 31 2e 20  arios:.#.#   1. 
2600: 57 68 65 6e 20 74 68 65 20 72 6f 77 20 62 65 69  When the row bei
2610: 6e 67 20 75 70 64 61 74 65 64 20 69 73 20 74 68  ng updated is th
2620: 65 20 6f 6e 6c 79 20 72 6f 77 20 69 6e 20 74 68  e only row in th
2630: 65 20 74 61 62 6c 65 2e 0a 23 20 20 20 32 2e 20  e table..#   2. 
2640: 57 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 2e 0a  When it is not..
2650: 23 0a 23 20 54 68 65 20 74 77 6f 20 63 61 73 65  #.# The two case
2660: 73 20 61 62 6f 76 65 20 74 61 6b 65 20 64 69 66  s above take dif
2670: 66 65 72 65 6e 74 20 70 61 74 68 73 20 62 65 63  ferent paths bec
2680: 61 75 73 65 20 69 6e 20 63 61 73 65 20 31 20 61  ause in case 1 a
2690: 6c 6c 20 64 61 74 61 20 0a 23 20 73 74 72 75 63  ll data .# struc
26a0: 74 75 72 65 73 20 63 61 6e 20 73 69 6d 70 6c 79  tures can simply
26b0: 20 62 65 20 65 6d 70 74 69 65 64 20 62 65 66 6f   be emptied befo
26c0: 72 65 20 69 6e 73 65 72 74 69 6e 67 20 74 68 65  re inserting the
26d0: 20 6e 65 77 20 72 6f 77 20 72 65 63 6f 72 64 2e   new row record.
26e0: 0a 23 20 49 6e 20 63 61 73 65 20 32 2c 20 74 68  .# In case 2, th
26f0: 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
2700: 73 20 61 63 74 75 61 6c 6c 79 20 68 61 76 65 20  s actually have 
2710: 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 23  to be updated..#
2720: 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76  .do_test fts3cov
2730: 2d 37 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  -7.1 {.  execsql
2740: 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 56 49   {.    CREATE VI
2750: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 37 20 55  RTUAL TABLE t7 U
2760: 53 49 4e 47 20 66 74 73 33 28 61 2c 20 62 2c 20  SING fts3(a, b, 
2770: 63 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49  c);.    INSERT I
2780: 4e 54 4f 20 74 37 20 56 41 4c 55 45 53 28 27 41  NTO t7 VALUES('A
2790: 27 2c 20 27 42 27 2c 20 27 43 27 29 3b 0a 20 20  ', 'B', 'C');.  
27a0: 20 20 55 50 44 41 54 45 20 74 37 20 53 45 54 20    UPDATE t7 SET 
27b0: 64 6f 63 69 64 20 3d 20 35 3b 0a 20 20 20 20 53  docid = 5;.    S
27c0: 45 4c 45 43 54 20 64 6f 63 69 64 2c 20 2a 20 46  ELECT docid, * F
27d0: 52 4f 4d 20 74 37 3b 0a 20 20 7d 0a 7d 20 7b 35  ROM t7;.  }.} {5
27e0: 20 41 20 42 20 43 7d 0a 64 6f 5f 74 65 73 74 20   A B C}.do_test 
27f0: 66 74 73 33 63 6f 76 2d 37 2e 32 20 7b 0a 20 20  fts3cov-7.2 {.  
2800: 65 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e  execsql {.    IN
2810: 53 45 52 54 20 49 4e 54 4f 20 74 37 20 56 41 4c  SERT INTO t7 VAL
2820: 55 45 53 28 27 44 27 2c 20 27 45 27 2c 20 27 46  UES('D', 'E', 'F
2830: 27 29 3b 0a 20 20 20 20 55 50 44 41 54 45 20 74  ');.    UPDATE t
2840: 37 20 53 45 54 20 64 6f 63 69 64 20 3d 20 31 20  7 SET docid = 1 
2850: 57 48 45 52 45 20 64 6f 63 69 64 20 3d 20 36 3b  WHERE docid = 6;
2860: 0a 20 20 20 20 53 45 4c 45 43 54 20 64 6f 63 69  .    SELECT doci
2870: 64 2c 20 2a 20 46 52 4f 4d 20 74 37 3b 0a 20 20  d, * FROM t7;.  
2880: 7d 0a 7d 20 7b 31 20 44 20 45 20 46 20 35 20 41  }.} {1 D E F 5 A
2890: 20 42 20 43 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d   B C}..#--------
28a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
28e0: 2d 0a 23 20 49 66 20 61 20 73 65 74 20 6f 66 20  -.# If a set of 
28f0: 64 6f 63 75 6d 65 6e 74 73 20 61 72 65 20 6d 6f  documents are mo
2900: 64 69 66 69 65 64 20 77 69 74 68 69 6e 20 61 20  dified within a 
2910: 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
2920: 20 0a 23 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d   .# pending-term
2930: 73 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  s table must be 
2940: 66 6c 75 73 68 65 64 20 65 61 63 68 20 74 69 6d  flushed each tim
2950: 65 20 61 20 64 6f 63 75 6d 65 6e 74 20 77 69 74  e a document wit
2960: 68 20 61 20 64 6f 63 69 64 0a 23 20 6c 65 73 73  h a docid.# less
2970: 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
2980: 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64  o the previous d
2990: 6f 63 69 64 20 69 73 20 6d 6f 64 69 66 69 65 64  ocid is modified
29a0: 2e 20 0a 23 0a 23 20 54 68 69 73 20 74 65 73 74  . .#.# This test
29b0: 20 63 68 65 63 6b 73 20 74 68 65 20 65 66 66 65   checks the effe
29c0: 63 74 73 20 6f 66 20 61 6e 20 4f 4f 4d 20 65 72  cts of an OOM er
29d0: 72 6f 72 20 6f 63 63 75 72 69 6e 67 20 77 68 65  ror occuring whe
29e0: 6e 20 74 68 65 20 0a 23 20 70 65 6e 64 69 6e 67  n the .# pending
29f0: 2d 74 65 72 6d 73 20 74 61 62 6c 65 20 69 73 20  -terms table is 
2a00: 66 6c 75 73 68 65 64 20 66 6f 72 20 74 68 69 73  flushed for this
2a10: 20 72 65 61 73 6f 6e 20 61 73 20 70 61 72 74 20   reason as part 
2a20: 6f 66 20 61 20 44 45 4c 45 54 45 20 0a 23 20 73  of a DELETE .# s
2a30: 74 61 74 65 6d 65 6e 74 2e 0a 23 0a 64 6f 5f 6d  tatement..#.do_m
2a40: 61 6c 6c 6f 63 5f 74 65 73 74 20 66 74 73 33 63  alloc_test fts3c
2a50: 6f 76 2d 38 20 2d 73 71 6c 70 72 65 70 20 7b 0a  ov-8 -sqlprep {.
2a60: 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 43 52 45    BEGIN;.    CRE
2a70: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
2a80: 45 20 74 38 20 55 53 49 4e 47 20 66 74 73 33 3b  E t8 USING fts3;
2a90: 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  .    INSERT INTO
2aa0: 20 74 38 20 56 41 4c 55 45 53 28 27 74 68 65 20   t8 VALUES('the 
2ab0: 6f 75 74 70 75 74 20 6f 66 20 65 61 63 68 20 62  output of each b
2ac0: 61 74 63 68 20 72 75 6e 27 29 3b 0a 20 20 20 20  atch run');.    
2ad0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 38 20 56  INSERT INTO t8 V
2ae0: 41 4c 55 45 53 28 27 28 70 6f 73 73 69 62 6c 79  ALUES('(possibly
2af0: 20 61 20 64 61 79 27 27 73 20 77 6f 72 6b 29 27   a day''s work)'
2b00: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
2b10: 54 4f 20 74 38 20 56 41 4c 55 45 53 28 27 77 61  TO t8 VALUES('wa
2b20: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 77 6f  s written to two
2b30: 20 73 65 70 61 72 61 74 65 20 64 69 73 6b 73 27   separate disks'
2b40: 29 3b 0a 20 20 43 4f 4d 4d 49 54 3b 0a 7d 20 2d  );.  COMMIT;.} -
2b50: 73 71 6c 62 6f 64 79 20 7b 0a 20 20 42 45 47 49  sqlbody {.  BEGI
2b60: 4e 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  N;.    DELETE FR
2b70: 4f 4d 20 74 38 20 57 48 45 52 45 20 72 6f 77 69  OM t8 WHERE rowi
2b80: 64 20 3d 20 33 3b 0a 20 20 20 20 44 45 4c 45 54  d = 3;.    DELET
2b90: 45 20 46 52 4f 4d 20 74 38 20 57 48 45 52 45 20  E FROM t8 WHERE 
2ba0: 72 6f 77 69 64 20 3d 20 32 3b 0a 20 20 20 20 44  rowid = 2;.    D
2bb0: 45 4c 45 54 45 20 46 52 4f 4d 20 74 38 20 57 48  ELETE FROM t8 WH
2bc0: 45 52 45 20 72 6f 77 69 64 20 3d 20 31 3b 0a 20  ERE rowid = 1;. 
2bd0: 20 43 4f 4d 4d 49 54 3b 0a 7d 0a 0a 23 2d 2d 2d   COMMIT;.}..#---
2be0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2bf0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c20: 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73 74 20 73 6f  ------.# Test so
2c30: 6d 65 20 62 72 61 6e 63 68 65 73 20 69 6e 20 74  me branches in t
2c40: 68 65 20 63 6f 64 65 20 74 68 61 74 20 68 61 6e  he code that han
2c50: 64 6c 65 73 20 22 73 70 65 63 69 61 6c 22 20 69  dles "special" i
2c60: 6e 73 65 72 74 73 20 6c 69 6b 65 3a 0a 23 0a 23  nserts like:.#.#
2c70: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
2c80: 31 28 74 31 29 20 56 41 4c 55 45 53 28 27 6f 70  1(t1) VALUES('op
2c90: 74 69 6d 69 7a 65 27 29 3b 0a 23 0a 23 20 41 6c  timize');.#.# Al
2ca0: 73 6f 20 74 65 73 74 20 74 68 61 74 20 61 6e 20  so test that an 
2cb0: 6f 70 74 69 6d 69 7a 65 20 28 49 4e 53 45 52 54  optimize (INSERT
2cc0: 20 6d 65 74 68 6f 64 29 20 77 6f 72 6b 73 20 6f   method) works o
2cd0: 6e 20 61 6e 20 65 6d 70 74 79 20 74 61 62 6c 65  n an empty table
2ce0: 2e 0a 23 0a 73 65 74 20 44 4f 5f 4d 41 4c 4c 4f  ..#.set DO_MALLO
2cf0: 43 5f 54 45 53 54 20 30 0a 64 6f 5f 74 65 73 74  C_TEST 0.do_test
2d00: 20 66 74 73 33 63 6f 76 2d 39 2e 31 20 7b 0a 20   fts3cov-9.1 {. 
2d10: 20 65 78 65 63 73 71 6c 20 7b 20 43 52 45 41 54   execsql { CREAT
2d20: 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
2d30: 78 78 20 55 53 49 4e 47 20 66 74 73 33 20 7d 0a  xx USING fts3 }.
2d40: 7d 20 7b 7d 0a 64 6f 5f 65 72 72 6f 72 5f 74 65  } {}.do_error_te
2d50: 73 74 20 66 74 73 33 63 6f 76 2d 39 2e 32 20 7b  st fts3cov-9.2 {
2d60: 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78  .  INSERT INTO x
2d70: 78 28 78 78 29 20 56 41 4c 55 45 53 28 27 6f 70  x(xx) VALUES('op
2d80: 74 69 6d 69 73 65 27 29 3b 20 20 20 2d 2d 20 42  timise');   -- B
2d90: 72 69 74 69 73 68 20 73 70 65 6c 6c 69 6e 67 0a  ritish spelling.
2da0: 7d 20 7b 53 51 4c 20 6c 6f 67 69 63 20 65 72 72  } {SQL logic err
2db0: 6f 72 7d 0a 64 6f 5f 65 72 72 6f 72 5f 74 65 73  or}.do_error_tes
2dc0: 74 20 66 74 73 33 63 6f 76 2d 39 2e 33 20 7b 0a  t fts3cov-9.3 {.
2dd0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78    INSERT INTO xx
2de0: 28 78 78 29 20 56 41 4c 55 45 53 28 27 73 68 6f  (xx) VALUES('sho
2df0: 72 74 27 29 3b 0a 7d 20 7b 53 51 4c 20 6c 6f 67  rt');.} {SQL log
2e00: 69 63 20 65 72 72 6f 72 7d 0a 64 6f 5f 65 72 72  ic error}.do_err
2e10: 6f 72 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  or_test fts3cov-
2e20: 39 2e 34 20 7b 0a 20 20 49 4e 53 45 52 54 20 49  9.4 {.  INSERT I
2e30: 4e 54 4f 20 78 78 28 78 78 29 20 56 41 4c 55 45  NTO xx(xx) VALUE
2e40: 53 28 27 77 61 79 74 6f 6f 6c 6f 6e 67 74 6f 62  S('waytoolongtob
2e50: 65 63 6f 72 72 65 63 74 27 29 3b 0a 7d 20 7b 53  ecorrect');.} {S
2e60: 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 7d 0a  QL logic error}.
2e70: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
2e80: 39 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  9.5 {.  execsql 
2e90: 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78  { INSERT INTO xx
2ea0: 28 78 78 29 20 56 41 4c 55 45 53 28 27 6f 70 74  (xx) VALUES('opt
2eb0: 69 6d 69 7a 65 27 29 20 7d 0a 7d 20 7b 7d 0a 0a  imize') }.} {}..
2ec0: 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  #---------------
2ed0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ee0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ef0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2f00: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73  ----------.# Tes
2f10: 74 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 63  t that a table c
2f20: 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20  an be optimized 
2f30: 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2f40: 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77   a transaction w
2f50: 68 65 6e 0a 23 20 74 68 65 20 70 65 6e 64 69 6e  hen.# the pendin
2f60: 67 2d 74 65 72 6d 73 20 74 61 62 6c 65 20 69 73  g-terms table is
2f70: 20 6e 6f 6e 2d 65 6d 70 74 79 2e 20 54 68 69 73   non-empty. This
2f80: 20 63 61 73 65 20 69 6e 76 6f 6c 76 65 73 20 73   case involves s
2f90: 6f 6d 65 20 65 78 74 72 61 0a 23 20 62 72 61 6e  ome extra.# bran
2fa0: 63 68 65 73 20 62 65 63 61 75 73 65 20 64 61 74  ches because dat
2fb0: 61 20 6d 75 73 74 20 62 65 20 72 65 61 64 20 6e  a must be read n
2fc0: 6f 74 20 6f 6e 6c 79 20 66 72 6f 6d 20 74 68 65  ot only from the
2fd0: 20 64 61 74 61 62 61 73 65 2c 20 62 75 74 0a 23   database, but.#
2fe0: 20 61 6c 73 6f 20 66 72 6f 6d 20 74 68 65 20 70   also from the p
2ff0: 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 74 61 62  ending-terms tab
3000: 6c 65 2e 0a 23 0a 64 6f 5f 6d 61 6c 6c 6f 63 5f  le..#.do_malloc_
3010: 74 65 73 74 20 66 74 73 33 63 6f 76 2d 31 30 20  test fts3cov-10 
3020: 2d 73 71 6c 70 72 65 70 20 7b 0a 20 20 43 52 45  -sqlprep {.  CRE
3030: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
3040: 45 20 74 31 30 20 55 53 49 4e 47 20 66 74 73 33  E t10 USING fts3
3050: 3b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  ;.  INSERT INTO 
3060: 74 31 30 20 56 41 4c 55 45 53 28 27 4f 70 74 69  t10 VALUES('Opti
3070: 6d 69 73 69 6e 67 20 69 6d 61 67 65 73 20 66 6f  mising images fo
3080: 72 20 74 68 65 20 77 65 62 20 69 73 20 61 20 74  r the web is a t
3090: 72 69 63 6b 79 20 62 75 73 69 6e 65 73 73 27 29  ricky business')
30a0: 3b 0a 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 49  ;.  BEGIN;.    I
30b0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 30 20 56  NSERT INTO t10 V
30c0: 41 4c 55 45 53 28 27 59 6f 75 20 68 61 76 65 20  ALUES('You have 
30d0: 74 6f 20 67 65 74 20 74 68 65 20 72 69 67 68 74  to get the right
30e0: 20 62 61 6c 61 6e 63 65 20 62 65 74 77 65 65 6e   balance between
30f0: 27 29 3b 0a 7d 20 2d 73 71 6c 62 6f 64 79 20 7b  ');.} -sqlbody {
3100: 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74  .  INSERT INTO t
3110: 31 30 28 74 31 30 29 20 56 41 4c 55 45 53 28 27  10(t10) VALUES('
3120: 6f 70 74 69 6d 69 7a 65 27 29 3b 0a 7d 0a 0a 23  optimize');.}..#
3130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3140: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3150: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3160: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3170: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 65 73 74  ---------.# Test
3180: 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65   a full-text que
3190: 72 79 20 66 6f 72 20 61 20 74 65 72 6d 20 74 68  ry for a term th
31a0: 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 74  at was once in t
31b0: 68 65 20 69 6e 64 65 78 2c 20 62 75 74 20 69 73  he index, but is
31c0: 0a 23 20 6e 6f 20 6c 6f 6e 67 65 72 2e 0a 23 0a  .# no longer..#.
31d0: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
31e0: 31 31 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c  11.1 {.  execsql
31f0: 20 7b 20 0a 20 20 20 20 43 52 45 41 54 45 20 56   { .    CREATE V
3200: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 78 78 20  IRTUAL TABLE xx 
3210: 55 53 49 4e 47 20 66 74 73 33 3b 0a 20 20 20 20  USING fts3;.    
3220: 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78 20 56  INSERT INTO xx V
3230: 41 4c 55 45 53 28 27 6f 6e 65 20 74 77 6f 20 74  ALUES('one two t
3240: 68 72 65 65 27 29 3b 0a 20 20 20 20 49 4e 53 45  hree');.    INSE
3250: 52 54 20 49 4e 54 4f 20 78 78 20 56 41 4c 55 45  RT INTO xx VALUE
3260: 53 28 27 66 6f 75 72 20 66 69 76 65 20 73 69 78  S('four five six
3270: 27 29 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  ');.    DELETE F
3280: 52 4f 4d 20 78 78 20 57 48 45 52 45 20 64 6f 63  ROM xx WHERE doc
3290: 69 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 65 78  id = 1;.  }.  ex
32a0: 65 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a  ecsql { SELECT *
32b0: 20 46 52 4f 4d 20 78 78 20 57 48 45 52 45 20 78   FROM xx WHERE x
32c0: 78 20 4d 41 54 43 48 20 27 74 77 6f 27 20 7d 0a  x MATCH 'two' }.
32d0: 7d 20 7b 7d 0a 0a 0a 64 6f 5f 6d 61 6c 6c 6f 63  } {}...do_malloc
32e0: 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 31 32  _test fts3cov-12
32f0: 20 2d 73 71 6c 70 72 65 70 20 7b 0a 20 20 43 52   -sqlprep {.  CR
3300: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
3310: 4c 45 20 74 31 32 20 55 53 49 4e 47 20 66 74 73  LE t12 USING fts
3320: 33 3b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  3;.  INSERT INTO
3330: 20 74 31 32 20 56 41 4c 55 45 53 28 27 69 73 20   t12 VALUES('is 
3340: 6f 6e 65 20 6f 66 20 74 68 65 20 74 77 6f 20 74  one of the two t
3350: 6f 67 74 68 65 72 27 29 3b 0a 20 20 42 45 47 49  ogther');.  BEGI
3360: 4e 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  N;.    INSERT IN
3370: 54 4f 20 74 31 32 20 56 41 4c 55 45 53 28 27 6f  TO t12 VALUES('o
3380: 6e 65 20 77 68 69 63 68 20 77 61 73 20 61 70 70  ne which was app
3390: 72 6f 70 72 69 61 74 65 20 61 74 20 74 68 65 20  ropriate at the 
33a0: 74 69 6d 65 27 29 3b 0a 7d 20 2d 73 71 6c 62 6f  time');.} -sqlbo
33b0: 64 79 20 7b 0a 20 20 53 45 4c 45 43 54 20 2a 20  dy {.  SELECT * 
33c0: 46 52 4f 4d 20 74 31 32 20 57 48 45 52 45 20 74  FROM t12 WHERE t
33d0: 31 32 20 4d 41 54 43 48 20 27 6f 6e 65 27 0a 7d  12 MATCH 'one'.}
33e0: 0a 0a 64 6f 5f 6d 61 6c 6c 6f 63 5f 74 65 73 74  ..do_malloc_test
33f0: 20 66 74 73 33 63 6f 76 2d 31 33 20 2d 73 71 6c   fts3cov-13 -sql
3400: 70 72 65 70 20 7b 0a 20 20 50 52 41 47 4d 41 20  prep {.  PRAGMA 
3410: 65 6e 63 6f 64 69 6e 67 20 3d 20 27 55 54 46 2d  encoding = 'UTF-
3420: 31 36 27 3b 0a 20 20 43 52 45 41 54 45 20 56 49  16';.  CREATE VI
3430: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 33 20  RTUAL TABLE t13 
3440: 55 53 49 4e 47 20 66 74 73 33 3b 0a 20 20 49 4e  USING fts3;.  IN
3450: 53 45 52 54 20 49 4e 54 4f 20 74 31 33 20 56 41  SERT INTO t13 VA
3460: 4c 55 45 53 28 27 74 77 6f 20 73 63 61 6c 61 72  LUES('two scalar
3470: 20 66 75 6e 63 74 69 6f 6e 73 27 29 3b 0a 20 20   functions');.  
3480: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 33 20  INSERT INTO t13 
3490: 56 41 4c 55 45 53 28 27 73 63 61 6c 61 72 20 74  VALUES('scalar t
34a0: 77 6f 20 66 75 6e 63 74 69 6f 6e 73 27 29 3b 0a  wo functions');.
34b0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
34c0: 33 20 56 41 4c 55 45 53 28 27 66 75 6e 63 74 69  3 VALUES('functi
34d0: 6f 6e 73 20 73 63 61 6c 61 72 20 74 77 6f 27 29  ons scalar two')
34e0: 3b 0a 7d 20 2d 73 71 6c 62 6f 64 79 20 7b 0a 20  ;.} -sqlbody {. 
34f0: 20 53 45 4c 45 43 54 20 73 6e 69 70 70 65 74 28   SELECT snippet(
3500: 74 31 33 2c 20 27 25 25 27 2c 20 27 25 25 27 2c  t13, '%%', '%%',
3510: 20 27 23 27 29 20 46 52 4f 4d 20 74 31 33 20 57   '#') FROM t13 W
3520: 48 45 52 45 20 74 31 33 20 4d 41 54 43 48 20 27  HERE t13 MATCH '
3530: 74 77 6f 27 3b 0a 20 20 53 45 4c 45 43 54 20 73  two';.  SELECT s
3540: 6e 69 70 70 65 74 28 74 31 33 2c 20 27 25 25 27  nippet(t13, '%%'
3550: 2c 20 27 25 25 27 29 20 46 52 4f 4d 20 74 31 33  , '%%') FROM t13
3560: 20 57 48 45 52 45 20 74 31 33 20 4d 41 54 43 48   WHERE t13 MATCH
3570: 20 27 74 77 6f 27 3b 0a 20 20 53 45 4c 45 43 54   'two';.  SELECT
3580: 20 73 6e 69 70 70 65 74 28 74 31 33 2c 20 27 25   snippet(t13, '%
3590: 25 27 29 20 46 52 4f 4d 20 74 31 33 20 57 48 45  %') FROM t13 WHE
35a0: 52 45 20 74 31 33 20 4d 41 54 43 48 20 27 74 77  RE t13 MATCH 'tw
35b0: 6f 27 3b 0a 7d 0a 0a 64 6f 5f 65 78 65 63 73 71  o';.}..do_execsq
35c0: 6c 5f 74 65 73 74 20 31 34 2e 30 20 7b 0a 20 20  l_test 14.0 {.  
35d0: 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
35e0: 41 42 4c 45 20 74 31 34 20 55 53 49 4e 47 20 66  ABLE t14 USING f
35f0: 74 73 34 28 61 2c 20 62 29 3b 0a 20 20 49 4e 53  ts4(a, b);.  INS
3600: 45 52 54 20 49 4e 54 4f 20 74 31 34 20 56 41 4c  ERT INTO t14 VAL
3610: 55 45 53 28 27 6f 6e 65 20 74 77 6f 20 74 68 72  UES('one two thr
3620: 65 65 27 2c 20 27 6f 6e 65 20 74 68 72 65 65 20  ee', 'one three 
3630: 66 6f 75 72 27 29 3b 0a 20 20 49 4e 53 45 52 54  four');.  INSERT
3640: 20 49 4e 54 4f 20 74 31 34 20 56 41 4c 55 45 53   INTO t14 VALUES
3650: 28 27 61 20 62 20 63 27 2c 20 27 64 20 65 20 61  ('a b c', 'd e a
3660: 27 29 3b 0a 7d 0a 64 6f 5f 65 78 65 63 73 71 6c  ');.}.do_execsql
3670: 5f 74 65 73 74 20 31 34 2e 31 20 7b 0a 20 20 53  _test 14.1 {.  S
3680: 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d  ELECT rowid FROM
3690: 20 74 31 34 20 57 48 45 52 45 20 74 31 34 20 4d   t14 WHERE t14 M
36a0: 41 54 43 48 20 27 22 6f 6e 65 20 74 77 6f 20 74  ATCH '"one two t
36b0: 68 72 65 65 22 27 0a 7d 20 7b 31 7d 0a 64 6f 5f  hree"'.} {1}.do_
36c0: 65 78 65 63 73 71 6c 5f 74 65 73 74 20 31 34 2e  execsql_test 14.
36d0: 32 20 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f 77  2 {.  SELECT row
36e0: 69 64 20 46 52 4f 4d 20 74 31 34 20 57 48 45 52  id FROM t14 WHER
36f0: 45 20 74 31 34 20 4d 41 54 43 48 20 27 22 6f 6e  E t14 MATCH '"on
3700: 65 20 66 6f 75 72 22 27 0a 7d 20 7b 7d 0a 64 6f  e four"'.} {}.do
3710: 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20 31 34  _execsql_test 14
3720: 2e 33 20 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f  .3 {.  SELECT ro
3730: 77 69 64 20 46 52 4f 4d 20 74 31 34 20 57 48 45  wid FROM t14 WHE
3740: 52 45 20 74 31 34 20 4d 41 54 43 48 20 27 22 65  RE t14 MATCH '"e
3750: 20 61 22 27 0a 7d 20 7b 32 7d 0a 64 6f 5f 65 78   a"'.} {2}.do_ex
3760: 65 63 73 71 6c 5f 74 65 73 74 20 31 34 2e 35 20  ecsql_test 14.5 
3770: 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64  {.  SELECT rowid
3780: 20 46 52 4f 4d 20 74 31 34 20 57 48 45 52 45 20   FROM t14 WHERE 
3790: 74 31 34 20 4d 41 54 43 48 20 27 22 65 20 62 22  t14 MATCH '"e b"
37a0: 27 0a 7d 20 7b 7d 0a 64 6f 5f 63 61 74 63 68 73  '.} {}.do_catchs
37b0: 71 6c 5f 74 65 73 74 20 31 34 2e 36 20 7b 0a 20  ql_test 14.6 {. 
37c0: 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52   SELECT rowid FR
37d0: 4f 4d 20 74 31 34 20 57 48 45 52 45 20 72 6f 77  OM t14 WHERE row
37e0: 69 64 20 4d 41 54 43 48 20 27 6f 6e 65 27 0a 7d  id MATCH 'one'.}
37f0: 20 7b 31 20 7b 75 6e 61 62 6c 65 20 74 6f 20 75   {1 {unable to u
3800: 73 65 20 66 75 6e 63 74 69 6f 6e 20 4d 41 54 43  se function MATC
3810: 48 20 69 6e 20 74 68 65 20 72 65 71 75 65 73 74  H in the request
3820: 65 64 20 63 6f 6e 74 65 78 74 7d 7d 0a 64 6f 5f  ed context}}.do_
3830: 63 61 74 63 68 73 71 6c 5f 74 65 73 74 20 31 34  catchsql_test 14
3840: 2e 37 20 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f  .7 {.  SELECT ro
3850: 77 69 64 20 46 52 4f 4d 20 74 31 34 20 57 48 45  wid FROM t14 WHE
3860: 52 45 20 64 6f 63 69 64 20 4d 41 54 43 48 20 27  RE docid MATCH '
3870: 6f 6e 65 27 0a 7d 20 7b 31 20 7b 75 6e 61 62 6c  one'.} {1 {unabl
3880: 65 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f  e to use functio
3890: 6e 20 4d 41 54 43 48 20 69 6e 20 74 68 65 20 72  n MATCH in the r
38a0: 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74  equested context
38b0: 7d 7d 0a 0a 64 6f 5f 65 78 65 63 73 71 6c 5f 74  }}..do_execsql_t
38c0: 65 73 74 20 31 35 2e 30 20 7b 0a 20 20 43 52 45  est 15.0 {.  CRE
38d0: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
38e0: 45 20 74 31 35 20 55 53 49 4e 47 20 66 74 73 34  E t15 USING fts4
38f0: 28 61 2c 20 62 2c 20 63 29 3b 0a 20 20 49 4e 53  (a, b, c);.  INS
3900: 45 52 54 20 49 4e 54 4f 20 74 31 35 20 56 41 4c  ERT INTO t15 VAL
3910: 55 45 53 28 27 61 62 63 20 64 65 66 20 67 68 69  UES('abc def ghi
3920: 27 2c 20 27 61 62 63 32 20 64 65 66 32 20 67 68  ', 'abc2 def2 gh
3930: 69 32 27 2c 20 27 61 62 63 33 20 64 65 66 33 20  i2', 'abc3 def3 
3940: 67 68 69 33 27 29 3b 0a 20 20 49 4e 53 45 52 54  ghi3');.  INSERT
3950: 20 49 4e 54 4f 20 74 31 35 20 56 41 4c 55 45 53   INTO t15 VALUES
3960: 28 27 61 62 63 32 20 64 65 66 32 20 67 68 69 32  ('abc2 def2 ghi2
3970: 27 2c 20 27 61 62 63 32 20 64 65 66 32 20 67 68  ', 'abc2 def2 gh
3980: 69 32 27 2c 20 27 61 62 63 20 64 65 66 33 20 67  i2', 'abc def3 g
3990: 68 69 33 27 29 3b 0a 7d 0a 64 6f 5f 65 78 65 63  hi3');.}.do_exec
39a0: 73 71 6c 5f 74 65 73 74 20 31 35 2e 31 20 7b 0a  sql_test 15.1 {.
39b0: 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46    SELECT rowid F
39c0: 52 4f 4d 20 74 31 35 20 57 48 45 52 45 20 74 31  ROM t15 WHERE t1
39d0: 35 20 4d 41 54 43 48 20 27 22 61 62 63 2a 20 64  5 MATCH '"abc* d
39e0: 65 66 32 22 27 0a 7d 20 7b 31 20 32 7d 0a 0a 23  ef2"'.} {1 2}..#
39f0: 20 54 65 73 74 20 61 20 63 6f 72 72 75 70 74 69   Test a corrupti
3a00: 6f 6e 20 63 61 73 65 2e 0a 23 0a 73 71 6c 69 74  on case..#.sqlit
3a10: 65 33 5f 64 62 5f 63 6f 6e 66 69 67 20 64 62 20  e3_db_config db 
3a20: 44 45 46 45 4e 53 49 56 45 20 30 0a 64 6f 5f 65  DEFENSIVE 0.do_e
3a30: 78 65 63 73 71 6c 5f 74 65 73 74 20 31 36 2e 31  xecsql_test 16.1
3a40: 20 7b 0a 20 20 43 52 45 41 54 45 20 56 49 52 54   {.  CREATE VIRT
3a50: 55 41 4c 20 54 41 42 4c 45 20 74 31 36 20 55 53  UAL TABLE t16 US
3a60: 49 4e 47 20 66 74 73 34 3b 0a 20 20 49 4e 53 45  ING fts4;.  INSE
3a70: 52 54 20 49 4e 54 4f 20 74 31 36 20 56 41 4c 55  RT INTO t16 VALU
3a80: 45 53 28 27 74 68 65 6f 72 65 74 69 63 61 6c 20  ES('theoretical 
3a90: 77 6f 72 6b 20 74 6f 20 65 78 61 6d 69 6e 65 20  work to examine 
3aa0: 74 68 65 20 72 65 6c 61 74 69 6f 6e 73 68 69 70  the relationship
3ab0: 27 29 3b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54  ');.  INSERT INT
3ac0: 4f 20 74 31 36 20 56 41 4c 55 45 53 28 27 73 6f  O t16 VALUES('so
3ad0: 6c 75 74 69 6f 6e 20 6f 66 20 6f 75 72 20 70 72  lution of our pr
3ae0: 6f 62 6c 65 6d 73 20 6f 6e 20 74 68 65 20 69 6e  oblems on the in
3af0: 76 69 73 69 62 6c 65 27 29 3b 0a 20 20 44 45 4c  visible');.  DEL
3b00: 45 54 45 20 46 52 4f 4d 20 74 31 36 5f 63 6f 6e  ETE FROM t16_con
3b10: 74 65 6e 74 20 57 48 45 52 45 20 72 6f 77 69 64  tent WHERE rowid
3b20: 20 3d 20 32 3b 0a 7d 0a 64 6f 5f 63 61 74 63 68   = 2;.}.do_catch
3b30: 73 71 6c 5f 74 65 73 74 20 31 36 2e 32 20 7b 0a  sql_test 16.2 {.
3b40: 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
3b50: 74 31 36 20 57 48 45 52 45 20 74 31 36 20 4d 41  t16 WHERE t16 MA
3b60: 54 43 48 20 27 69 6e 76 69 73 69 62 6c 65 27 0a  TCH 'invisible'.
3b70: 7d 20 7b 31 20 7b 64 61 74 61 62 61 73 65 20 64  } {1 {database d
3b80: 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c  isk image is mal
3b90: 66 6f 72 6d 65 64 7d 7d 0a 0a 23 20 41 6e 64 20  formed}}..# And 
3ba0: 61 6e 6f 74 68 65 72 20 63 6f 72 72 75 70 74 69  another corrupti
3bb0: 6f 6e 20 74 65 73 74 20 63 61 73 65 2e 0a 23 0a  on test case..#.
3bc0: 64 6f 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20  do_execsql_test 
3bd0: 31 37 2e 31 20 7b 0a 20 20 43 52 45 41 54 45 20  17.1 {.  CREATE 
3be0: 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31  VIRTUAL TABLE t1
3bf0: 37 20 55 53 49 4e 47 20 66 74 73 34 3b 0a 20 20  7 USING fts4;.  
3c00: 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 37 28  INSERT INTO t17(
3c10: 63 6f 6e 74 65 6e 74 29 20 56 41 4c 55 45 53 28  content) VALUES(
3c20: 27 6f 6e 65 20 6f 6e 65 20 6f 6e 65 27 29 3b 0a  'one one one');.
3c30: 20 20 55 50 44 41 54 45 20 74 31 37 5f 73 65 67    UPDATE t17_seg
3c40: 64 69 72 20 53 45 54 20 72 6f 6f 74 20 3d 20 58  dir SET root = X
3c50: 27 30 30 30 33 36 46 36 45 36 35 46 46 46 46 46  '00036F6E65FFFFF
3c60: 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46  FFFFFFFFFFFFFFFF
3c70: 46 30 32 30 33 30 33 30 30 27 0a 7d 20 7b 7d 0a  F02030300'.} {}.
3c80: 64 6f 5f 63 61 74 63 68 73 71 6c 5f 74 65 73 74  do_catchsql_test
3c90: 20 31 37 2e 32 20 7b 0a 20 20 53 45 4c 45 43 54   17.2 {.  SELECT
3ca0: 20 2a 20 46 52 4f 4d 20 74 31 37 20 57 48 45 52   * FROM t17 WHER
3cb0: 45 20 74 31 37 20 4d 41 54 43 48 20 27 6f 6e 65  E t17 MATCH 'one
3cc0: 27 0a 7d 20 7b 31 20 7b 64 61 74 61 62 61 73 65  '.} {1 {database
3cd0: 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d   disk image is m
3ce0: 61 6c 66 6f 72 6d 65 64 7d 7d 0a 0a 0a 0a 0a 66  alformed}}.....f
3cf0: 69 6e 69 73 68 5f 74 65 73 74 0a                 inish_test.