/ Hex Artifact Content
Login

Artifact cb932743da52a1c79a1ab8983e26c8121cf02263d6ff16e1f642e6f9b8348338:


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 64 6f   NULL) }.} {}.do
0fb0: 5f 65 72 72 6f 72 5f 74 65 73 74 20 66 74 73 33  _error_test fts3
0fc0: 63 6f 76 2d 32 2e 35 20 7b 0a 20 20 53 45 4c 45  cov-2.5 {.  SELE
0fd0: 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
0fe0: 52 45 20 74 31 20 4d 41 54 43 48 20 27 63 6c 6f  RE t1 MATCH 'clo
0ff0: 75 64 27 0a 7d 20 7b 53 51 4c 20 6c 6f 67 69 63  ud'.} {SQL logic
1000: 20 65 72 72 6f 72 7d 0a 0a 23 2d 2d 2d 2d 2d 2d   error}..#------
1010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1020: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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 0a 23 20 54 68 65 20 66 6f 6c 6c 6f  ----.# The follo
1060: 77 69 6e 67 20 74 65 73 74 73 20 61 72 65 20 74  wing tests are t
1070: 6f 20 74 65 73 74 20 74 68 65 20 65 66 66 65 63  o test the effec
1080: 74 73 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73  ts of OOM errors
1090: 20 77 68 69 6c 65 20 73 74 6f 72 69 6e 67 0a 23   while storing.#
10a0: 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 70 65   terms in the pe
10b0: 6e 64 69 6e 67 2d 68 61 73 68 20 74 61 62 6c 65  nding-hash table
10c0: 2e 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20  . Specifically, 
10d0: 77 68 69 6c 65 20 63 72 65 61 74 69 6e 67 20 64  while creating d
10e0: 6f 63 6c 69 73 74 0a 23 20 62 6c 6f 62 73 20 74  oclist.# blobs t
10f0: 6f 20 73 74 6f 72 65 20 69 6e 20 74 68 65 20 74  o store in the t
1100: 61 62 6c 65 2e 20 4d 6f 72 65 20 73 70 65 63 69  able. More speci
1110: 66 69 63 61 6c 6c 79 2c 20 74 6f 20 74 65 73 74  fically, to test
1120: 20 4f 4f 4d 20 65 72 72 6f 72 73 20 77 68 69 6c   OOM errors whil
1130: 65 0a 23 20 61 70 70 65 6e 64 69 6e 67 20 63 6f  e.# appending co
1140: 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20 74 6f 20  lumn numbers to 
1150: 64 6f 63 6c 69 73 74 73 2e 20 46 6f 72 20 65 78  doclists. For ex
1160: 61 6d 70 6c 65 2c 20 69 66 20 61 20 64 6f 63 6c  ample, if a docl
1170: 69 73 74 20 63 6f 6e 73 69 73 74 73 0a 23 20 6f  ist consists.# o
1180: 66 3a 0a 23 0a 23 20 20 20 3c 64 6f 63 69 64 3e  f:.#.#   <docid>
1190: 20 3c 63 6f 6c 75 6d 6e 20 30 20 6f 66 66 73 65   <column 0 offse
11a0: 74 2d 6c 69 73 74 3e 20 30 78 30 31 20 3c 63 6f  t-list> 0x01 <co
11b0: 6c 75 6d 6e 20 4e 3e 20 3c 63 6f 6c 75 6d 6e 20  lumn N> <column 
11c0: 4e 20 6f 66 66 73 65 74 2d 6c 69 73 74 3e 0a 23  N offset-list>.#
11d0: 0a 23 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .# The following
11e0: 20 74 65 73 74 73 20 63 68 65 63 6b 20 74 68 61   tests check tha
11f0: 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20  t malloc errors 
1200: 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c  encountered whil
1210: 65 20 61 70 70 65 6e 64 69 6e 67 0a 23 20 74 68  e appending.# th
1220: 65 20 22 30 78 30 31 20 3c 63 6f 6c 75 6d 6e 20  e "0x01 <column 
1230: 4e 3e 22 20 64 61 74 61 20 74 6f 20 74 68 65 20  N>" data to the 
1240: 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 72 6f 77  dynamically grow
1250: 61 62 6c 65 20 62 6c 6f 62 20 75 73 65 64 20 74  able blob used t
1260: 6f 20 0a 23 20 61 63 63 75 6d 75 6c 61 74 65 20  o .# accumulate 
1270: 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20 6d  the doclist in m
1280: 65 6d 6f 72 79 20 61 72 65 20 68 61 6e 64 6c 65  emory are handle
1290: 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 23 0a 64  d correctly..#.d
12a0: 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 33  o_test fts3cov-3
12b0: 2e 31 20 7b 0a 20 20 73 65 74 20 63 6f 6c 73 20  .1 {.  set cols 
12c0: 5b 6c 69 73 74 5d 0a 20 20 73 65 74 20 76 61 6c  [list].  set val
12d0: 73 20 5b 6c 69 73 74 5d 0a 20 20 66 6f 72 20 7b  s [list].  for {
12e0: 73 65 74 20 69 20 30 7d 20 7b 24 69 20 3c 20 31  set i 0} {$i < 1
12f0: 32 30 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 0a 20  20} {incr i} {. 
1300: 20 20 20 6c 61 70 70 65 6e 64 20 63 6f 6c 73 20     lappend cols 
1310: 22 63 6f 6c 24 69 22 0a 20 20 20 20 6c 61 70 70  "col$i".    lapp
1320: 65 6e 64 20 76 61 6c 73 20 22 27 77 6f 72 64 27  end vals "'word'
1330: 22 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20  ".  }.  execsql 
1340: 22 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20  "CREATE VIRTUAL 
1350: 54 41 42 4c 45 20 74 32 20 55 53 49 4e 47 20 66  TABLE t2 USING f
1360: 74 73 33 28 5b 6a 6f 69 6e 20 24 63 6f 6c 73 20  ts3([join $cols 
1370: 2c 5d 29 22 0a 7d 20 7b 7d 0a 73 65 74 20 44 4f  ,])".} {}.set DO
1380: 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 31 20 0a  _MALLOC_TEST 1 .
1390: 64 6f 5f 77 72 69 74 65 5f 74 65 73 74 20 66 74  do_write_test ft
13a0: 73 33 63 6f 76 2d 33 2e 32 20 74 32 5f 63 6f 6e  s3cov-3.2 t2_con
13b0: 74 65 6e 74 20 22 0a 20 20 49 4e 53 45 52 54 20  tent ".  INSERT 
13c0: 49 4e 54 4f 20 74 32 28 64 6f 63 69 64 2c 20 5b  INTO t2(docid, [
13d0: 6a 6f 69 6e 20 24 63 6f 6c 73 20 2c 5d 29 20 56  join $cols ,]) V
13e0: 41 4c 55 45 53 28 31 2c 20 5b 6a 6f 69 6e 20 24  ALUES(1, [join $
13f0: 76 61 6c 73 20 2c 5d 29 0a 22 0a 64 6f 5f 77 72  vals ,]).".do_wr
1400: 69 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76  ite_test fts3cov
1410: 2d 33 2e 33 20 74 32 5f 63 6f 6e 74 65 6e 74 20  -3.3 t2_content 
1420: 22 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  ".  INSERT INTO 
1430: 74 32 28 64 6f 63 69 64 2c 20 5b 6a 6f 69 6e 20  t2(docid, [join 
1440: 24 63 6f 6c 73 20 2c 5d 29 20 56 41 4c 55 45 53  $cols ,]) VALUES
1450: 28 32 30 30 2c 20 5b 6a 6f 69 6e 20 24 76 61 6c  (200, [join $val
1460: 73 20 2c 5d 29 0a 22 0a 64 6f 5f 77 72 69 74 65  s ,]).".do_write
1470: 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 33 2e  _test fts3cov-3.
1480: 34 20 74 32 5f 63 6f 6e 74 65 6e 74 20 22 0a 20  4 t2_content ". 
1490: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 32 28   INSERT INTO t2(
14a0: 64 6f 63 69 64 2c 20 5b 6a 6f 69 6e 20 24 63 6f  docid, [join $co
14b0: 6c 73 20 2c 5d 29 20 56 41 4c 55 45 53 28 36 30  ls ,]) VALUES(60
14c0: 30 30 30 2c 20 5b 6a 6f 69 6e 20 24 76 61 6c 73  000, [join $vals
14d0: 20 2c 5d 29 0a 22 0a 0a 23 2d 2d 2d 2d 2d 2d 2d   ,])."..#-------
14e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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 0a 23 20 49 66 20 74 6f 6f 20 6d 75 63 68  --.# If too much
1530: 20 64 61 74 61 20 61 63 63 75 6d 75 6c 61 74 65   data accumulate
1540: 73 20 69 6e 20 74 68 65 20 70 65 6e 64 69 6e 67  s in the pending
1550: 2d 74 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c  -terms hash tabl
1560: 65 2c 20 69 74 20 69 73 0a 23 20 66 6c 75 73 68  e, it is.# flush
1570: 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
1580: 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  se automatically
1590: 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 72  , even if the tr
15a0: 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f  ansaction has no
15b0: 74 0a 23 20 66 69 6e 69 73 68 65 64 2e 20 54 68  t.# finished. Th
15c0: 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74  e following test
15d0: 73 20 63 68 65 63 6b 20 74 68 65 20 65 66 66 65  s check the effe
15e0: 63 74 73 20 6f 66 20 65 6e 63 6f 75 6e 74 65 72  cts of encounter
15f0: 69 6e 67 20 61 6e 20 4f 4f 4d 20 0a 23 20 77 68  ing an OOM .# wh
1600: 69 6c 65 20 64 6f 69 6e 67 20 74 68 69 73 2e 0a  ile doing this..
1610: 23 0a 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f  #.do_test fts3co
1620: 76 2d 34 2e 31 20 7b 0a 20 20 65 78 65 63 73 71  v-4.1 {.  execsq
1630: 6c 20 7b 0a 20 20 20 20 43 52 45 41 54 45 20 56  l {.    CREATE V
1640: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 33 20  IRTUAL TABLE t3 
1650: 55 53 49 4e 47 20 66 74 73 33 28 78 29 3b 0a 20  USING fts3(x);. 
1660: 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74     INSERT INTO t
1670: 33 28 74 33 29 20 56 41 4c 55 45 53 28 27 6e 6f  3(t3) VALUES('no
1680: 64 65 73 69 7a 65 3d 32 34 27 29 3b 0a 20 20 20  desize=24');.   
1690: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 33 28   INSERT INTO t3(
16a0: 74 33 29 20 56 41 4c 55 45 53 28 27 6d 61 78 70  t3) VALUES('maxp
16b0: 65 6e 64 69 6e 67 3d 31 30 30 27 29 3b 0a 20 20  ending=100');.  
16c0: 7d 0a 7d 20 7b 7d 0a 73 65 74 20 44 4f 5f 4d 41  }.} {}.set DO_MA
16d0: 4c 4c 4f 43 5f 54 45 53 54 20 31 20 0a 64 6f 5f  LLOC_TEST 1 .do_
16e0: 77 72 69 74 65 5f 74 65 73 74 20 66 74 73 33 63  write_test fts3c
16f0: 6f 76 2d 34 2e 32 20 74 33 5f 63 6f 6e 74 65 6e  ov-4.2 t3_conten
1700: 74 20 7b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54  t {.  INSERT INT
1710: 4f 20 74 33 28 64 6f 63 69 64 2c 20 78 29 0a 20  O t3(docid, x). 
1720: 20 20 20 53 45 4c 45 43 54 20 31 2c 20 27 54 68     SELECT 1, 'Th
1730: 65 6e 20 43 68 72 69 73 74 61 62 65 6c 20 73 74  en Christabel st
1740: 72 65 74 63 68 65 64 20 66 6f 72 74 68 20 68 65  retched forth he
1750: 72 20 68 61 6e 64 2c 27 20 55 4e 49 4f 4e 20 41  r hand,' UNION A
1760: 4c 4c 0a 20 20 20 20 53 45 4c 45 43 54 20 33 2c  LL.    SELECT 3,
1770: 20 27 41 6e 64 20 63 6f 6d 66 6f 72 74 65 64 20   'And comforted 
1780: 66 61 69 72 20 47 65 72 61 6c 64 69 6e 65 3a 27  fair Geraldine:'
1790: 20 20 20 20 20 20 20 20 20 20 20 20 20 55 4e 49               UNI
17a0: 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c 45 43  ON ALL.    SELEC
17b0: 54 20 34 2c 20 27 27 27 4f 20 77 65 6c 6c 2c 20  T 4, '''O well, 
17c0: 62 72 69 67 68 74 20 64 61 6d 65 2c 20 6d 61 79  bright dame, may
17d0: 20 79 6f 75 20 63 6f 6d 6d 61 6e 64 27 20 20 20   you command'   
17e0: 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53   UNION ALL.    S
17f0: 45 4c 45 43 54 20 35 2c 20 27 54 68 65 20 73 65  ELECT 5, 'The se
1800: 72 76 69 63 65 20 6f 66 20 53 69 72 20 4c 65 6f  rvice of Sir Leo
1810: 6c 69 6e 65 3b 27 20 20 20 20 20 20 20 20 20 20  line;'          
1820: 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20       UNION ALL. 
1830: 20 20 20 53 45 4c 45 43 54 20 32 2c 20 27 41 6e     SELECT 2, 'An
1840: 64 20 67 6c 61 64 6c 79 20 6f 75 72 20 73 74 6f  d gladly our sto
1850: 75 74 20 63 68 69 76 61 6c 72 79 27 20 20 20 20  ut chivalry'    
1860: 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41           UNION A
1870: 4c 4c 0a 20 20 20 20 53 45 4c 45 43 54 20 37 2c  LL.    SELECT 7,
1880: 20 27 57 69 6c 6c 20 68 65 20 73 65 6e 64 20 66   'Will he send f
1890: 6f 72 74 68 2c 20 61 6e 64 20 66 72 69 65 6e 64  orth, and friend
18a0: 73 20 77 69 74 68 61 6c 2c 27 20 20 20 55 4e 49  s withal,'   UNI
18b0: 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c 45 43  ON ALL.    SELEC
18c0: 54 20 38 2c 20 27 54 6f 20 67 75 69 64 65 20 61  T 8, 'To guide a
18d0: 6e 64 20 67 75 61 72 64 20 79 6f 75 20 73 61 66  nd guard you saf
18e0: 65 20 61 6e 64 20 66 72 65 65 27 20 20 20 20 20  e and free'     
18f0: 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53   UNION ALL.    S
1900: 45 4c 45 43 54 20 36 2c 20 27 48 6f 6d 65 20 74  ELECT 6, 'Home t
1910: 6f 20 79 6f 75 72 20 6e 6f 62 6c 65 20 66 61 74  o your noble fat
1920: 68 65 72 27 27 73 20 68 61 6c 6c 2e 27 27 27 0a  her''s hall.'''.
1930: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
1940: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1950: 2d 2d 2d 2d 2d 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 0a 23 20  -------------.# 
1980: 57 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 74 68  When building th
1990: 65 20 69 6e 74 65 72 6e 61 6c 20 74 72 65 65 20  e internal tree 
19a0: 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65 61  structure for ea
19b0: 63 68 20 73 65 67 6d 65 6e 74 20 62 2d 74 72 65  ch segment b-tre
19c0: 65 2c 20 46 54 53 33 0a 23 20 61 73 73 75 6d 65  e, FTS3.# assume
19d0: 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65  s that the conte
19e0: 6e 74 20 6f 66 20 65 61 63 68 20 69 6e 74 65 72  nt of each inter
19f0: 6e 61 6c 20 6e 6f 64 65 20 77 69 6c 6c 20 62 65  nal node will be
1a00: 20 6c 65 73 73 20 74 68 61 6e 0a 23 20 24 6e 6f   less than.# $no
1a10: 64 65 73 69 7a 65 20 62 79 74 65 73 2c 20 77 68  desize bytes, wh
1a20: 65 72 65 20 24 6e 6f 64 65 73 69 7a 65 20 69 73  ere $nodesize is
1a30: 20 74 68 65 20 61 64 76 69 73 6f 72 79 20 6e 6f   the advisory no
1a40: 64 65 20 73 69 7a 65 2e 20 49 66 20 74 68 69 73  de size. If this
1a50: 20 74 75 72 6e 73 0a 23 20 6f 75 74 20 74 6f 20   turns.# out to 
1a60: 62 65 20 75 6e 74 72 75 65 2c 20 74 68 65 6e 20  be untrue, then 
1a70: 61 6e 20 65 78 74 72 61 20 62 75 66 66 65 72 20  an extra buffer 
1a80: 6d 75 73 74 20 62 65 20 6d 61 6c 6c 6f 63 27 64  must be malloc'd
1a90: 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 0a   for each term..
1aa0: 23 20 54 68 69 73 20 74 65 73 74 20 63 61 73 65  # This test case
1ab0: 20 74 65 73 74 73 20 74 68 65 73 65 20 70 61 74   tests these pat
1ac0: 68 73 20 61 6e 64 20 74 68 65 20 65 66 66 65 63  hs and the effec
1ad0: 74 73 20 6f 66 20 73 61 69 64 20 6d 61 6c 6c 6f  ts of said mallo
1ae0: 63 73 20 66 61 69 6c 69 6e 67 0a 23 20 62 79 20  cs failing.# by 
1af0: 69 6e 73 65 72 74 69 6e 67 20 69 6e 73 65 72 74  inserting insert
1b00: 20 61 20 64 6f 63 75 6d 65 6e 74 20 77 69 74 68   a document with
1b10: 20 73 6f 6d 65 20 66 61 69 72 6c 79 20 6c 61 72   some fairly lar
1b20: 67 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 61 0a  ge terms into a.
1b30: 23 20 66 75 6c 6c 2d 74 65 78 74 20 74 61 62 6c  # full-text tabl
1b40: 65 20 77 69 74 68 20 61 20 76 65 72 79 20 73 6d  e with a very sm
1b50: 61 6c 6c 20 6e 6f 64 65 2d 73 69 7a 65 2e 20 0a  all node-size. .
1b60: 23 0a 23 20 54 65 73 74 20 74 68 69 73 20 68 61  #.# Test this ha
1b70: 6e 64 6c 69 6e 67 20 6f 66 20 6c 61 72 67 65 20  ndling of large 
1b80: 74 65 72 6d 73 20 69 6e 20 74 68 72 65 65 20 63  terms in three c
1b90: 6f 6e 74 65 78 74 73 3a 0a 23 0a 23 20 20 20 31  ontexts:.#.#   1
1ba0: 2e 20 57 68 65 6e 20 66 6c 75 73 68 69 6e 67 20  . When flushing 
1bb0: 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d  the pending-term
1bc0: 73 20 74 61 62 6c 65 2e 0a 23 20 20 20 32 2e 20  s table..#   2. 
1bd0: 57 68 65 6e 20 6f 70 74 69 6d 69 7a 69 6e 67 20  When optimizing 
1be0: 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  the data structu
1bf0: 72 65 73 20 75 73 69 6e 67 20 74 68 65 20 49 4e  res using the IN
1c00: 53 45 52 54 20 73 79 6e 74 61 78 2e 20 0a 23 20  SERT syntax. .# 
1c10: 20 20 32 2e 20 57 68 65 6e 20 6f 70 74 69 6d 69    2. When optimi
1c20: 7a 69 6e 67 20 74 68 65 20 64 61 74 61 20 73 74  zing the data st
1c30: 72 75 63 74 75 72 65 73 20 75 73 69 6e 67 20 74  ructures using t
1c40: 68 65 20 64 65 70 72 65 63 61 74 65 64 20 53 45  he deprecated SE
1c50: 4c 45 43 54 20 73 79 6e 74 61 78 2e 20 0a 23 0a  LECT syntax. .#.
1c60: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
1c70: 35 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  5.1 {.  execsql 
1c80: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 56 49 52  {.    CREATE VIR
1c90: 54 55 41 4c 20 54 41 42 4c 45 20 74 34 20 55 53  TUAL TABLE t4 US
1ca0: 49 4e 47 20 66 74 73 33 28 78 29 3b 0a 20 20 20  ING fts3(x);.   
1cb0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 34 28   INSERT INTO t4(
1cc0: 74 34 29 20 56 41 4c 55 45 53 28 27 6e 6f 64 65  t4) VALUES('node
1cd0: 73 69 7a 65 3d 32 34 27 29 3b 0a 20 20 7d 0a 7d  size=24');.  }.}
1ce0: 20 7b 7d 0a 73 65 74 20 44 4f 5f 4d 41 4c 4c 4f   {}.set DO_MALLO
1cf0: 43 5f 54 45 53 54 20 31 0a 0a 23 20 54 65 73 74  C_TEST 1..# Test
1d00: 20 77 68 65 6e 20 66 6c 75 73 68 69 6e 67 20 70   when flushing p
1d10: 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 74 61 62  ending-terms tab
1d20: 6c 65 2e 0a 64 6f 5f 77 72 69 74 65 5f 74 65 73  le..do_write_tes
1d30: 74 20 66 74 73 33 63 6f 76 2d 35 2e 32 20 74 34  t fts3cov-5.2 t4
1d40: 5f 63 6f 6e 74 65 6e 74 20 7b 0a 20 20 49 4e 53  _content {.  INS
1d50: 45 52 54 20 49 4e 54 4f 20 74 34 0a 20 20 20 20  ERT INTO t4.    
1d60: 53 45 4c 45 43 54 20 27 49 74 69 73 61 6e 61 6e  SELECT 'Itisanan
1d70: 63 69 65 6e 74 4d 61 72 69 6e 65 72 41 6e 64 68  cientMarinerAndh
1d80: 65 73 74 6f 70 70 65 74 68 6f 6e 65 6f 66 74 68  estoppethoneofth
1d90: 72 65 65 41 41 27 20 55 4e 49 4f 4e 20 41 4c 4c  reeAA' UNION ALL
1da0: 0a 20 20 20 20 53 45 4c 45 43 54 20 27 49 74 69  .    SELECT 'Iti
1db0: 73 61 6e 61 6e 63 69 65 6e 74 4d 61 72 69 6e 65  sanancientMarine
1dc0: 72 41 6e 64 68 65 73 74 6f 70 70 65 74 68 6f 6e  rAndhestoppethon
1dd0: 65 6f 66 74 68 72 65 65 42 42 27 20 55 4e 49 4f  eofthreeBB' UNIO
1de0: 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c 45 43 54  N ALL.    SELECT
1df0: 20 27 49 74 69 73 61 6e 61 6e 63 69 65 6e 74 4d   'ItisanancientM
1e00: 61 72 69 6e 65 72 41 6e 64 68 65 73 74 6f 70 70  arinerAndhestopp
1e10: 65 74 68 6f 6e 65 6f 66 74 68 72 65 65 43 43 27  ethoneofthreeCC'
1e20: 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53   UNION ALL.    S
1e30: 45 4c 45 43 54 20 27 42 79 74 68 79 6c 6f 6e 67  ELECT 'Bythylong
1e40: 67 72 65 79 62 65 61 72 64 61 6e 64 67 6c 69 74  greybeardandglit
1e50: 74 65 72 69 6e 67 65 79 65 4e 6f 77 77 68 65 72  teringeyeNowwher
1e60: 65 66 6f 72 65 73 74 6f 70 70 73 74 41 41 27 20  eforestoppstAA' 
1e70: 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45  UNION ALL.    SE
1e80: 4c 45 43 54 20 27 42 79 74 68 79 6c 6f 6e 67 67  LECT 'Bythylongg
1e90: 72 65 79 62 65 61 72 64 61 6e 64 67 6c 69 74 74  reybeardandglitt
1ea0: 65 72 69 6e 67 65 79 65 4e 6f 77 77 68 65 72 65  eringeyeNowwhere
1eb0: 66 6f 72 65 73 74 6f 70 70 73 74 42 42 27 20 55  forestoppstBB' U
1ec0: 4e 49 4f 4e 20 41 4c 4c 0a 20 20 20 20 53 45 4c  NION ALL.    SEL
1ed0: 45 43 54 20 27 42 79 74 68 79 6c 6f 6e 67 67 72  ECT 'Bythylonggr
1ee0: 65 79 62 65 61 72 64 61 6e 64 67 6c 69 74 74 65  eybeardandglitte
1ef0: 72 69 6e 67 65 79 65 4e 6f 77 77 68 65 72 65 66  ringeyeNowwheref
1f00: 6f 72 65 73 74 6f 70 70 73 74 43 43 27 0a 7d 0a  orestoppstCC'.}.
1f10: 0a 23 20 54 65 73 74 20 77 68 65 6e 20 6f 70 74  .# Test when opt
1f20: 69 6d 69 7a 69 6e 67 20 76 69 61 20 49 4e 53 45  imizing via INSE
1f30: 52 54 2e 0a 64 6f 5f 74 65 73 74 20 66 74 73 33  RT..do_test fts3
1f40: 63 6f 76 2d 35 2e 33 20 7b 20 65 78 65 63 73 71  cov-5.3 { execsq
1f50: 6c 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  l { INSERT INTO 
1f60: 74 34 20 56 41 4c 55 45 53 28 27 65 78 74 72 61  t4 VALUES('extra
1f70: 21 27 29 20 7d 20 7d 20 7b 7d 0a 64 6f 5f 77 72  !') } } {}.do_wr
1f80: 69 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76  ite_test fts3cov
1f90: 2d 35 2e 32 20 74 34 5f 73 65 67 6d 65 6e 74 73  -5.2 t4_segments
1fa0: 20 7b 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74   { INSERT INTO t
1fb0: 34 28 74 34 29 20 56 41 4c 55 45 53 28 27 6f 70  4(t4) VALUES('op
1fc0: 74 69 6d 69 7a 65 27 29 20 7d 0a 0a 23 20 54 65  timize') }..# Te
1fd0: 73 74 20 77 68 65 6e 20 6f 70 74 69 6d 69 7a 69  st when optimizi
1fe0: 6e 67 20 76 69 61 20 53 45 4c 45 43 54 2e 0a 64  ng via SELECT..d
1ff0: 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 35  o_test fts3cov-5
2000: 2e 35 20 7b 20 65 78 65 63 73 71 6c 20 7b 20 49  .5 { execsql { I
2010: 4e 53 45 52 54 20 49 4e 54 4f 20 74 34 20 56 41  NSERT INTO t4 VA
2020: 4c 55 45 53 28 27 6d 6f 72 65 20 65 78 74 72 61  LUES('more extra
2030: 21 27 29 20 7d 20 7d 20 7b 7d 0a 64 6f 5f 77 72  !') } } {}.do_wr
2040: 69 74 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76  ite_test fts3cov
2050: 2d 35 2e 36 20 74 34 5f 73 65 67 6d 65 6e 74 73  -5.6 t4_segments
2060: 20 7b 0a 20 20 53 45 4c 45 43 54 20 2a 20 46 52   {.  SELECT * FR
2070: 4f 4d 20 28 53 45 4c 45 43 54 20 6f 70 74 69 6d  OM (SELECT optim
2080: 69 7a 65 28 74 34 29 20 46 52 4f 4d 20 74 34 20  ize(t4) FROM t4 
2090: 4c 49 4d 49 54 20 31 29 0a 20 20 45 58 43 45 50  LIMIT 1).  EXCEP
20a0: 54 20 53 45 4c 45 43 54 20 27 49 6e 64 65 78 20  T SELECT 'Index 
20b0: 6f 70 74 69 6d 69 7a 65 64 27 0a 7d 0a 0a 23 2d  optimized'.}..#-
20c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
20d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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 0a 23 20 57 68 65 6e 20  --------.# When 
2110: 6d 65 72 67 69 6e 67 20 61 6c 6c 20 73 65 67 6d  merging all segm
2120: 65 6e 74 73 20 61 74 20 61 20 67 69 76 65 6e 20  ents at a given 
2130: 6c 65 76 65 6c 20 74 6f 20 63 72 65 61 74 65 20  level to create 
2140: 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74  a single segment
2150: 0a 23 20 61 74 20 6c 65 76 65 6c 2b 31 2c 20 46  .# at level+1, F
2160: 54 53 33 20 72 75 6e 73 20 61 20 71 75 65 72 79  TS3 runs a query
2170: 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 23 0a   of the form:.#.
2180: 23 20 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74  #   SELECT count
2190: 28 2a 29 20 46 52 4f 4d 20 25 5f 73 65 67 64 69  (*) FROM %_segdi
21a0: 72 20 57 48 45 52 45 20 6c 65 76 65 6c 20 3d 20  r WHERE level = 
21b0: 3f 0a 23 0a 23 20 54 68 65 20 71 75 65 72 79 20  ?.#.# The query 
21c0: 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 68 65 20  is compiled the 
21d0: 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20  first time this 
21e0: 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
21f0: 75 69 72 65 64 20 61 6e 64 0a 23 20 72 65 75 73  uired and.# reus
2200: 65 64 20 74 68 65 72 65 61 66 74 65 72 2e 20 54  ed thereafter. T
2210: 68 69 73 20 74 65 73 74 20 61 69 6d 73 20 74 6f  his test aims to
2220: 20 74 65 73 74 20 74 68 65 20 65 66 66 65 63 74   test the effect
2230: 73 20 6f 66 20 61 6e 20 4f 4f 4d 20 77 68 69 6c  s of an OOM whil
2240: 65 0a 23 20 70 72 65 70 61 72 69 6e 67 20 61 6e  e.# preparing an
2250: 64 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  d executing this
2260: 20 71 75 65 72 79 20 66 6f 72 20 74 68 65 20 66   query for the f
2270: 69 72 73 74 20 74 69 6d 65 2e 0a 23 0a 23 20 54  irst time..#.# T
2280: 68 65 6e 2c 20 6b 65 65 70 20 69 6e 73 65 72 74  hen, keep insert
2290: 69 6e 67 20 72 6f 77 73 20 69 6e 74 6f 20 74 68  ing rows into th
22a0: 65 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20  e table so that 
22b0: 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 61  the effects of a
22c0: 6e 20 4f 4f 4d 0a 23 20 77 68 69 6c 65 20 72 65  n OOM.# while re
22d0: 2d 65 78 65 63 75 74 69 6e 67 20 74 68 65 20 73  -executing the s
22e0: 61 6d 65 20 71 75 65 72 79 20 63 61 6e 20 61 6c  ame query can al
22f0: 73 6f 20 62 65 20 74 65 73 74 65 64 2e 0a 23 0a  so be tested..#.
2300: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
2310: 36 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  6.1 {.  execsql 
2320: 7b 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  { CREATE VIRTUAL
2330: 20 54 41 42 4c 45 20 74 35 20 55 53 49 4e 47 20   TABLE t5 USING 
2340: 66 74 73 33 28 78 29 20 7d 0a 20 20 66 6f 72 20  fts3(x) }.  for 
2350: 7b 73 65 74 20 69 20 30 7d 20 7b 24 69 3c 31 36  {set i 0} {$i<16
2360: 7d 20 7b 69 6e 63 72 20 69 7d 20 7b 20 65 78 65  } {incr i} { exe
2370: 63 73 71 6c 20 22 49 4e 53 45 52 54 20 49 4e 54  csql "INSERT INT
2380: 4f 20 74 35 20 56 41 4c 55 45 53 28 27 74 65 72  O t5 VALUES('ter
2390: 6d 24 69 27 29 22 20 7d 0a 20 20 65 78 65 63 73  m$i')" }.  execs
23a0: 71 6c 20 7b 20 53 45 4c 45 43 54 20 63 6f 75 6e  ql { SELECT coun
23b0: 74 28 2a 29 20 46 52 4f 4d 20 74 35 5f 73 65 67  t(*) FROM t5_seg
23c0: 64 69 72 20 7d 0a 7d 20 7b 31 36 7d 0a 0a 23 20  dir }.} {16}..# 
23d0: 46 69 72 73 74 20 74 69 6d 65 2e 0a 64 62 20 63  First time..db c
23e0: 6c 6f 73 65 0a 73 71 6c 69 74 65 33 20 64 62 20  lose.sqlite3 db 
23f0: 74 65 73 74 2e 64 62 0a 64 6f 5f 77 72 69 74 65  test.db.do_write
2400: 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 36 2e  _test fts3cov-6.
2410: 32 20 74 35 5f 63 6f 6e 74 65 6e 74 20 7b 0a 20  2 t5_content {. 
2420: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 35 20   INSERT INTO t5 
2430: 56 41 4c 55 45 53 28 27 73 65 67 6d 65 6e 74 20  VALUES('segment 
2440: 6e 75 6d 62 65 72 20 31 36 21 27 29 3b 0a 7d 0a  number 16!');.}.
2450: 0a 23 20 53 65 63 6f 6e 64 20 74 69 6d 65 2e 0a  .# Second time..
2460: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
2470: 36 2e 33 20 7b 0a 20 20 66 6f 72 20 7b 73 65 74  6.3 {.  for {set
2480: 20 69 20 31 7d 20 7b 24 69 3c 31 36 7d 20 7b 69   i 1} {$i<16} {i
2490: 6e 63 72 20 69 7d 20 7b 20 65 78 65 63 73 71 6c  ncr i} { execsql
24a0: 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 35   "INSERT INTO t5
24b0: 20 56 41 4c 55 45 53 28 27 74 65 72 6d 24 69 27   VALUES('term$i'
24c0: 29 22 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  )" }.  execsql {
24d0: 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29   SELECT count(*)
24e0: 20 46 52 4f 4d 20 74 35 5f 73 65 67 64 69 72 20   FROM t5_segdir 
24f0: 7d 0a 7d 20 7b 31 37 7d 0a 64 6f 5f 77 72 69 74  }.} {17}.do_writ
2500: 65 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 36  e_test fts3cov-6
2510: 2e 34 20 74 35 5f 63 6f 6e 74 65 6e 74 20 7b 0a  .4 t5_content {.
2520: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 35    INSERT INTO t5
2530: 20 56 41 4c 55 45 53 28 27 73 65 67 6d 65 6e 74   VALUES('segment
2540: 20 6e 75 6d 62 65 72 20 31 36 21 27 29 3b 0a 7d   number 16!');.}
2550: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
2560: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2570: 2d 2d 2d 2d 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 0a 23 20 55  ------------.# U
25a0: 70 64 61 74 65 20 74 68 65 20 64 6f 63 69 64 20  pdate the docid 
25b0: 6f 66 20 61 20 72 6f 77 2e 20 54 65 73 74 20 74  of a row. Test t
25c0: 68 69 73 20 69 6e 20 74 77 6f 20 73 63 65 6e 61  his in two scena
25d0: 72 69 6f 73 3a 0a 23 0a 23 20 20 20 31 2e 20 57  rios:.#.#   1. W
25e0: 68 65 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e  hen the row bein
25f0: 67 20 75 70 64 61 74 65 64 20 69 73 20 74 68 65  g updated is the
2600: 20 6f 6e 6c 79 20 72 6f 77 20 69 6e 20 74 68 65   only row in the
2610: 20 74 61 62 6c 65 2e 0a 23 20 20 20 32 2e 20 57   table..#   2. W
2620: 68 65 6e 20 69 74 20 69 73 20 6e 6f 74 2e 0a 23  hen it is not..#
2630: 0a 23 20 54 68 65 20 74 77 6f 20 63 61 73 65 73  .# The two cases
2640: 20 61 62 6f 76 65 20 74 61 6b 65 20 64 69 66 66   above take diff
2650: 65 72 65 6e 74 20 70 61 74 68 73 20 62 65 63 61  erent paths beca
2660: 75 73 65 20 69 6e 20 63 61 73 65 20 31 20 61 6c  use in case 1 al
2670: 6c 20 64 61 74 61 20 0a 23 20 73 74 72 75 63 74  l data .# struct
2680: 75 72 65 73 20 63 61 6e 20 73 69 6d 70 6c 79 20  ures can simply 
2690: 62 65 20 65 6d 70 74 69 65 64 20 62 65 66 6f 72  be emptied befor
26a0: 65 20 69 6e 73 65 72 74 69 6e 67 20 74 68 65 20  e inserting the 
26b0: 6e 65 77 20 72 6f 77 20 72 65 63 6f 72 64 2e 0a  new row record..
26c0: 23 20 49 6e 20 63 61 73 65 20 32 2c 20 74 68 65  # In case 2, the
26d0: 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
26e0: 20 61 63 74 75 61 6c 6c 79 20 68 61 76 65 20 74   actually have t
26f0: 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 23 0a  o be updated..#.
2700: 64 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d  do_test fts3cov-
2710: 37 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  7.1 {.  execsql 
2720: 7b 0a 20 20 20 20 43 52 45 41 54 45 20 56 49 52  {.    CREATE VIR
2730: 54 55 41 4c 20 54 41 42 4c 45 20 74 37 20 55 53  TUAL TABLE t7 US
2740: 49 4e 47 20 66 74 73 33 28 61 2c 20 62 2c 20 63  ING fts3(a, b, c
2750: 29 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e  );.    INSERT IN
2760: 54 4f 20 74 37 20 56 41 4c 55 45 53 28 27 41 27  TO t7 VALUES('A'
2770: 2c 20 27 42 27 2c 20 27 43 27 29 3b 0a 20 20 20  , 'B', 'C');.   
2780: 20 55 50 44 41 54 45 20 74 37 20 53 45 54 20 64   UPDATE t7 SET d
2790: 6f 63 69 64 20 3d 20 35 3b 0a 20 20 20 20 53 45  ocid = 5;.    SE
27a0: 4c 45 43 54 20 64 6f 63 69 64 2c 20 2a 20 46 52  LECT docid, * FR
27b0: 4f 4d 20 74 37 3b 0a 20 20 7d 0a 7d 20 7b 35 20  OM t7;.  }.} {5 
27c0: 41 20 42 20 43 7d 0a 64 6f 5f 74 65 73 74 20 66  A B C}.do_test f
27d0: 74 73 33 63 6f 76 2d 37 2e 32 20 7b 0a 20 20 65  ts3cov-7.2 {.  e
27e0: 78 65 63 73 71 6c 20 7b 0a 20 20 20 20 49 4e 53  xecsql {.    INS
27f0: 45 52 54 20 49 4e 54 4f 20 74 37 20 56 41 4c 55  ERT INTO t7 VALU
2800: 45 53 28 27 44 27 2c 20 27 45 27 2c 20 27 46 27  ES('D', 'E', 'F'
2810: 29 3b 0a 20 20 20 20 55 50 44 41 54 45 20 74 37  );.    UPDATE t7
2820: 20 53 45 54 20 64 6f 63 69 64 20 3d 20 31 20 57   SET docid = 1 W
2830: 48 45 52 45 20 64 6f 63 69 64 20 3d 20 36 3b 0a  HERE docid = 6;.
2840: 20 20 20 20 53 45 4c 45 43 54 20 64 6f 63 69 64      SELECT docid
2850: 2c 20 2a 20 46 52 4f 4d 20 74 37 3b 0a 20 20 7d  , * FROM t7;.  }
2860: 0a 7d 20 7b 31 20 44 20 45 20 46 20 35 20 41 20  .} {1 D E F 5 A 
2870: 42 20 43 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d  B C}..#---------
2880: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2890: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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: 0a 23 20 49 66 20 61 20 73 65 74 20 6f 66 20 64  .# If a set of d
28d0: 6f 63 75 6d 65 6e 74 73 20 61 72 65 20 6d 6f 64  ocuments are mod
28e0: 69 66 69 65 64 20 77 69 74 68 69 6e 20 61 20 74  ified within a t
28f0: 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20  ransaction, the 
2900: 0a 23 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73  .# pending-terms
2910: 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 66   table must be f
2920: 6c 75 73 68 65 64 20 65 61 63 68 20 74 69 6d 65  lushed each time
2930: 20 61 20 64 6f 63 75 6d 65 6e 74 20 77 69 74 68   a document with
2940: 20 61 20 64 6f 63 69 64 0a 23 20 6c 65 73 73 20   a docid.# less 
2950: 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
2960: 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 64 6f   the previous do
2970: 63 69 64 20 69 73 20 6d 6f 64 69 66 69 65 64 2e  cid is modified.
2980: 20 0a 23 0a 23 20 54 68 69 73 20 74 65 73 74 20   .#.# This test 
2990: 63 68 65 63 6b 73 20 74 68 65 20 65 66 66 65 63  checks the effec
29a0: 74 73 20 6f 66 20 61 6e 20 4f 4f 4d 20 65 72 72  ts of an OOM err
29b0: 6f 72 20 6f 63 63 75 72 69 6e 67 20 77 68 65 6e  or occuring when
29c0: 20 74 68 65 20 0a 23 20 70 65 6e 64 69 6e 67 2d   the .# pending-
29d0: 74 65 72 6d 73 20 74 61 62 6c 65 20 69 73 20 66  terms table is f
29e0: 6c 75 73 68 65 64 20 66 6f 72 20 74 68 69 73 20  lushed for this 
29f0: 72 65 61 73 6f 6e 20 61 73 20 70 61 72 74 20 6f  reason as part o
2a00: 66 20 61 20 44 45 4c 45 54 45 20 0a 23 20 73 74  f a DELETE .# st
2a10: 61 74 65 6d 65 6e 74 2e 0a 23 0a 64 6f 5f 6d 61  atement..#.do_ma
2a20: 6c 6c 6f 63 5f 74 65 73 74 20 66 74 73 33 63 6f  lloc_test fts3co
2a30: 76 2d 38 20 2d 73 71 6c 70 72 65 70 20 7b 0a 20  v-8 -sqlprep {. 
2a40: 20 42 45 47 49 4e 3b 0a 20 20 20 20 43 52 45 41   BEGIN;.    CREA
2a50: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2a60: 20 74 38 20 55 53 49 4e 47 20 66 74 73 33 3b 0a   t8 USING fts3;.
2a70: 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2a80: 74 38 20 56 41 4c 55 45 53 28 27 74 68 65 20 6f  t8 VALUES('the o
2a90: 75 74 70 75 74 20 6f 66 20 65 61 63 68 20 62 61  utput of each ba
2aa0: 74 63 68 20 72 75 6e 27 29 3b 0a 20 20 20 20 49  tch run');.    I
2ab0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 38 20 56 41  NSERT INTO t8 VA
2ac0: 4c 55 45 53 28 27 28 70 6f 73 73 69 62 6c 79 20  LUES('(possibly 
2ad0: 61 20 64 61 79 27 27 73 20 77 6f 72 6b 29 27 29  a day''s work)')
2ae0: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
2af0: 4f 20 74 38 20 56 41 4c 55 45 53 28 27 77 61 73  O t8 VALUES('was
2b00: 20 77 72 69 74 74 65 6e 20 74 6f 20 74 77 6f 20   written to two 
2b10: 73 65 70 61 72 61 74 65 20 64 69 73 6b 73 27 29  separate disks')
2b20: 3b 0a 20 20 43 4f 4d 4d 49 54 3b 0a 7d 20 2d 73  ;.  COMMIT;.} -s
2b30: 71 6c 62 6f 64 79 20 7b 0a 20 20 42 45 47 49 4e  qlbody {.  BEGIN
2b40: 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
2b50: 4d 20 74 38 20 57 48 45 52 45 20 72 6f 77 69 64  M t8 WHERE rowid
2b60: 20 3d 20 33 3b 0a 20 20 20 20 44 45 4c 45 54 45   = 3;.    DELETE
2b70: 20 46 52 4f 4d 20 74 38 20 57 48 45 52 45 20 72   FROM t8 WHERE r
2b80: 6f 77 69 64 20 3d 20 32 3b 0a 20 20 20 20 44 45  owid = 2;.    DE
2b90: 4c 45 54 45 20 46 52 4f 4d 20 74 38 20 57 48 45  LETE FROM t8 WHE
2ba0: 52 45 20 72 6f 77 69 64 20 3d 20 31 3b 0a 20 20  RE rowid = 1;.  
2bb0: 43 4f 4d 4d 49 54 3b 0a 7d 0a 0a 23 2d 2d 2d 2d  COMMIT;.}..#----
2bc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2bd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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 0a 23 20 54 65 73 74 20 73 6f 6d  -----.# Test som
2c10: 65 20 62 72 61 6e 63 68 65 73 20 69 6e 20 74 68  e branches in th
2c20: 65 20 63 6f 64 65 20 74 68 61 74 20 68 61 6e 64  e code that hand
2c30: 6c 65 73 20 22 73 70 65 63 69 61 6c 22 20 69 6e  les "special" in
2c40: 73 65 72 74 73 20 6c 69 6b 65 3a 0a 23 0a 23 20  serts like:.#.# 
2c50: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
2c60: 28 74 31 29 20 56 41 4c 55 45 53 28 27 6f 70 74  (t1) VALUES('opt
2c70: 69 6d 69 7a 65 27 29 3b 0a 23 0a 23 20 41 6c 73  imize');.#.# Als
2c80: 6f 20 74 65 73 74 20 74 68 61 74 20 61 6e 20 6f  o test that an o
2c90: 70 74 69 6d 69 7a 65 20 28 49 4e 53 45 52 54 20  ptimize (INSERT 
2ca0: 6d 65 74 68 6f 64 29 20 77 6f 72 6b 73 20 6f 6e  method) works on
2cb0: 20 61 6e 20 65 6d 70 74 79 20 74 61 62 6c 65 2e   an empty table.
2cc0: 0a 23 0a 73 65 74 20 44 4f 5f 4d 41 4c 4c 4f 43  .#.set DO_MALLOC
2cd0: 5f 54 45 53 54 20 30 0a 64 6f 5f 74 65 73 74 20  _TEST 0.do_test 
2ce0: 66 74 73 33 63 6f 76 2d 39 2e 31 20 7b 0a 20 20  fts3cov-9.1 {.  
2cf0: 65 78 65 63 73 71 6c 20 7b 20 43 52 45 41 54 45  execsql { CREATE
2d00: 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 78   VIRTUAL TABLE x
2d10: 78 20 55 53 49 4e 47 20 66 74 73 33 20 7d 0a 7d  x USING fts3 }.}
2d20: 20 7b 7d 0a 64 6f 5f 65 72 72 6f 72 5f 74 65 73   {}.do_error_tes
2d30: 74 20 66 74 73 33 63 6f 76 2d 39 2e 32 20 7b 0a  t fts3cov-9.2 {.
2d40: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78    INSERT INTO xx
2d50: 28 78 78 29 20 56 41 4c 55 45 53 28 27 6f 70 74  (xx) VALUES('opt
2d60: 69 6d 69 73 65 27 29 3b 20 20 20 2d 2d 20 42 72  imise');   -- Br
2d70: 69 74 69 73 68 20 73 70 65 6c 6c 69 6e 67 0a 7d  itish spelling.}
2d80: 20 7b 53 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f   {SQL logic erro
2d90: 72 7d 0a 64 6f 5f 65 72 72 6f 72 5f 74 65 73 74  r}.do_error_test
2da0: 20 66 74 73 33 63 6f 76 2d 39 2e 33 20 7b 0a 20   fts3cov-9.3 {. 
2db0: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78 28   INSERT INTO xx(
2dc0: 78 78 29 20 56 41 4c 55 45 53 28 27 73 68 6f 72  xx) VALUES('shor
2dd0: 74 27 29 3b 0a 7d 20 7b 53 51 4c 20 6c 6f 67 69  t');.} {SQL logi
2de0: 63 20 65 72 72 6f 72 7d 0a 64 6f 5f 65 72 72 6f  c error}.do_erro
2df0: 72 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 39  r_test fts3cov-9
2e00: 2e 34 20 7b 0a 20 20 49 4e 53 45 52 54 20 49 4e  .4 {.  INSERT IN
2e10: 54 4f 20 78 78 28 78 78 29 20 56 41 4c 55 45 53  TO xx(xx) VALUES
2e20: 28 27 77 61 79 74 6f 6f 6c 6f 6e 67 74 6f 62 65  ('waytoolongtobe
2e30: 63 6f 72 72 65 63 74 27 29 3b 0a 7d 20 7b 53 51  correct');.} {SQ
2e40: 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 7d 0a 64  L logic error}.d
2e50: 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 39  o_test fts3cov-9
2e60: 2e 35 20 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b  .5 {.  execsql {
2e70: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 78 78 28   INSERT INTO xx(
2e80: 78 78 29 20 56 41 4c 55 45 53 28 27 6f 70 74 69  xx) VALUES('opti
2e90: 6d 69 7a 65 27 29 20 7d 0a 7d 20 7b 7d 0a 0a 23  mize') }.} {}..#
2ea0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2eb0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2ec0: 2d 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 0a 23 20 54 65 73 74  ---------.# Test
2ef0: 20 74 68 61 74 20 61 20 74 61 62 6c 65 20 63 61   that a table ca
2f00: 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 69  n be optimized i
2f10: 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
2f20: 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 68  a transaction wh
2f30: 65 6e 0a 23 20 74 68 65 20 70 65 6e 64 69 6e 67  en.# the pending
2f40: 2d 74 65 72 6d 73 20 74 61 62 6c 65 20 69 73 20  -terms table is 
2f50: 6e 6f 6e 2d 65 6d 70 74 79 2e 20 54 68 69 73 20  non-empty. This 
2f60: 63 61 73 65 20 69 6e 76 6f 6c 76 65 73 20 73 6f  case involves so
2f70: 6d 65 20 65 78 74 72 61 0a 23 20 62 72 61 6e 63  me extra.# branc
2f80: 68 65 73 20 62 65 63 61 75 73 65 20 64 61 74 61  hes because data
2f90: 20 6d 75 73 74 20 62 65 20 72 65 61 64 20 6e 6f   must be read no
2fa0: 74 20 6f 6e 6c 79 20 66 72 6f 6d 20 74 68 65 20  t only from the 
2fb0: 64 61 74 61 62 61 73 65 2c 20 62 75 74 0a 23 20  database, but.# 
2fc0: 61 6c 73 6f 20 66 72 6f 6d 20 74 68 65 20 70 65  also from the pe
2fd0: 6e 64 69 6e 67 2d 74 65 72 6d 73 20 74 61 62 6c  nding-terms tabl
2fe0: 65 2e 0a 23 0a 64 6f 5f 6d 61 6c 6c 6f 63 5f 74  e..#.do_malloc_t
2ff0: 65 73 74 20 66 74 73 33 63 6f 76 2d 31 30 20 2d  est fts3cov-10 -
3000: 73 71 6c 70 72 65 70 20 7b 0a 20 20 43 52 45 41  sqlprep {.  CREA
3010: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
3020: 20 74 31 30 20 55 53 49 4e 47 20 66 74 73 33 3b   t10 USING fts3;
3030: 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74  .  INSERT INTO t
3040: 31 30 20 56 41 4c 55 45 53 28 27 4f 70 74 69 6d  10 VALUES('Optim
3050: 69 73 69 6e 67 20 69 6d 61 67 65 73 20 66 6f 72  ising images for
3060: 20 74 68 65 20 77 65 62 20 69 73 20 61 20 74 72   the web is a tr
3070: 69 63 6b 79 20 62 75 73 69 6e 65 73 73 27 29 3b  icky business');
3080: 0a 20 20 42 45 47 49 4e 3b 0a 20 20 20 20 49 4e  .  BEGIN;.    IN
3090: 53 45 52 54 20 49 4e 54 4f 20 74 31 30 20 56 41  SERT INTO t10 VA
30a0: 4c 55 45 53 28 27 59 6f 75 20 68 61 76 65 20 74  LUES('You have t
30b0: 6f 20 67 65 74 20 74 68 65 20 72 69 67 68 74 20  o get the right 
30c0: 62 61 6c 61 6e 63 65 20 62 65 74 77 65 65 6e 27  balance between'
30d0: 29 3b 0a 7d 20 2d 73 71 6c 62 6f 64 79 20 7b 0a  );.} -sqlbody {.
30e0: 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
30f0: 30 28 74 31 30 29 20 56 41 4c 55 45 53 28 27 6f  0(t10) VALUES('o
3100: 70 74 69 6d 69 7a 65 27 29 3b 0a 7d 0a 0a 23 2d  ptimize');.}..#-
3110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
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 0a 23 20 54 65 73 74 20  --------.# Test 
3160: 61 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72  a full-text quer
3170: 79 20 66 6f 72 20 61 20 74 65 72 6d 20 74 68 61  y for a term tha
3180: 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 74 68  t was once in th
3190: 65 20 69 6e 64 65 78 2c 20 62 75 74 20 69 73 0a  e index, but is.
31a0: 23 20 6e 6f 20 6c 6f 6e 67 65 72 2e 0a 23 0a 64  # no longer..#.d
31b0: 6f 5f 74 65 73 74 20 66 74 73 33 63 6f 76 2d 31  o_test fts3cov-1
31c0: 31 2e 31 20 7b 0a 20 20 65 78 65 63 73 71 6c 20  1.1 {.  execsql 
31d0: 7b 20 0a 20 20 20 20 43 52 45 41 54 45 20 56 49  { .    CREATE VI
31e0: 52 54 55 41 4c 20 54 41 42 4c 45 20 78 78 20 55  RTUAL TABLE xx U
31f0: 53 49 4e 47 20 66 74 73 33 3b 0a 20 20 20 20 49  SING fts3;.    I
3200: 4e 53 45 52 54 20 49 4e 54 4f 20 78 78 20 56 41  NSERT INTO xx VA
3210: 4c 55 45 53 28 27 6f 6e 65 20 74 77 6f 20 74 68  LUES('one two th
3220: 72 65 65 27 29 3b 0a 20 20 20 20 49 4e 53 45 52  ree');.    INSER
3230: 54 20 49 4e 54 4f 20 78 78 20 56 41 4c 55 45 53  T INTO xx VALUES
3240: 28 27 66 6f 75 72 20 66 69 76 65 20 73 69 78 27  ('four five six'
3250: 29 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  );.    DELETE FR
3260: 4f 4d 20 78 78 20 57 48 45 52 45 20 64 6f 63 69  OM xx WHERE doci
3270: 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 65 78 65  d = 1;.  }.  exe
3280: 63 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  csql { SELECT * 
3290: 46 52 4f 4d 20 78 78 20 57 48 45 52 45 20 78 78  FROM xx WHERE xx
32a0: 20 4d 41 54 43 48 20 27 74 77 6f 27 20 7d 0a 7d   MATCH 'two' }.}
32b0: 20 7b 7d 0a 0a 0a 64 6f 5f 6d 61 6c 6c 6f 63 5f   {}...do_malloc_
32c0: 74 65 73 74 20 66 74 73 33 63 6f 76 2d 31 32 20  test fts3cov-12 
32d0: 2d 73 71 6c 70 72 65 70 20 7b 0a 20 20 43 52 45  -sqlprep {.  CRE
32e0: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
32f0: 45 20 74 31 32 20 55 53 49 4e 47 20 66 74 73 33  E t12 USING fts3
3300: 3b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  ;.  INSERT INTO 
3310: 74 31 32 20 56 41 4c 55 45 53 28 27 69 73 20 6f  t12 VALUES('is o
3320: 6e 65 20 6f 66 20 74 68 65 20 74 77 6f 20 74 6f  ne of the two to
3330: 67 74 68 65 72 27 29 3b 0a 20 20 42 45 47 49 4e  gther');.  BEGIN
3340: 3b 0a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54  ;.    INSERT INT
3350: 4f 20 74 31 32 20 56 41 4c 55 45 53 28 27 6f 6e  O t12 VALUES('on
3360: 65 20 77 68 69 63 68 20 77 61 73 20 61 70 70 72  e which was appr
3370: 6f 70 72 69 61 74 65 20 61 74 20 74 68 65 20 74  opriate at the t
3380: 69 6d 65 27 29 3b 0a 7d 20 2d 73 71 6c 62 6f 64  ime');.} -sqlbod
3390: 79 20 7b 0a 20 20 53 45 4c 45 43 54 20 2a 20 46  y {.  SELECT * F
33a0: 52 4f 4d 20 74 31 32 20 57 48 45 52 45 20 74 31  ROM t12 WHERE t1
33b0: 32 20 4d 41 54 43 48 20 27 6f 6e 65 27 0a 7d 0a  2 MATCH 'one'.}.
33c0: 0a 64 6f 5f 6d 61 6c 6c 6f 63 5f 74 65 73 74 20  .do_malloc_test 
33d0: 66 74 73 33 63 6f 76 2d 31 33 20 2d 73 71 6c 70  fts3cov-13 -sqlp
33e0: 72 65 70 20 7b 0a 20 20 50 52 41 47 4d 41 20 65  rep {.  PRAGMA e
33f0: 6e 63 6f 64 69 6e 67 20 3d 20 27 55 54 46 2d 31  ncoding = 'UTF-1
3400: 36 27 3b 0a 20 20 43 52 45 41 54 45 20 56 49 52  6';.  CREATE VIR
3410: 54 55 41 4c 20 54 41 42 4c 45 20 74 31 33 20 55  TUAL TABLE t13 U
3420: 53 49 4e 47 20 66 74 73 33 3b 0a 20 20 49 4e 53  SING fts3;.  INS
3430: 45 52 54 20 49 4e 54 4f 20 74 31 33 20 56 41 4c  ERT INTO t13 VAL
3440: 55 45 53 28 27 74 77 6f 20 73 63 61 6c 61 72 20  UES('two scalar 
3450: 66 75 6e 63 74 69 6f 6e 73 27 29 3b 0a 20 20 49  functions');.  I
3460: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 33 20 56  NSERT INTO t13 V
3470: 41 4c 55 45 53 28 27 73 63 61 6c 61 72 20 74 77  ALUES('scalar tw
3480: 6f 20 66 75 6e 63 74 69 6f 6e 73 27 29 3b 0a 20  o functions');. 
3490: 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 33   INSERT INTO t13
34a0: 20 56 41 4c 55 45 53 28 27 66 75 6e 63 74 69 6f   VALUES('functio
34b0: 6e 73 20 73 63 61 6c 61 72 20 74 77 6f 27 29 3b  ns scalar two');
34c0: 0a 7d 20 2d 73 71 6c 62 6f 64 79 20 7b 0a 20 20  .} -sqlbody {.  
34d0: 53 45 4c 45 43 54 20 73 6e 69 70 70 65 74 28 74  SELECT snippet(t
34e0: 31 33 2c 20 27 25 25 27 2c 20 27 25 25 27 2c 20  13, '%%', '%%', 
34f0: 27 23 27 29 20 46 52 4f 4d 20 74 31 33 20 57 48  '#') FROM t13 WH
3500: 45 52 45 20 74 31 33 20 4d 41 54 43 48 20 27 74  ERE t13 MATCH 't
3510: 77 6f 27 3b 0a 20 20 53 45 4c 45 43 54 20 73 6e  wo';.  SELECT sn
3520: 69 70 70 65 74 28 74 31 33 2c 20 27 25 25 27 2c  ippet(t13, '%%',
3530: 20 27 25 25 27 29 20 46 52 4f 4d 20 74 31 33 20   '%%') FROM t13 
3540: 57 48 45 52 45 20 74 31 33 20 4d 41 54 43 48 20  WHERE t13 MATCH 
3550: 27 74 77 6f 27 3b 0a 20 20 53 45 4c 45 43 54 20  'two';.  SELECT 
3560: 73 6e 69 70 70 65 74 28 74 31 33 2c 20 27 25 25  snippet(t13, '%%
3570: 27 29 20 46 52 4f 4d 20 74 31 33 20 57 48 45 52  ') FROM t13 WHER
3580: 45 20 74 31 33 20 4d 41 54 43 48 20 27 74 77 6f  E t13 MATCH 'two
3590: 27 3b 0a 7d 0a 0a 64 6f 5f 65 78 65 63 73 71 6c  ';.}..do_execsql
35a0: 5f 74 65 73 74 20 31 34 2e 30 20 7b 0a 20 20 43  _test 14.0 {.  C
35b0: 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
35c0: 42 4c 45 20 74 31 34 20 55 53 49 4e 47 20 66 74  BLE t14 USING ft
35d0: 73 34 28 61 2c 20 62 29 3b 0a 20 20 49 4e 53 45  s4(a, b);.  INSE
35e0: 52 54 20 49 4e 54 4f 20 74 31 34 20 56 41 4c 55  RT INTO t14 VALU
35f0: 45 53 28 27 6f 6e 65 20 74 77 6f 20 74 68 72 65  ES('one two thre
3600: 65 27 2c 20 27 6f 6e 65 20 74 68 72 65 65 20 66  e', 'one three f
3610: 6f 75 72 27 29 3b 0a 20 20 49 4e 53 45 52 54 20  our');.  INSERT 
3620: 49 4e 54 4f 20 74 31 34 20 56 41 4c 55 45 53 28  INTO t14 VALUES(
3630: 27 61 20 62 20 63 27 2c 20 27 64 20 65 20 61 27  'a b c', 'd e a'
3640: 29 3b 0a 7d 0a 64 6f 5f 65 78 65 63 73 71 6c 5f  );.}.do_execsql_
3650: 74 65 73 74 20 31 34 2e 31 20 7b 0a 20 20 53 45  test 14.1 {.  SE
3660: 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20  LECT rowid FROM 
3670: 74 31 34 20 57 48 45 52 45 20 74 31 34 20 4d 41  t14 WHERE t14 MA
3680: 54 43 48 20 27 22 6f 6e 65 20 74 77 6f 20 74 68  TCH '"one two th
3690: 72 65 65 22 27 0a 7d 20 7b 31 7d 0a 64 6f 5f 65  ree"'.} {1}.do_e
36a0: 78 65 63 73 71 6c 5f 74 65 73 74 20 31 34 2e 32  xecsql_test 14.2
36b0: 20 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f 77 69   {.  SELECT rowi
36c0: 64 20 46 52 4f 4d 20 74 31 34 20 57 48 45 52 45  d FROM t14 WHERE
36d0: 20 74 31 34 20 4d 41 54 43 48 20 27 22 6f 6e 65   t14 MATCH '"one
36e0: 20 66 6f 75 72 22 27 0a 7d 20 7b 7d 0a 64 6f 5f   four"'.} {}.do_
36f0: 65 78 65 63 73 71 6c 5f 74 65 73 74 20 31 34 2e  execsql_test 14.
3700: 33 20 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f 77  3 {.  SELECT row
3710: 69 64 20 46 52 4f 4d 20 74 31 34 20 57 48 45 52  id FROM t14 WHER
3720: 45 20 74 31 34 20 4d 41 54 43 48 20 27 22 65 20  E t14 MATCH '"e 
3730: 61 22 27 0a 7d 20 7b 32 7d 0a 64 6f 5f 65 78 65  a"'.} {2}.do_exe
3740: 63 73 71 6c 5f 74 65 73 74 20 31 34 2e 35 20 7b  csql_test 14.5 {
3750: 0a 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20  .  SELECT rowid 
3760: 46 52 4f 4d 20 74 31 34 20 57 48 45 52 45 20 74  FROM t14 WHERE t
3770: 31 34 20 4d 41 54 43 48 20 27 22 65 20 62 22 27  14 MATCH '"e b"'
3780: 0a 7d 20 7b 7d 0a 64 6f 5f 63 61 74 63 68 73 71  .} {}.do_catchsq
3790: 6c 5f 74 65 73 74 20 31 34 2e 36 20 7b 0a 20 20  l_test 14.6 {.  
37a0: 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f  SELECT rowid FRO
37b0: 4d 20 74 31 34 20 57 48 45 52 45 20 72 6f 77 69  M t14 WHERE rowi
37c0: 64 20 4d 41 54 43 48 20 27 6f 6e 65 27 0a 7d 20  d MATCH 'one'.} 
37d0: 7b 31 20 7b 75 6e 61 62 6c 65 20 74 6f 20 75 73  {1 {unable to us
37e0: 65 20 66 75 6e 63 74 69 6f 6e 20 4d 41 54 43 48  e function MATCH
37f0: 20 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65   in the requeste
3800: 64 20 63 6f 6e 74 65 78 74 7d 7d 0a 64 6f 5f 63  d context}}.do_c
3810: 61 74 63 68 73 71 6c 5f 74 65 73 74 20 31 34 2e  atchsql_test 14.
3820: 37 20 7b 0a 20 20 53 45 4c 45 43 54 20 72 6f 77  7 {.  SELECT row
3830: 69 64 20 46 52 4f 4d 20 74 31 34 20 57 48 45 52  id FROM t14 WHER
3840: 45 20 64 6f 63 69 64 20 4d 41 54 43 48 20 27 6f  E docid MATCH 'o
3850: 6e 65 27 0a 7d 20 7b 31 20 7b 75 6e 61 62 6c 65  ne'.} {1 {unable
3860: 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69 6f 6e   to use function
3870: 20 4d 41 54 43 48 20 69 6e 20 74 68 65 20 72 65   MATCH in the re
3880: 71 75 65 73 74 65 64 20 63 6f 6e 74 65 78 74 7d  quested context}
3890: 7d 0a 0a 64 6f 5f 65 78 65 63 73 71 6c 5f 74 65  }..do_execsql_te
38a0: 73 74 20 31 35 2e 30 20 7b 0a 20 20 43 52 45 41  st 15.0 {.  CREA
38b0: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
38c0: 20 74 31 35 20 55 53 49 4e 47 20 66 74 73 34 28   t15 USING fts4(
38d0: 61 2c 20 62 2c 20 63 29 3b 0a 20 20 49 4e 53 45  a, b, c);.  INSE
38e0: 52 54 20 49 4e 54 4f 20 74 31 35 20 56 41 4c 55  RT INTO t15 VALU
38f0: 45 53 28 27 61 62 63 20 64 65 66 20 67 68 69 27  ES('abc def ghi'
3900: 2c 20 27 61 62 63 32 20 64 65 66 32 20 67 68 69  , 'abc2 def2 ghi
3910: 32 27 2c 20 27 61 62 63 33 20 64 65 66 33 20 67  2', 'abc3 def3 g
3920: 68 69 33 27 29 3b 0a 20 20 49 4e 53 45 52 54 20  hi3');.  INSERT 
3930: 49 4e 54 4f 20 74 31 35 20 56 41 4c 55 45 53 28  INTO t15 VALUES(
3940: 27 61 62 63 32 20 64 65 66 32 20 67 68 69 32 27  'abc2 def2 ghi2'
3950: 2c 20 27 61 62 63 32 20 64 65 66 32 20 67 68 69  , 'abc2 def2 ghi
3960: 32 27 2c 20 27 61 62 63 20 64 65 66 33 20 67 68  2', 'abc def3 gh
3970: 69 33 27 29 3b 0a 7d 0a 64 6f 5f 65 78 65 63 73  i3');.}.do_execs
3980: 71 6c 5f 74 65 73 74 20 31 35 2e 31 20 7b 0a 20  ql_test 15.1 {. 
3990: 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52   SELECT rowid FR
39a0: 4f 4d 20 74 31 35 20 57 48 45 52 45 20 74 31 35  OM t15 WHERE t15
39b0: 20 4d 41 54 43 48 20 27 22 61 62 63 2a 20 64 65   MATCH '"abc* de
39c0: 66 32 22 27 0a 7d 20 7b 31 20 32 7d 0a 0a 23 20  f2"'.} {1 2}..# 
39d0: 54 65 73 74 20 61 20 63 6f 72 72 75 70 74 69 6f  Test a corruptio
39e0: 6e 20 63 61 73 65 2e 0a 23 0a 73 71 6c 69 74 65  n case..#.sqlite
39f0: 33 5f 64 62 5f 63 6f 6e 66 69 67 20 64 62 20 44  3_db_config db D
3a00: 45 46 45 4e 53 49 56 45 20 30 0a 64 6f 5f 65 78  EFENSIVE 0.do_ex
3a10: 65 63 73 71 6c 5f 74 65 73 74 20 31 36 2e 31 20  ecsql_test 16.1 
3a20: 7b 0a 20 20 43 52 45 41 54 45 20 56 49 52 54 55  {.  CREATE VIRTU
3a30: 41 4c 20 54 41 42 4c 45 20 74 31 36 20 55 53 49  AL TABLE t16 USI
3a40: 4e 47 20 66 74 73 34 3b 0a 20 20 49 4e 53 45 52  NG fts4;.  INSER
3a50: 54 20 49 4e 54 4f 20 74 31 36 20 56 41 4c 55 45  T INTO t16 VALUE
3a60: 53 28 27 74 68 65 6f 72 65 74 69 63 61 6c 20 77  S('theoretical w
3a70: 6f 72 6b 20 74 6f 20 65 78 61 6d 69 6e 65 20 74  ork to examine t
3a80: 68 65 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 27  he relationship'
3a90: 29 3b 0a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  );.  INSERT INTO
3aa0: 20 74 31 36 20 56 41 4c 55 45 53 28 27 73 6f 6c   t16 VALUES('sol
3ab0: 75 74 69 6f 6e 20 6f 66 20 6f 75 72 20 70 72 6f  ution of our pro
3ac0: 62 6c 65 6d 73 20 6f 6e 20 74 68 65 20 69 6e 76  blems on the inv
3ad0: 69 73 69 62 6c 65 27 29 3b 0a 20 20 44 45 4c 45  isible');.  DELE
3ae0: 54 45 20 46 52 4f 4d 20 74 31 36 5f 63 6f 6e 74  TE FROM t16_cont
3af0: 65 6e 74 20 57 48 45 52 45 20 72 6f 77 69 64 20  ent WHERE rowid 
3b00: 3d 20 32 3b 0a 7d 0a 64 6f 5f 63 61 74 63 68 73  = 2;.}.do_catchs
3b10: 71 6c 5f 74 65 73 74 20 31 36 2e 32 20 7b 0a 20  ql_test 16.2 {. 
3b20: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
3b30: 31 36 20 57 48 45 52 45 20 74 31 36 20 4d 41 54  16 WHERE t16 MAT
3b40: 43 48 20 27 69 6e 76 69 73 69 62 6c 65 27 0a 7d  CH 'invisible'.}
3b50: 20 7b 31 20 7b 64 61 74 61 62 61 73 65 20 64 69   {1 {database di
3b60: 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66  sk image is malf
3b70: 6f 72 6d 65 64 7d 7d 0a 0a 23 20 41 6e 64 20 61  ormed}}..# And a
3b80: 6e 6f 74 68 65 72 20 63 6f 72 72 75 70 74 69 6f  nother corruptio
3b90: 6e 20 74 65 73 74 20 63 61 73 65 2e 0a 23 0a 64  n test case..#.d
3ba0: 6f 5f 65 78 65 63 73 71 6c 5f 74 65 73 74 20 31  o_execsql_test 1
3bb0: 37 2e 31 20 7b 0a 20 20 43 52 45 41 54 45 20 56  7.1 {.  CREATE V
3bc0: 49 52 54 55 41 4c 20 54 41 42 4c 45 20 74 31 37  IRTUAL TABLE t17
3bd0: 20 55 53 49 4e 47 20 66 74 73 34 3b 0a 20 20 49   USING fts4;.  I
3be0: 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 37 28 63  NSERT INTO t17(c
3bf0: 6f 6e 74 65 6e 74 29 20 56 41 4c 55 45 53 28 27  ontent) VALUES('
3c00: 6f 6e 65 20 6f 6e 65 20 6f 6e 65 27 29 3b 0a 20  one one one');. 
3c10: 20 55 50 44 41 54 45 20 74 31 37 5f 73 65 67 64   UPDATE t17_segd
3c20: 69 72 20 53 45 54 20 72 6f 6f 74 20 3d 20 58 27  ir SET root = X'
3c30: 30 30 30 33 36 46 36 45 36 35 46 46 46 46 46 46  00036F6E65FFFFFF
3c40: 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46 46  FFFFFFFFFFFFFFFF
3c50: 30 32 30 33 30 33 30 30 27 0a 7d 20 7b 7d 0a 64  02030300'.} {}.d
3c60: 6f 5f 63 61 74 63 68 73 71 6c 5f 74 65 73 74 20  o_catchsql_test 
3c70: 31 37 2e 32 20 7b 0a 20 20 53 45 4c 45 43 54 20  17.2 {.  SELECT 
3c80: 2a 20 46 52 4f 4d 20 74 31 37 20 57 48 45 52 45  * FROM t17 WHERE
3c90: 20 74 31 37 20 4d 41 54 43 48 20 27 6f 6e 65 27   t17 MATCH 'one'
3ca0: 0a 7d 20 7b 31 20 7b 64 61 74 61 62 61 73 65 20  .} {1 {database 
3cb0: 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61  disk image is ma
3cc0: 6c 66 6f 72 6d 65 64 7d 7d 0a 0a 0a 0a 0a 66 69  lformed}}.....fi
3cd0: 6e 69 73 68 5f 74 65 73 74 0a                    nish_test.