phonebookengines/contactsmodel/tsrc/cntplsql/src/t_cpplpredictivesearchtable.cpp
changeset 31 2a11b5b00470
parent 27 de1630741fbe
child 46 efe85016a067
equal deleted inserted replaced
27:de1630741fbe 31:2a11b5b00470
    17 
    17 
    18 //  INTERNAL INCLUDES
    18 //  INTERNAL INCLUDES
    19 #include "t_cpplpredictivesearchtable.h"
    19 #include "t_cpplpredictivesearchtable.h"
    20 #include "pltables.h"
    20 #include "pltables.h"
    21 #include "dbsqlconstants.h"
    21 #include "dbsqlconstants.h"
       
    22 #include "cpcskeymap.h" // USE_ORBIT_KEYMAP macro
    22 
    23 
    23 //  SYSTEM INCLUDES
    24 //  SYSTEM INCLUDES
    24 #include <digia/eunit/eunitmacros.h>
    25 #include <digia/eunit/eunitmacros.h>
    25 #include <f32file.h> // added for setup2L() 
    26 #include <f32file.h> // added for setup2L() 
    26 
    27 
    42 _LIT(KTestFirstName, "123");
    43 _LIT(KTestFirstName, "123");
    43 _LIT(KTestLastName, "45678");
    44 _LIT(KTestLastName, "45678");
    44 
    45 
    45 // Must have same value as KMaxDigits in cpplpredictivesearchtable.cpp
    46 // Must have same value as KMaxDigits in cpplpredictivesearchtable.cpp
    46 const TInt KMaxDigits = 15;
    47 const TInt KMaxDigits = 15;
       
    48 
       
    49 
       
    50 const TInt KTableCount = 12;
    47 
    51 
    48 // Must have same value as KConversionError in cpplpredictivesearchtable.cpp
    52 // Must have same value as KConversionError in cpplpredictivesearchtable.cpp
    49 const quint64 KConversionError = 0xeeeeeeeeeeeeeee;
    53 const quint64 KConversionError = 0xeeeeeeeeeeeeeee;
    50     
    54     
    51 
    55 
   785     }
   789     }
   786 
   790 
   787 void UT_CPplPredictiveSearchTable::UT_ConvertToHexL()
   791 void UT_CPplPredictiveSearchTable::UT_ConvertToHexL()
   788 	{
   792 	{
   789 	// Basic case
   793 	// Basic case
   790 	EUNIT_ASSERT_EQUALS(0x4458aaaaaaaaaaa, iTable->ConvertToHex("4458"));
   794 	EUNIT_ASSERT_EQUALS(0x4458fffffffffff, iTable->ConvertToHex("4458"));
       
   795 	
       
   796 	// Long digit
       
   797 	EUNIT_ASSERT_EQUALS(0x123456789012345, iTable->ConvertToHex("123456789012345"));
   791 
   798 
   792 	// Trailing zeros
   799 	// Trailing zeros
   793 	EUNIT_ASSERT_EQUALS(0x12345678900aaaa, iTable->ConvertToHex("12345678900"));
   800 	EUNIT_ASSERT_EQUALS(0x12345678900ffff, iTable->ConvertToHex("12345678900"));
   794 
   801 
   795 	// Leading zeros
   802 	// Leading zeros
   796 	EUNIT_ASSERT_EQUALS(0x00123456789aaaa, iTable->ConvertToHex("00123456789"));
   803 	EUNIT_ASSERT_EQUALS(0x00123456789ffff, iTable->ConvertToHex("00123456789"));
   797 
   804 
   798 	// Just zeros
   805 	// Just zeros
   799 	EUNIT_ASSERT_EQUALS(0x00000aaaaaaaaaa, iTable->ConvertToHex("00000"));
   806 	EUNIT_ASSERT_EQUALS(0x00000ffffffffff, iTable->ConvertToHex("00000"));
   800 	EUNIT_ASSERT_EQUALS(0x00000000000000a, iTable->ConvertToHex("00000000000000"));
   807 	EUNIT_ASSERT_EQUALS(0x00000000000000f, iTable->ConvertToHex("00000000000000"));
   801 	EUNIT_ASSERT_EQUALS(0x000000000000000, iTable->ConvertToHex("000000000000000"));
   808 	EUNIT_ASSERT_EQUALS(0x000000000000000, iTable->ConvertToHex("000000000000000"));
   802 	EUNIT_ASSERT_EQUALS(0x0aaaaaaaaaaaaaa, iTable->ConvertToHex("0"));
   809 	EUNIT_ASSERT_EQUALS(0x0ffffffffffffff, iTable->ConvertToHex("0"));
   803 
   810 
   804 	// Zeros in the middle
   811 	// Zeros in the middle
   805 	EUNIT_ASSERT_EQUALS(0x12300450008000a, iTable->ConvertToHex("12300450008000"));
   812 	EUNIT_ASSERT_EQUALS(0x12300450008000f, iTable->ConvertToHex("12300450008000"));
   806 
   813 
   807 	// Empty string
   814 	// Empty string
   808 	EUNIT_ASSERT_EQUALS(0xaaaaaaaaaaaaaaa, iTable->ConvertToHex(""));
   815 	EUNIT_ASSERT_EQUALS(0xfffffffffffffff, iTable->ConvertToHex(""));
   809 	
   816 	
   810 	// Unmapped characters
   817 	// Unmapped characters
   811 	EUNIT_ASSERT_EQUALS(0x123aaaaaaaaaaaa, iTable->ConvertToHex("123??45??67"));
   818 	EUNIT_ASSERT_EQUALS(0x123ffffffffffff, iTable->ConvertToHex("123??45??67"));
   812 	EUNIT_ASSERT_EQUALS(0x00aaaaaaaaaaaaa, iTable->ConvertToHex("00?1234567"));
   819 	EUNIT_ASSERT_EQUALS(0x00fffffffffffff, iTable->ConvertToHex("00?1234567"));
   813 	EUNIT_ASSERT_EQUALS(0xaaaaaaaaaaaaaaa, iTable->ConvertToHex("?1234567"));
   820 	EUNIT_ASSERT_EQUALS(0xfffffffffffffff, iTable->ConvertToHex("?1234567"));
   814 	EUNIT_ASSERT_EQUALS(0xaaaaaaaaaaaaaaa, iTable->ConvertToHex("???"));
   821 	EUNIT_ASSERT_EQUALS(0xfffffffffffffff, iTable->ConvertToHex("???"));
   815 	
   822 	
   816 	// Too many digits
   823 	// Too many digits
   817 	EUNIT_ASSERT_EQUALS(KConversionError, iTable->ConvertToHex("12345678901234567890"));
   824 	EUNIT_ASSERT_EQUALS(KConversionError, iTable->ConvertToHex("12345678901234567890"));
   818 	}
   825 	}
   819 
   826 
   862     TInt aCountInTable4,
   869     TInt aCountInTable4,
   863     TInt aCountInTable5,
   870     TInt aCountInTable5,
   864     TInt aCountInTable6,
   871     TInt aCountInTable6,
   865     TInt aCountInTable7,
   872     TInt aCountInTable7,
   866     TInt aCountInTable8,
   873     TInt aCountInTable8,
   867     TInt aCountInTable9)
   874     TInt aCountInTable9,
   868     {
   875     TInt aCountInTable10,
   869     const TInt KTableCount = 10;
   876     TInt aCountInTable11)
       
   877     {
   870     TPtrC tableNames[KTableCount] =
   878     TPtrC tableNames[KTableCount] =
   871         {
   879         {
   872         KSqlContactPredSearchTable0,
   880         KSqlContactPredSearchTable0,
   873         KSqlContactPredSearchTable1,
   881         KSqlContactPredSearchTable1,
   874         KSqlContactPredSearchTable2,
   882         KSqlContactPredSearchTable2,
   876         KSqlContactPredSearchTable4,
   884         KSqlContactPredSearchTable4,
   877         KSqlContactPredSearchTable5,
   885         KSqlContactPredSearchTable5,
   878         KSqlContactPredSearchTable6,
   886         KSqlContactPredSearchTable6,
   879         KSqlContactPredSearchTable7,
   887         KSqlContactPredSearchTable7,
   880         KSqlContactPredSearchTable8,
   888         KSqlContactPredSearchTable8,
   881         KSqlContactPredSearchTable9
   889         KSqlContactPredSearchTable9,
       
   890         KSqlContactPredSearchTable10,
       
   891         KSqlContactPredSearchTable11
   882         };
   892         };
   883     TInt rowCounts[KTableCount] = {0};
   893     TInt rowCounts[KTableCount] = {0};
   884     
   894     
   885     for (TInt i = 0; i < KTableCount; ++i)
   895     for (TInt i = 0; i < KTableCount; ++i)
   886         {
   896         {
   887         HBufC* s = HBufC::NewLC(KCountSelect().Length() +
   897         HBufC* s = HBufC::NewLC(KCountSelect().Length() +
   888             // All table names are same length
   898             // Enough space for longest table name
   889             KSqlContactPredSearchTable0().Length());
   899             KSqlContactPredSearchTable11().Length());
   890         TPtr ptr = s->Des();
   900         TPtr ptr = s->Des();
   891         ptr.Format(KCountSelect, &tableNames[i]);
   901         ptr.Format(KCountSelect, &tableNames[i]);
   892 
   902 
   893         TSqlScalarFullSelectQuery scalarQuery(iDB);
   903         TSqlScalarFullSelectQuery scalarQuery(iDB);
   894     
   904     
   904     EUNIT_ASSERT_EQUALS(aCountInTable5, rowCounts[5]);
   914     EUNIT_ASSERT_EQUALS(aCountInTable5, rowCounts[5]);
   905     EUNIT_ASSERT_EQUALS(aCountInTable6, rowCounts[6]);
   915     EUNIT_ASSERT_EQUALS(aCountInTable6, rowCounts[6]);
   906     EUNIT_ASSERT_EQUALS(aCountInTable7, rowCounts[7]);
   916     EUNIT_ASSERT_EQUALS(aCountInTable7, rowCounts[7]);
   907     EUNIT_ASSERT_EQUALS(aCountInTable8, rowCounts[8]);
   917     EUNIT_ASSERT_EQUALS(aCountInTable8, rowCounts[8]);
   908     EUNIT_ASSERT_EQUALS(aCountInTable9, rowCounts[9]);
   918     EUNIT_ASSERT_EQUALS(aCountInTable9, rowCounts[9]);
       
   919     EUNIT_ASSERT_EQUALS(aCountInTable10, rowCounts[10]);
       
   920     EUNIT_ASSERT_EQUALS(aCountInTable11, rowCounts[11]);
   909     }
   921     }
   910 
   922 
   911 // There is only need to search from one table (unless search string begins
   923 // There is only need to search from one table (unless search string begins
   912 // with a zero).
   924 // with a zero).
   913 // E.g. if search string is "102", the results are both in tables 1 and 2
   925 // E.g. if search string is "102", the results are both in tables 1 and 2
   928 	if (aSearchString.Length() == 1)
   940 	if (aSearchString.Length() == 1)
   929 		{
   941 		{
   930 		// No need to use "WHERE.." or "ORDER BY first_name ASC"
   942 		// No need to use "WHERE.." or "ORDER BY first_name ASC"
   931 		_LIT(KSearchOneDigitFormat, "SELECT contact_id FROM %S");
   943 		_LIT(KSearchOneDigitFormat, "SELECT contact_id FROM %S");
   932 		select = HBufC::NewLC(KSearchOneDigitFormat().Length() +
   944 		select = HBufC::NewLC(KSearchOneDigitFormat().Length() +
   933 							  KSqlContactPredSearchTable0().Length());
   945 							  KSqlContactPredSearchTable11().Length());
   934 		select->Des().AppendFormat(KSearchOneDigitFormat, &tableName);
   946 		select->Des().AppendFormat(KSearchOneDigitFormat, &tableName);
   935 		}
   947 		}
   936 	else
   948 	else
   937 		{
   949 		{
   938 		// This does not yet support zero in the middle
   950 		// This does not yet support zero in the middle
   942 		_LIT(KSearchFormat, "SELECT contact_id FROM %S WHERE \
   954 		_LIT(KSearchFormat, "SELECT contact_id FROM %S WHERE \
   943 (nbr>%ld AND nbr<%ld) OR (nbr2>%ld AND nbr2<%ld) OR (nbr3>%ld AND nbr3<%ld) OR (nbr4>%ld AND nbr4<%ld);");
   955 (nbr>%ld AND nbr<%ld) OR (nbr2>%ld AND nbr2<%ld) OR (nbr3>%ld AND nbr3<%ld) OR (nbr4>%ld AND nbr4<%ld);");
   944 		TInt KNbrColumns = 4;
   956 		TInt KNbrColumns = 4;
   945 		TInt KSpaceForLimits = KNbrColumns * 2 * (KMaxDigits + 2); // Two extra for decimal representation of max 15 hex digits
   957 		TInt KSpaceForLimits = KNbrColumns * 2 * (KMaxDigits + 2); // Two extra for decimal representation of max 15 hex digits
   946 		select = HBufC::NewLC(KSearchFormat().Length() +
   958 		select = HBufC::NewLC(KSearchFormat().Length() +
   947 							  KSqlContactPredSearchTable0().Length() +
   959 							  KSqlContactPredSearchTable11().Length() +
   948 							  KSpaceForLimits);
   960 							  KSpaceForLimits);
   949 		select->Des().AppendFormat(KSearchFormat, &tableName,
   961 		select->Des().AppendFormat(KSearchFormat, &tableName,
   950 								   lowerLimit, upperLimit,
   962 								   lowerLimit, upperLimit,
   951 								   lowerLimit, upperLimit,
   963 								   lowerLimit, upperLimit,
   952 								   lowerLimit, upperLimit,
   964 								   lowerLimit, upperLimit,
   996 			return KSqlContactPredSearchTable7;	
  1008 			return KSqlContactPredSearchTable7;	
   997 		case '8':
  1009 		case '8':
   998 			return KSqlContactPredSearchTable8;
  1010 			return KSqlContactPredSearchTable8;
   999 		case '9':
  1011 		case '9':
  1000 			return KSqlContactPredSearchTable9;
  1012 			return KSqlContactPredSearchTable9;
       
  1013 		case 'a':
       
  1014 		    return KSqlContactPredSearchTable10;
       
  1015 		case 'b':
       
  1016 		    return KSqlContactPredSearchTable11;
  1001 		default:
  1017 		default:
  1002 			return KNullDesC;
  1018 			return KNullDesC;
  1003 		}
  1019 		}
  1004 	}
  1020 	}
  1005 
  1021 
  1008 	return ConvertToNbrL(aString, '0');
  1024 	return ConvertToNbrL(aString, '0');
  1009 	}
  1025 	}
  1010 
  1026 
  1011 TInt64 UT_CPplPredictiveSearchTable::UpperLimitL(const TDesC& aString) const
  1027 TInt64 UT_CPplPredictiveSearchTable::UpperLimitL(const TDesC& aString) const
  1012 	{
  1028 	{
  1013 	return ConvertToNbrL(aString, 'a');
  1029 	return ConvertToNbrL(aString, 'f');
  1014 	}
  1030 	}
  1015 
  1031 
  1016 TInt64 UT_CPplPredictiveSearchTable::ConvertToNbrL(const TDesC& aString,
  1032 TInt64 UT_CPplPredictiveSearchTable::ConvertToNbrL(const TDesC& aString,
  1017 												   TChar aPadChar) const
  1033 												   TChar aPadChar) const
  1018 	{
  1034 	{
  1042 	// SQLite does not have SHOW TABLES command, so try to search from
  1058 	// SQLite does not have SHOW TABLES command, so try to search from
  1043     // predictive search table.
  1059     // predictive search table.
  1044     _LIT(KCheckIfTableExistsFormat, "SELECT %S FROM %S;");
  1060     _LIT(KCheckIfTableExistsFormat, "SELECT %S FROM %S;");
  1045     TInt bufSize = KCheckIfTableExistsFormat().Length() +
  1061     TInt bufSize = KCheckIfTableExistsFormat().Length() +
  1046                    KPredSearchContactId().Length() +
  1062                    KPredSearchContactId().Length() +
  1047                    KSqlContactPredSearchTable0().Length();
  1063                    KSqlContactPredSearchTable11().Length();
  1048     HBufC* sqlStatement = HBufC::NewLC(bufSize);
  1064     HBufC* sqlStatement = HBufC::NewLC(bufSize);
  1049     sqlStatement->Des().AppendFormat(KCheckIfTableExistsFormat,
  1065     sqlStatement->Des().AppendFormat(KCheckIfTableExistsFormat,
  1050         &KPredSearchContactId,
  1066         &KPredSearchContactId,
  1051         &KSqlContactPredSearchTable0);
  1067         &KSqlContactPredSearchTable0);
  1052 
  1068 
  1061 	}
  1077 	}
  1062 
  1078 
  1063 // This function has code copied from CPplContactItemManager::DeletePredSearchTablesL
  1079 // This function has code copied from CPplContactItemManager::DeletePredSearchTablesL
  1064 void UT_CPplPredictiveSearchTable::CPplContactItemManager_DeletePredSearchTablesL()
  1080 void UT_CPplPredictiveSearchTable::CPplContactItemManager_DeletePredSearchTablesL()
  1065 	{
  1081 	{
  1066 	const TInt KTableCount = 10;
       
  1067     const TDesC* KTableNames[KTableCount] =
  1082     const TDesC* KTableNames[KTableCount] =
  1068         {
  1083         {
  1069         &KSqlContactPredSearchTable0,
  1084         &KSqlContactPredSearchTable0,
  1070         &KSqlContactPredSearchTable1,
  1085         &KSqlContactPredSearchTable1,
  1071         &KSqlContactPredSearchTable2,
  1086         &KSqlContactPredSearchTable2,
  1073         &KSqlContactPredSearchTable4,
  1088         &KSqlContactPredSearchTable4,
  1074         &KSqlContactPredSearchTable5,
  1089         &KSqlContactPredSearchTable5,
  1075         &KSqlContactPredSearchTable6,
  1090         &KSqlContactPredSearchTable6,
  1076         &KSqlContactPredSearchTable7,
  1091         &KSqlContactPredSearchTable7,
  1077         &KSqlContactPredSearchTable8,
  1092         &KSqlContactPredSearchTable8,
  1078         &KSqlContactPredSearchTable9
  1093         &KSqlContactPredSearchTable9,
       
  1094         &KSqlContactPredSearchTable10,
       
  1095         &KSqlContactPredSearchTable11
  1079         };
  1096         };
  1080 
  1097 
  1081 	// IF EXISTS suppresses error that would occur if table does not exist
  1098 	// IF EXISTS suppresses error that would occur if table does not exist
  1082 	_LIT(KDropTable, "DROP TABLE IF EXISTS %S;");
  1099 	_LIT(KDropTable, "DROP TABLE IF EXISTS %S;");
  1083     for (TInt i = 0; i < KTableCount; ++i)
  1100     for (TInt i = 0; i < KTableCount; ++i)