~ubuntu-branches/ubuntu/maverick/sqlite3/maverick-updates

« back to all changes in this revision

Viewing changes to test/func.test

  • Committer: Bazaar Package Importer
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2008-10-01 20:16:18 UTC
  • mfrom: (3.1.20 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081001201618-yfvqqj1qs29wdtcc
Tags: 3.5.9-5
Backport fix for distinct on indexes (closes: #500792).

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
# This file implements regression tests for SQLite library.  The
12
12
# focus of this file is testing built-in functions.
13
13
#
14
 
# $Id: func.test,v 1.55 2006/09/16 21:45:14 drh Exp $
 
14
# $Id: func.test,v 1.78 2008/04/16 12:58:54 drh Exp $
15
15
 
16
16
set testdir [file dirname $argv0]
17
17
source $testdir/tester.tcl
220
220
do_test func-5.3 {
221
221
  execsql {SELECT upper(a), lower(a) FROM t2}
222
222
} {1 1 {} {} 345 345 {} {} 67890 67890}
223
 
do_test func-5.4 {
224
 
  catchsql {SELECT upper(a,5) FROM t2}
225
 
} {1 {wrong number of arguments to function upper()}}
 
223
ifcapable !icu {
 
224
  do_test func-5.4 {
 
225
    catchsql {SELECT upper(a,5) FROM t2}
 
226
  } {1 {wrong number of arguments to function upper()}}
 
227
}
226
228
do_test func-5.5 {
227
229
  catchsql {SELECT upper(*) FROM t2}
228
230
} {1 {wrong number of arguments to function upper()}}
296
298
    SELECT random() is not null;
297
299
  }
298
300
} {1}
 
301
do_test func-9.2 {
 
302
  execsql {
 
303
    SELECT typeof(random());
 
304
  }
 
305
} {integer}
 
306
do_test func-9.3 {
 
307
  execsql {
 
308
    SELECT randomblob(32) is not null;
 
309
  }
 
310
} {1}
 
311
do_test func-9.4 {
 
312
  execsql {
 
313
    SELECT typeof(randomblob(32));
 
314
  }
 
315
} {blob}
 
316
do_test func-9.5 {
 
317
  execsql {
 
318
    SELECT length(randomblob(32)), length(randomblob(-5)),
 
319
           length(randomblob(2000))
 
320
  }
 
321
} {32 1 2000}
299
322
 
 
323
# The "hex()" function was added in order to be able to render blobs
 
324
# generated by randomblob().  So this seems like a good place to test
 
325
# hex().
 
326
#
 
327
ifcapable bloblit {
 
328
  do_test func-9.10 {
 
329
    execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')}
 
330
  } {00112233445566778899AABBCCDDEEFF}
 
331
}
 
332
set encoding [db one {PRAGMA encoding}]
 
333
if {$encoding=="UTF-16le"} {
 
334
  do_test func-9.11-utf16le {
 
335
    execsql {SELECT hex(replace('abcdefg','ef','12'))}
 
336
  } {6100620063006400310032006700}
 
337
  do_test func-9.12-utf16le {
 
338
    execsql {SELECT hex(replace('abcdefg','','12'))}
 
339
  } {{}}
 
340
  do_test func-9.13-utf16le {
 
341
    execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
 
342
  } {610061006100610061006100620063006400650066006700}
 
343
} elseif {$encoding=="UTF-8"} {
 
344
  do_test func-9.11-utf8 {
 
345
    execsql {SELECT hex(replace('abcdefg','ef','12'))}
 
346
  } {61626364313267}
 
347
  do_test func-9.12-utf8 {
 
348
    execsql {SELECT hex(replace('abcdefg','','12'))}
 
349
  } {{}}
 
350
  do_test func-9.13-utf8 {
 
351
    execsql {SELECT hex(replace('aabcdefg','a','aaa'))}
 
352
  } {616161616161626364656667}
 
353
}
 
354
  
300
355
# Use the "sqlite_register_test_function" TCL command which is part of
301
356
# the text fixture in order to verify correct operation of some of
302
357
# the user-defined SQL function APIs that are not used by the built-in
369
424
# value. Function test_destructor_count() returns the number of outstanding
370
425
# destructor calls for values returned by test_destructor().
371
426
#
372
 
do_test func-12.1 {
373
 
  execsql {
374
 
    SELECT test_destructor('hello world'), test_destructor_count();
375
 
  }
376
 
} {{hello world} 1}
 
427
if {[db eval {PRAGMA encoding}]=="UTF-8"} {
 
428
  do_test func-12.1-utf8 {
 
429
    execsql {
 
430
      SELECT test_destructor('hello world'), test_destructor_count();
 
431
    }
 
432
  } {{hello world} 1}
 
433
} else {
 
434
  do_test func-12.1-utf16 {
 
435
    execsql {
 
436
      SELECT test_destructor16('hello world'), test_destructor_count();
 
437
    }
 
438
  } {{hello world} 1}
 
439
}
377
440
do_test func-12.2 {
378
441
  execsql {
379
442
    SELECT test_destructor_count();
381
444
} {0}
382
445
do_test func-12.3 {
383
446
  execsql {
384
 
    SELECT test_destructor('hello')||' world', test_destructor_count();
 
447
    SELECT test_destructor('hello')||' world'
385
448
  }
386
 
} {{hello world} 0}
 
449
} {{hello world}}
387
450
do_test func-12.4 {
388
451
  execsql {
389
452
    SELECT test_destructor_count();
408
471
  }
409
472
} {}
410
473
 
 
474
 
411
475
# Test that the auxdata API for scalar functions works. This test uses
412
476
# a special user-defined function only available in test builds,
413
477
# test_auxdata(). Function test_auxdata() takes any number of arguments.
414
 
btree_breakpoint
415
478
do_test func-13.1 {
416
479
  execsql {
417
480
    SELECT test_auxdata('hello world');
451
514
  set DB [sqlite3_connection_pointer db]
452
515
  set sql "SELECT test_auxdata( ? , a ) FROM t4;"
453
516
  set STMT [sqlite3_prepare $DB $sql -1 TAIL]
454
 
  sqlite3_bind_text $STMT 1 hello -1
 
517
  sqlite3_bind_text $STMT 1 hello\000 -1
455
518
  set res [list]
456
519
  while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
457
520
    lappend res [sqlite3_column_text $STMT 0]
472
535
} {1}
473
536
 
474
537
do_test func-15.1 {
475
 
  catchsql {
476
 
    select test_error(NULL);
477
 
  }
 
538
  catchsql {select test_error(NULL)}
478
539
} {1 {}}
 
540
do_test func-15.2 {
 
541
  catchsql {select test_error('this is the error message')}
 
542
} {1 {this is the error message}}
 
543
do_test func-15.3 {
 
544
  catchsql {select test_error('this is the error message',12)}
 
545
} {1 {this is the error message}}
 
546
do_test func-15.4 {
 
547
  db errorcode
 
548
} {12}
479
549
 
480
550
# Test the quote function for BLOB and NULL values.
481
551
do_test func-16.1 {
700
770
  }
701
771
}
702
772
 
 
773
# Tests of the REPLACE function.
 
774
#
 
775
do_test func-21.1 {
 
776
  catchsql {
 
777
    SELECT replace(1,2);
 
778
  }
 
779
} {1 {wrong number of arguments to function replace()}}
 
780
do_test func-21.2 {
 
781
  catchsql {
 
782
    SELECT replace(1,2,3,4);
 
783
  }
 
784
} {1 {wrong number of arguments to function replace()}}
 
785
do_test func-21.3 {
 
786
  execsql {
 
787
    SELECT typeof(replace("This is the main test string", NULL, "ALT"));
 
788
  }
 
789
} {null}
 
790
do_test func-21.4 {
 
791
  execsql {
 
792
    SELECT typeof(replace(NULL, "main", "ALT"));
 
793
  }
 
794
} {null}
 
795
do_test func-21.5 {
 
796
  execsql {
 
797
    SELECT typeof(replace("This is the main test string", "main", NULL));
 
798
  }
 
799
} {null}
 
800
do_test func-21.6 {
 
801
  execsql {
 
802
    SELECT replace("This is the main test string", "main", "ALT");
 
803
  }
 
804
} {{This is the ALT test string}}
 
805
do_test func-21.7 {
 
806
  execsql {
 
807
    SELECT replace("This is the main test string", "main", "larger-main");
 
808
  }
 
809
} {{This is the larger-main test string}}
 
810
do_test func-21.8 {
 
811
  execsql {
 
812
    SELECT replace("aaaaaaa", "a", "0123456789");
 
813
  }
 
814
} {0123456789012345678901234567890123456789012345678901234567890123456789}
 
815
 
 
816
ifcapable tclvar {
 
817
  do_test func-21.9 {
 
818
    # Attempt to exploit a buffer-overflow that at one time existed 
 
819
    # in the REPLACE function. 
 
820
    set ::str "[string repeat A 29998]CC[string repeat A 35537]"
 
821
    set ::rep [string repeat B 65536]
 
822
    execsql {
 
823
      SELECT LENGTH(REPLACE($::str, 'C', $::rep));
 
824
    }
 
825
  } [expr 29998 + 2*65536 + 35537]
 
826
}
 
827
 
 
828
# Tests for the TRIM, LTRIM and RTRIM functions.
 
829
#
 
830
do_test func-22.1 {
 
831
  catchsql {SELECT trim(1,2,3)}
 
832
} {1 {wrong number of arguments to function trim()}}
 
833
do_test func-22.2 {
 
834
  catchsql {SELECT ltrim(1,2,3)}
 
835
} {1 {wrong number of arguments to function ltrim()}}
 
836
do_test func-22.3 {
 
837
  catchsql {SELECT rtrim(1,2,3)}
 
838
} {1 {wrong number of arguments to function rtrim()}}
 
839
do_test func-22.4 {
 
840
  execsql {SELECT trim('  hi  ');}
 
841
} {hi}
 
842
do_test func-22.5 {
 
843
  execsql {SELECT ltrim('  hi  ');}
 
844
} {{hi  }}
 
845
do_test func-22.6 {
 
846
  execsql {SELECT rtrim('  hi  ');}
 
847
} {{  hi}}
 
848
do_test func-22.7 {
 
849
  execsql {SELECT trim('  hi  ','xyz');}
 
850
} {{  hi  }}
 
851
do_test func-22.8 {
 
852
  execsql {SELECT ltrim('  hi  ','xyz');}
 
853
} {{  hi  }}
 
854
do_test func-22.9 {
 
855
  execsql {SELECT rtrim('  hi  ','xyz');}
 
856
} {{  hi  }}
 
857
do_test func-22.10 {
 
858
  execsql {SELECT trim('xyxzy  hi  zzzy','xyz');}
 
859
} {{  hi  }}
 
860
do_test func-22.11 {
 
861
  execsql {SELECT ltrim('xyxzy  hi  zzzy','xyz');}
 
862
} {{  hi  zzzy}}
 
863
do_test func-22.12 {
 
864
  execsql {SELECT rtrim('xyxzy  hi  zzzy','xyz');}
 
865
} {{xyxzy  hi  }}
 
866
do_test func-22.13 {
 
867
  execsql {SELECT trim('  hi  ','');}
 
868
} {{  hi  }}
 
869
if {[db one {PRAGMA encoding}]=="UTF-8"} {
 
870
  do_test func-22.14 {
 
871
    execsql {SELECT hex(trim(x'c280e1bfbff48fbfbf6869',x'6162e1bfbfc280'))}
 
872
  } {F48FBFBF6869}
 
873
  do_test func-22.15 {
 
874
    execsql {SELECT hex(trim(x'6869c280e1bfbff48fbfbf61',
 
875
                             x'6162e1bfbfc280f48fbfbf'))}
 
876
  } {6869}
 
877
  do_test func-22.16 {
 
878
    execsql {SELECT hex(trim(x'ceb1ceb2ceb3',x'ceb1'));}
 
879
  } {CEB2CEB3}
 
880
}
 
881
do_test func-22.20 {
 
882
  execsql {SELECT typeof(trim(NULL));}
 
883
} {null}
 
884
do_test func-22.21 {
 
885
  execsql {SELECT typeof(trim(NULL,'xyz'));}
 
886
} {null}
 
887
do_test func-22.22 {
 
888
  execsql {SELECT typeof(trim('hello',NULL));}
 
889
} {null}
 
890
 
 
891
# This is to test the deprecated sqlite3_aggregate_count() API.
 
892
#
 
893
do_test func-23.1 {
 
894
  sqlite3_create_aggregate db
 
895
  execsql {
 
896
    SELECT legacy_count() FROM t6;
 
897
  }
 
898
} {3}
 
899
 
 
900
# The group_concat() function.
 
901
#
 
902
do_test func-24.1 {
 
903
  execsql {
 
904
    SELECT group_concat(t1) FROM tbl1
 
905
  }
 
906
} {this,program,is,free,software}
 
907
do_test func-24.2 {
 
908
  execsql {
 
909
    SELECT group_concat(t1,' ') FROM tbl1
 
910
  }
 
911
} {{this program is free software}}
 
912
do_test func-24.3 {
 
913
  execsql {
 
914
    SELECT group_concat(t1,' ' || rowid || ' ') FROM tbl1
 
915
  }
 
916
} {{this 2 program 3 is 4 free 5 software}}
 
917
do_test func-24.4 {
 
918
  execsql {
 
919
    SELECT group_concat(NULL,t1) FROM tbl1
 
920
  }
 
921
} {{}}
 
922
do_test func-24.5 {
 
923
  execsql {
 
924
    SELECT group_concat(t1,NULL) FROM tbl1
 
925
  }
 
926
} {thisprogramisfreesoftware}
 
927
do_test func-24.6 {
 
928
  execsql {
 
929
    SELECT 'BEGIN-'||group_concat(t1) FROM tbl1
 
930
  }
 
931
} {BEGIN-this,program,is,free,software}
 
932
 
 
933
# Use the test_isolation function to make sure that type conversions
 
934
# on function arguments do not effect subsequent arguments.
 
935
#
 
936
do_test func-25.1 {
 
937
  execsql {SELECT test_isolation(t1,t1) FROM tbl1}
 
938
} {this program is free software}
 
939
 
703
940
finish_test