Example #1
0
void nmrSVDSolverTest::GenericTest(_matrixType & input) {
    typedef typename _matrixType::value_type value_type;
    
    /* Keep a copy of original since SVD modifies input */
    vctDynamicMatrix<value_type> inputCopy(input.rows(), input.cols(), input.IsRowMajor());
    inputCopy.Assign(input);
    
    /* Memory allocation */
    nmrSVDSolver SVDSolver(input);

    /* Solve */
    SVDSolver.Solve(input);

    /* Create a diagonal matrix from the singular values */
    vctDynamicMatrix<value_type> matrixS(input.rows(), input.cols(), input.IsRowMajor());
    matrixS.SetAll(0.0);
    matrixS.Diagonal().Assign(SVDSolver.GetS().Column(0));

    /* Recompose */
    vctDynamicMatrix<value_type> product(input.rows(), input.cols(), input.IsRowMajor());
    product = SVDSolver.GetU() * (matrixS * SVDSolver.GetVt());

    /* Compare initial with result */
    value_type error = (inputCopy - product).LinfNorm();
    CPPUNIT_ASSERT(error < cmnTypeTraits<value_type>::Tolerance());

    /* Make sure that both U and V are orthonormal */
    CPPUNIT_ASSERT(nmrIsOrthonormal(SVDSolver.GetU()));
    CPPUNIT_ASSERT(nmrIsOrthonormal(SVDSolver.GetVt()));
}
// verifies that the specified mb sequences decode to the specified wc sequence
void MBConvTestCase::TestDecoder(
    const wchar_t* wideBuffer,  // the same character sequence as multiBuffer, encoded as wchar_t
    size_t         wideChars,   // the number of wide characters at wideBuffer
    const char*    multiBuffer, // a multibyte encoded character sequence that can be decoded by "converter"
    size_t         multiBytes,  // the byte length of the multibyte character sequence that can be decoded by "converter"
    wxMBConv*      converter,   // the wxMBConv object that can decode multiBuffer into a wide character sequence
    int            sizeofNull   // number of bytes occupied by terminating null in this encoding
    )
{
    const unsigned UNINITIALIZED = 0xcd;

    // copy the input bytes into a null terminated buffer
    wxCharBuffer inputCopy( multiBytes+sizeofNull );
    memcpy( inputCopy.data(), multiBuffer, multiBytes );
    memset( &inputCopy.data()[multiBytes], 0, sizeofNull );

    // calculate the output size
    size_t outputWritten = converter->MB2WC
        ( 
        0, 
        (const char*)inputCopy.data(), 
        0
        );
    // make sure the correct output length was calculated
    CPPUNIT_ASSERT( outputWritten == wideChars );

    // convert the string
    size_t guardChars = 8; // to make sure we're not overrunning the output buffer
    size_t nullCharacters = 1;
    size_t outputBufferChars = outputWritten + nullCharacters + guardChars;
    wxWCharBuffer outputBuffer(outputBufferChars);
    memset( outputBuffer.data(), UNINITIALIZED, outputBufferChars*sizeof(wchar_t) );

    outputWritten = converter->MB2WC
        ( 
        outputBuffer.data(), 
        (const char*)inputCopy.data(), 
        outputBufferChars
        );
    // make sure the correct number of characters were outputs
    CPPUNIT_ASSERT( outputWritten == wideChars );

    // make sure the characters generated are correct
    CPPUNIT_ASSERT( 0 == memcmp( outputBuffer, wideBuffer, wideChars*sizeof(wchar_t) ) );

    // the output buffer should be null terminated
    CPPUNIT_ASSERT(  outputBuffer[outputWritten] == 0 );

    // make sure the rest of the output buffer is untouched
    for ( size_t i = (wideChars+1)*sizeof(wchar_t); i < (outputBufferChars*sizeof(wchar_t)); i++ )
    {
        CPPUNIT_ASSERT( ((unsigned char*)outputBuffer.data())[i] == UNINITIALIZED );
    }

#if wxUSE_UNICODE && wxUSE_STREAMS
    TestStreamDecoder( wideBuffer, wideChars, multiBuffer, multiBytes, converter );
#endif
}