Example #1
0
Collator::Result Collator::collate(const UChar* lhs, size_t lhsLength, const UChar* rhs, size_t rhsLength) const
{
    if (lhsLength == 0)
        if (rhsLength == 0)
            return Collator::Equal;
        else
            return Collator::Less;
    else if (rhsLength == 0)
        return Collator::Greater;

    if (!m_haveLCID) {
        m_collationLocale = getLCIDForLocaleName(m_locale);
        m_haveLCID = true;
    }

    Vector<unsigned char> lhsSortKey;
    createSortKey(m_collationLocale, true, lhs, lhsLength, lhsSortKey);

    Vector<unsigned char> rhsSortKey;
    createSortKey(m_collationLocale, true, rhs, rhsLength, rhsSortKey);

    Collator::Result const caseInsesitiveResult = compareByteVectors(lhsSortKey, rhsSortKey);
    if (caseInsesitiveResult == Collator::Equal) {
        createSortKey(m_haveLCID, false, lhs, lhsLength, lhsSortKey);
        createSortKey(m_haveLCID, false, rhs, rhsLength, rhsSortKey);
        Collator::Result const caseSensitiveResult = compareByteVectors(lhsSortKey, rhsSortKey);
        if (!m_lowerFirst)
            return caseSensitiveResult;
        if (caseSensitiveResult == Collator::Less)
            return Collator::Greater;
        else if (caseSensitiveResult == Collator::Greater)
            return Collator::Less;
        return Collator::Equal;
    }
    return caseInsesitiveResult;
}
/****************************************************************************
Desc:
****************************************************************************/
RCODE	SortKeyTestImpl::verifyQuery( 
	const char *		pszQuery, 
	char*					ppszNames[][ 2],
	IF_Query *			pQuery,
	FLMUINT				uiNumNames,
	FLMBOOL				bFirstAscending,
	FLMBOOL				bLastAscending,
	FLMBOOL				bDocsIndexed,
	FLMBOOL				bIxFirstAscending,
	FLMBOOL				bIxLastAscending)
{
	RCODE					rc = NE_XFLM_OK;
	IF_DOMNode *		pResultNode = NULL;
	IF_DOMNode *		pFirstNameNode = NULL;
	IF_DOMNode *		pLastNameNode = NULL;
	FLMUINT				uiLoop;
	FLMUINT				uiPos;
	char					szBuf[ 100];
	IF_DataVector *	pSearchKey = NULL;
	char					szTestName[ 200];
	static FLMUINT		uiCallCount = 0;
	const char *		pszTestNameFormat = 
		"Verify Query #%u "
		"(%s/First Name Asc == %u/Last Name Asc ==%u/%s)";


	uiCallCount++;

	f_sprintf( szTestName, pszTestNameFormat, uiCallCount,
		pszQuery, (unsigned)bFirstAscending, (unsigned)bLastAscending,
		"positioning");

	beginTest( szTestName, 
		"Ensure queries return proper results in proper order",
		"No Additional Info",
		"");

	// positioning tests

	if ( RC_BAD( rc = pQuery->setupQueryExpr( m_pDb, pszQuery)))
	{
		MAKE_FLM_ERROR_STRING( "setupQueryExpr failed.", m_szDetails, rc);
		goto Exit;
	}

	if ( bDocsIndexed && 
		(bFirstAscending == bIxFirstAscending && 
		bLastAscending == bIxLastAscending))
	{
		pQuery->setIndex( IX_NUM);
	}

	if ( RC_BAD( rc = pQuery->enablePositioning()))
	{
		MAKE_FLM_ERROR_STRING( "enablePositioning failed.", m_szDetails, rc);
		goto Exit;
	}

	if ( RC_BAD( rc = createSortKey( 
		pQuery, bFirstAscending, bLastAscending)))
	{
		goto Exit;
	}

	for( uiLoop = 0; uiLoop < uiNumNames; uiLoop++)
	{

		if ( RC_BAD( rc = pQuery->positionTo( 
			m_pDb, &pResultNode, 0, uiLoop)))
		{
			MAKE_FLM_ERROR_STRING( "positionTo failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pResultNode->getFirstChild( m_pDb, &pFirstNameNode)))
		{
			MAKE_FLM_ERROR_STRING( "getFirstChild failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pFirstNameNode->getNextSibling( m_pDb, &pLastNameNode)))
		{
			MAKE_FLM_ERROR_STRING( "getNextSibling failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pFirstNameNode->getUTF8( m_pDb, 
			(FLMBYTE *)szBuf, sizeof( szBuf), 0, sizeof( szBuf) - 1)))
		{
			MAKE_FLM_ERROR_STRING( "getUTF8 failed.", m_szDetails, rc);
			goto Exit;
		} 

		if ( f_strcmp( szBuf, ppszNames[uiLoop][0]) != 0)
		{
			rc = RC_SET( NE_XFLM_FAILURE);
			MAKE_FLM_ERROR_STRING( "Unexpected value.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pLastNameNode->getUTF8( m_pDb, 
			(FLMBYTE *)szBuf, sizeof( szBuf), 0, sizeof( szBuf) - 1)))
		{
			MAKE_FLM_ERROR_STRING( "getUTF8 failed.", m_szDetails, rc);
			goto Exit;
		} 

		if ( f_strcmp( szBuf, ppszNames[uiLoop][1]) != 0)
		{
			rc = RC_SET( NE_XFLM_FAILURE);
			MAKE_FLM_ERROR_STRING( "Unexpected value.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pQuery->getPosition( m_pDb, &uiPos)))
		{
			MAKE_FLM_ERROR_STRING( "getPosition failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( uiPos != uiLoop)
		{
			rc = RC_SET( NE_XFLM_FAILURE);
			MAKE_FLM_ERROR_STRING( "Unexpected position", m_szDetails, rc);
			goto Exit;
		}

	}

	endTest("PASS");

	f_sprintf( szTestName, pszTestNameFormat, uiCallCount,
		pszQuery, (unsigned)bFirstAscending, (unsigned)bLastAscending,
		"search key positioning");

	beginTest( szTestName, 
		"Ensure queries return proper results in proper order",
		"No Additional Info",
		"");
		
	if( RC_BAD( rc = m_pDbSystem->createIFDataVector( &pSearchKey)))
	{
		goto Exit;
	}

	// positioning tests 2

	for( uiLoop = 0; uiLoop < uiNumNames; uiLoop++)
	{

		if ( uiLoop == 0)
		{
			if ( RC_BAD( rc = pQuery->positionTo( 
				m_pDb, &pResultNode, 0, pSearchKey, XFLM_FIRST)))
			{
				MAKE_FLM_ERROR_STRING( "positionTo failed.", m_szDetails, rc);
				goto Exit;
			}
		}
		else
		{
			if ( RC_BAD( rc = pSearchKey->setUTF8( 0, 
				(FLMBYTE *)ppszNames[uiLoop][0])))
			{
				MAKE_FLM_ERROR_STRING( "setUTF8 failed.", m_szDetails, rc);
				goto Exit;
			}

			if ( RC_BAD( rc = pSearchKey->setUTF8( 1, 
				(FLMBYTE *)ppszNames[uiLoop][1])))
			{
				MAKE_FLM_ERROR_STRING( "setUTF8 failed.", m_szDetails, rc);
				goto Exit;
			}

			if ( RC_BAD( rc = pQuery->positionTo( 
				m_pDb, &pResultNode, 0, pSearchKey, XFLM_EXACT)))
			{
				MAKE_FLM_ERROR_STRING( "positionTo failed.", m_szDetails, rc);
				goto Exit;
			}
		}

		if ( RC_BAD( rc = pResultNode->getFirstChild( m_pDb, &pFirstNameNode)))
		{
			MAKE_FLM_ERROR_STRING( "getFirstChild failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pFirstNameNode->getNextSibling( m_pDb, &pLastNameNode)))
		{
			MAKE_FLM_ERROR_STRING( "getNextSibling failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pFirstNameNode->getUTF8( m_pDb,
			(FLMBYTE *)szBuf, sizeof( szBuf), 0, sizeof( szBuf) - 1)))
		{
			MAKE_FLM_ERROR_STRING( "getUTF8 failed.", m_szDetails, rc);
			goto Exit;
		} 

		if ( f_strcmp( szBuf, ppszNames[uiLoop][0]) != 0)
		{
			rc = RC_SET( NE_XFLM_FAILURE);
			MAKE_FLM_ERROR_STRING( "Unexpected value.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pLastNameNode->getUTF8( m_pDb, 
			(FLMBYTE *)szBuf, sizeof( szBuf), 0, sizeof( szBuf) - 1)))
		{
			MAKE_FLM_ERROR_STRING( "getUTF8 failed.", m_szDetails, rc);
			goto Exit;
		} 

		if ( f_strcmp( szBuf, ppszNames[uiLoop][1]) != 0)
		{
			rc = RC_SET( NE_XFLM_FAILURE);
			MAKE_FLM_ERROR_STRING( "Unexpected value.", m_szDetails, rc);
			goto Exit;
		}

		if ( RC_BAD( rc = pQuery->getPosition( m_pDb, &uiPos)))
		{
			MAKE_FLM_ERROR_STRING( "getPosition failed.", m_szDetails, rc);
			goto Exit;
		}

		if ( uiPos != uiLoop)
		{
			rc = RC_SET( NE_XFLM_FAILURE);
			MAKE_FLM_ERROR_STRING( "Unexpected position", m_szDetails, rc);
			goto Exit;
		}
	}

	endTest("PASS");

Exit:

	if( RC_BAD( rc))
	{
		endTest("FAIL");
	}

	if( pResultNode)
	{
		pResultNode->Release();
	}

	if( pFirstNameNode)
	{
		pFirstNameNode->Release();
	}

	if( pLastNameNode)
	{
		pLastNameNode->Release();
	}
	
	if( pSearchKey)
	{
		pSearchKey->Release();
	}

	return rc;
}