17d9bd4e1Sdrh# 2006 February 16 27d9bd4e1Sdrh# 37d9bd4e1Sdrh# The author disclaims copyright to this source code. In place of 47d9bd4e1Sdrh# a legal notice, here is a blessing: 57d9bd4e1Sdrh# 67d9bd4e1Sdrh# May you do good and not evil. 77d9bd4e1Sdrh# May you find forgiveness for yourself and forgive others. 87d9bd4e1Sdrh# May you share freely, never taking more than you give. 97d9bd4e1Sdrh# 107d9bd4e1Sdrh#*********************************************************************** 117d9bd4e1Sdrh# 127d9bd4e1Sdrh# This file contains code to verify that the SQLITE_UTF16_ALIGNED 137d9bd4e1Sdrh# flag passed into the sqlite3_create_collation() function insures 147d9bd4e1Sdrh# that all strings passed to that function are aligned on an even 157d9bd4e1Sdrh# byte boundary. 167d9bd4e1Sdrh# 17bbf695d6Sdrh# $Id: utf16align.test,v 1.2 2008/11/07 03:29:34 drh Exp $ 187d9bd4e1Sdrh 197d9bd4e1Sdrhset testdir [file dirname $argv0] 207d9bd4e1Sdrhsource $testdir/tester.tcl 217d9bd4e1Sdrh 227d9bd4e1Sdrh# Skip this entire test if we do not support UTF16 237d9bd4e1Sdrh# 247d9bd4e1Sdrhifcapable !utf16 { 257d9bd4e1Sdrh finish_test 267d9bd4e1Sdrh return 277d9bd4e1Sdrh} 287d9bd4e1Sdrh 297d9bd4e1Sdrh# Create a database with a UTF16 encoding. Put in lots of string 307d9bd4e1Sdrh# data of varying lengths. 317d9bd4e1Sdrh# 327d9bd4e1Sdrhdo_test utf16align-1.0 { 337d9bd4e1Sdrh set unaligned_string_counter 0 347d9bd4e1Sdrh add_alignment_test_collations [sqlite3_connection_pointer db] 35*7d44b22dSdrh sqlite3_db_config db SQLITE_DBCONFIG_DQS_DML 1 367d9bd4e1Sdrh execsql { 377d9bd4e1Sdrh PRAGMA encoding=UTF16; 387d9bd4e1Sdrh CREATE TABLE t1( 397d9bd4e1Sdrh id INTEGER PRIMARY KEY, 407d9bd4e1Sdrh spacer TEXT, 417d9bd4e1Sdrh a TEXT COLLATE utf16_aligned, 427d9bd4e1Sdrh b TEXT COLLATE utf16_unaligned 437d9bd4e1Sdrh ); 447d9bd4e1Sdrh INSERT INTO t1(a) VALUES("abc"); 457d9bd4e1Sdrh INSERT INTO t1(a) VALUES("defghi"); 467d9bd4e1Sdrh INSERT INTO t1(a) VALUES("jklmnopqrstuv"); 477d9bd4e1Sdrh INSERT INTO t1(a) VALUES("wxyz0123456789-"); 487d9bd4e1Sdrh UPDATE t1 SET b=a||'-'||a; 497d9bd4e1Sdrh INSERT INTO t1(a,b) SELECT a||b, b||a FROM t1; 507d9bd4e1Sdrh INSERT INTO t1(a,b) SELECT a||b, b||a FROM t1; 517d9bd4e1Sdrh INSERT INTO t1(a,b) SELECT a||b, b||a FROM t1; 527d9bd4e1Sdrh INSERT INTO t1(a,b) VALUES('one','two'); 537d9bd4e1Sdrh INSERT INTO t1(a,b) SELECT a, b FROM t1; 547d9bd4e1Sdrh UPDATE t1 SET spacer = CASE WHEN rowid&1 THEN 'x' ELSE 'xx' END; 557d9bd4e1Sdrh SELECT count(*) FROM t1; 567d9bd4e1Sdrh } 577d9bd4e1Sdrh} 66 587d9bd4e1Sdrhdo_test utf16align-1.1 { 597d9bd4e1Sdrh set unaligned_string_counter 607d9bd4e1Sdrh} 0 617d9bd4e1Sdrh 627d9bd4e1Sdrh# Creating an index that uses the unaligned collation. We should see 637d9bd4e1Sdrh# some unaligned strings passed to the collating function. 647d9bd4e1Sdrh# 657d9bd4e1Sdrhdo_test utf16align-1.2 { 667d9bd4e1Sdrh execsql { 677d9bd4e1Sdrh CREATE INDEX t1i1 ON t1(spacer, b); 687d9bd4e1Sdrh } 697d9bd4e1Sdrh # puts $unaligned_string_counter 707d9bd4e1Sdrh expr {$unaligned_string_counter>0} 717d9bd4e1Sdrh} 1 727d9bd4e1Sdrh 737d9bd4e1Sdrh# Create another index that uses the aligned collation. This time 747d9bd4e1Sdrh# there should be no unaligned accesses 757d9bd4e1Sdrh# 767d9bd4e1Sdrhdo_test utf16align-1.3 { 777d9bd4e1Sdrh set unaligned_string_counter 0 787d9bd4e1Sdrh execsql { 797d9bd4e1Sdrh CREATE INDEX t1i2 ON t1(spacer, a); 807d9bd4e1Sdrh } 817d9bd4e1Sdrh expr {$unaligned_string_counter>0} 827d9bd4e1Sdrh} 0 837d9bd4e1Sdrhintegrity_check utf16align-1.4 847d9bd4e1Sdrh 85bbf695d6Sdrh# ticket #3482 86bbf695d6Sdrh# 87bbf695d6Sdrhdb close 88bbf695d6Sdrhsqlite3 db :memory: 89bbf695d6Sdrhdo_test utf16align-2.1 { 90bbf695d6Sdrh db eval { 91bbf695d6Sdrh PRAGMA encoding=UTF16be; 92bbf695d6Sdrh SELECT hex(ltrim(x'6efcda')); 93bbf695d6Sdrh } 94bbf695d6Sdrh} {6EFC} 95bbf695d6Sdrh 967d9bd4e1Sdrhfinish_test 97