From 2d9c0c85925353890f21990274d9187f0a6ef33a Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Mon, 11 Nov 2024 13:06:37 +0300 Subject: [PATCH] C-API cleanup: core module tests --- modules/core/include/opencv2/core/core_c.h | 2 - modules/core/include/opencv2/core/cvdef.h | 2 + modules/core/perf/perf_reduce.cpp | 1 - modules/core/test/test_allocator.cpp | 1 - modules/core/test/test_arithm.cpp | 2 - modules/core/test/test_ds.cpp | 2141 -------------------- modules/core/test/test_dxt.cpp | 39 +- modules/core/test/test_io.cpp | 8 +- modules/core/test/test_mat.cpp | 124 +- modules/core/test/test_math.cpp | 116 +- modules/core/test/test_umat.cpp | 1 - modules/imgproc/test/test_pc.cpp | 10 +- 12 files changed, 73 insertions(+), 2374 deletions(-) delete mode 100644 modules/core/test/test_ds.cpp diff --git a/modules/core/include/opencv2/core/core_c.h b/modules/core/include/opencv2/core/core_c.h index fd72a2cf3e..50625b34d3 100644 --- a/modules/core/include/opencv2/core/core_c.h +++ b/modules/core/include/opencv2/core/core_c.h @@ -220,8 +220,6 @@ allocated using cvCreateData or set explicitly to user-allocated data via cvSetD */ CVAPI(CvMat*) cvCreateMatHeader( int rows, int cols, int type ); -#define CV_AUTOSTEP 0x7fffffff - /** @brief Initializes a pre-allocated matrix header. This function is often used to process raw data with OpenCV matrix functions. For example, the diff --git a/modules/core/include/opencv2/core/cvdef.h b/modules/core/include/opencv2/core/cvdef.h index 96603e4ca5..3d2a4c28b1 100644 --- a/modules/core/include/opencv2/core/cvdef.h +++ b/modules/core/include/opencv2/core/cvdef.h @@ -521,6 +521,8 @@ Cv64suf; #define CV_ELEM_SIZE(type) (CV_MAT_CN(type)*CV_ELEM_SIZE1(type)) +#define CV_AUTOSTEP 0x7fffffff + #ifndef MIN # define MIN(a,b) ((a) > (b) ? (b) : (a)) #endif diff --git a/modules/core/perf/perf_reduce.cpp b/modules/core/perf/perf_reduce.cpp index 844303aa7d..bcd10b18c5 100644 --- a/modules/core/perf/perf_reduce.cpp +++ b/modules/core/perf/perf_reduce.cpp @@ -1,5 +1,4 @@ #include "perf_precomp.hpp" -#include "opencv2/core/core_c.h" namespace opencv_test { diff --git a/modules/core/test/test_allocator.cpp b/modules/core/test/test_allocator.cpp index 18a9d3781a..88b03b689e 100644 --- a/modules/core/test/test_allocator.cpp +++ b/modules/core/test/test_allocator.cpp @@ -2,7 +2,6 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" -#include "opencv2/core/core_c.h" // needed for CV_AUTOSTEP namespace opencv_test { namespace { diff --git a/modules/core/test/test_arithm.cpp b/modules/core/test/test_arithm.cpp index 560a237634..e998db494b 100644 --- a/modules/core/test/test_arithm.cpp +++ b/modules/core/test/test_arithm.cpp @@ -3,8 +3,6 @@ // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" #include "ref_reduce_arg.impl.hpp" -#include "opencv2/core/core_c.h" - #include namespace opencv_test { namespace { diff --git a/modules/core/test/test_ds.cpp b/modules/core/test/test_ds.cpp deleted file mode 100644 index 9824b520ab..0000000000 --- a/modules/core/test/test_ds.cpp +++ /dev/null @@ -1,2141 +0,0 @@ -// This file is part of OpenCV project. -// It is subject to the license terms in the LICENSE file found in the top-level directory -// of this distribution and at http://opencv.org/license.html. -#include "test_precomp.hpp" -#include "opencv2/core/core_c.h" - -namespace opencv_test { namespace { - -typedef struct CvTsSimpleSeq -{ - schar* array; - int count; - int max_count; - int elem_size; -} CvTsSimpleSeq; - - -static CvTsSimpleSeq* cvTsCreateSimpleSeq( int max_count, int elem_size ) -{ - CvTsSimpleSeq* seq = (CvTsSimpleSeq*)cvAlloc( sizeof(*seq) + max_count * elem_size ); - seq->elem_size = elem_size; - seq->max_count = max_count; - seq->count = 0; - seq->array = (schar*)(seq + 1); - return seq; -} - - -static void cvTsReleaseSimpleSeq( CvTsSimpleSeq** seq ) -{ - cvFree( seq ); -} - - -static schar* cvTsSimpleSeqElem( CvTsSimpleSeq* seq, int index ) -{ - CV_Assert( 0 <= index && index < seq->count ); - return seq->array + index * seq->elem_size; -} - - -static void cvTsClearSimpleSeq( CvTsSimpleSeq* seq ) -{ - seq->count = 0; -} - - -static void cvTsSimpleSeqShiftAndCopy( CvTsSimpleSeq* seq, int from_idx, int to_idx, void* elem=0 ) -{ - int elem_size = seq->elem_size; - - if( from_idx == to_idx ) - return; - - if (elem) - CV_Assert(from_idx < to_idx); - else - CV_Assert(from_idx > to_idx); - - if( from_idx < seq->count ) - { - memmove( seq->array + to_idx*elem_size, seq->array + from_idx*elem_size, - (seq->count - from_idx)*elem_size ); - } - seq->count += to_idx - from_idx; - if( elem ) - memcpy( seq->array + from_idx*elem_size, elem, (to_idx - from_idx)*elem_size ); -} - -static void cvTsSimpleSeqInvert( CvTsSimpleSeq* seq ) -{ - int i, k, len = seq->count, elem_size = seq->elem_size; - schar *data = seq->array, t; - - for( i = 0; i < len/2; i++ ) - { - schar* a = data + i*elem_size; - schar* b = data + (len - i - 1)*elem_size; - for( k = 0; k < elem_size; k++ ) - CV_SWAP( a[k], b[k], t ); - } -} - -/****************************************************************************************\ - * simple cvset implementation * - \****************************************************************************************/ - -typedef struct CvTsSimpleSet -{ - schar* array; - int count, max_count; - int elem_size; - int* free_stack; - int free_count; -} CvTsSimpleSet; - - -static void cvTsClearSimpleSet( CvTsSimpleSet* set_header ) -{ - int i; - int elem_size = set_header->elem_size; - - for( i = 0; i < set_header->max_count; i++ ) - { - set_header->array[i*elem_size] = 0; - set_header->free_stack[i] = set_header->max_count - i - 1; - } - set_header->free_count = set_header->max_count; - set_header->count = 0; -} - - -static CvTsSimpleSet* cvTsCreateSimpleSet( int max_count, int elem_size ) -{ - CvTsSimpleSet* set_header = (CvTsSimpleSet*)cvAlloc( sizeof(*set_header) + max_count * - (elem_size + 1 + sizeof(int))); - set_header->elem_size = elem_size + 1; - set_header->max_count = max_count; - set_header->free_stack = (int*)(set_header + 1); - set_header->array = (schar*)(set_header->free_stack + max_count); - - cvTsClearSimpleSet( set_header ); - return set_header; -} - - -static void cvTsReleaseSimpleSet( CvTsSimpleSet** set_header ) -{ - cvFree( set_header ); -} - - -static schar* cvTsSimpleSetFind( CvTsSimpleSet* set_header, int index ) -{ - int idx = index * set_header->elem_size; - CV_Assert( 0 <= index && index < set_header->max_count ); - return set_header->array[idx] ? set_header->array + idx + 1 : 0; -} - - -static int cvTsSimpleSetAdd( CvTsSimpleSet* set_header, void* elem ) -{ - int idx, idx2; - CV_Assert( set_header->free_count > 0 ); - - idx = set_header->free_stack[--set_header->free_count]; - idx2 = idx * set_header->elem_size; - CV_Assert( set_header->array[idx2] == 0 ); - set_header->array[idx2] = 1; - if( set_header->elem_size > 1 ) - memcpy( set_header->array + idx2 + 1, elem, set_header->elem_size - 1 ); - set_header->count = MAX( set_header->count, idx + 1 ); - - return idx; -} - - -static void cvTsSimpleSetRemove( CvTsSimpleSet* set_header, int index ) -{ - CV_Assert( set_header->free_count < set_header->max_count && - 0 <= index && index < set_header->max_count ); - CV_Assert( set_header->array[index * set_header->elem_size] == 1 ); - - set_header->free_stack[set_header->free_count++] = index; - set_header->array[index * set_header->elem_size] = 0; -} - - -/****************************************************************************************\ - * simple graph implementation * - \****************************************************************************************/ - -typedef struct CvTsSimpleGraph -{ - char* matrix; - int edge_size; - int oriented; - CvTsSimpleSet* vtx; -} CvTsSimpleGraph; - - -static void cvTsClearSimpleGraph( CvTsSimpleGraph* graph ) -{ - int max_vtx_count = graph->vtx->max_count; - cvTsClearSimpleSet( graph->vtx ); - memset( graph->matrix, 0, max_vtx_count * max_vtx_count * graph->edge_size ); -} - - -static CvTsSimpleGraph* cvTsCreateSimpleGraph( int max_vtx_count, int vtx_size, - int edge_size, int oriented ) -{ - CvTsSimpleGraph* graph; - - CV_Assert( max_vtx_count > 1 && vtx_size >= 0 && edge_size >= 0 ); - graph = (CvTsSimpleGraph*)cvAlloc( sizeof(*graph) + - max_vtx_count * max_vtx_count * (edge_size + 1)); - graph->vtx = cvTsCreateSimpleSet( max_vtx_count, vtx_size ); - graph->edge_size = edge_size + 1; - graph->matrix = (char*)(graph + 1); - graph->oriented = oriented; - - cvTsClearSimpleGraph( graph ); - return graph; -} - - -static void cvTsReleaseSimpleGraph( CvTsSimpleGraph** graph ) -{ - if( *graph ) - { - cvTsReleaseSimpleSet( &(graph[0]->vtx) ); - cvFree( graph ); - } -} - - -static int cvTsSimpleGraphAddVertex( CvTsSimpleGraph* graph, void* vertex ) -{ - return cvTsSimpleSetAdd( graph->vtx, vertex ); -} - - -static void cvTsSimpleGraphRemoveVertex( CvTsSimpleGraph* graph, int index ) -{ - int i, max_vtx_count = graph->vtx->max_count; - int edge_size = graph->edge_size; - cvTsSimpleSetRemove( graph->vtx, index ); - - /* remove all the corresponding edges */ - for( i = 0; i < max_vtx_count; i++ ) - { - graph->matrix[(i*max_vtx_count + index)*edge_size] = - graph->matrix[(index*max_vtx_count + i)*edge_size] = 0; - } -} - - -static void cvTsSimpleGraphAddEdge( CvTsSimpleGraph* graph, int idx1, int idx2, void* edge ) -{ - int i, t, n = graph->oriented ? 1 : 2; - - CV_Assert( cvTsSimpleSetFind( graph->vtx, idx1 ) && - cvTsSimpleSetFind( graph->vtx, idx2 )); - - for( i = 0; i < n; i++ ) - { - int ofs = (idx1*graph->vtx->max_count + idx2)*graph->edge_size; - CV_Assert( graph->matrix[ofs] == 0 ); - graph->matrix[ofs] = 1; - if( graph->edge_size > 1 ) - memcpy( graph->matrix + ofs + 1, edge, graph->edge_size - 1 ); - - CV_SWAP( idx1, idx2, t ); - } -} - - -static void cvTsSimpleGraphRemoveEdge( CvTsSimpleGraph* graph, int idx1, int idx2 ) -{ - int i, t, n = graph->oriented ? 1 : 2; - - CV_Assert( cvTsSimpleSetFind( graph->vtx, idx1 ) && - cvTsSimpleSetFind( graph->vtx, idx2 )); - - for( i = 0; i < n; i++ ) - { - int ofs = (idx1*graph->vtx->max_count + idx2)*graph->edge_size; - CV_Assert( graph->matrix[ofs] == 1 ); - graph->matrix[ofs] = 0; - CV_SWAP( idx1, idx2, t ); - } -} - - -static schar* cvTsSimpleGraphFindVertex( CvTsSimpleGraph* graph, int index ) -{ - return cvTsSimpleSetFind( graph->vtx, index ); -} - - -static char* cvTsSimpleGraphFindEdge( CvTsSimpleGraph* graph, int idx1, int idx2 ) -{ - if( cvTsSimpleGraphFindVertex( graph, idx1 ) && - cvTsSimpleGraphFindVertex( graph, idx2 )) - { - char* edge = graph->matrix + (idx1 * graph->vtx->max_count + idx2)*graph->edge_size; - if( edge[0] ) return edge + 1; - } - return 0; -} - - -static int cvTsSimpleGraphVertexDegree( CvTsSimpleGraph* graph, int index ) -{ - int i, count = 0; - int edge_size = graph->edge_size; - int max_vtx_count = graph->vtx->max_count; - CV_Assert( cvTsSimpleGraphFindVertex( graph, index ) != 0 ); - - for( i = 0; i < max_vtx_count; i++ ) - { - count += graph->matrix[(i*max_vtx_count + index)*edge_size] + - graph->matrix[(index*max_vtx_count + i)*edge_size]; - } - - if( !graph->oriented ) - { - CV_Assert( count % 2 == 0 ); - count /= 2; - } - return count; -} - - -///////////////////////////////////// the tests ////////////////////////////////// - -#define CV_TS_SEQ_CHECK_CONDITION( expr, err_msg ) \ -if( !(expr) ) \ -{ \ -set_error_context( #expr, err_msg, __FILE__, __LINE__ ); \ -ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );\ -throw -1; \ -} - -class Core_DynStructBaseTest : public cvtest::BaseTest -{ -public: - Core_DynStructBaseTest(); - virtual ~Core_DynStructBaseTest(); - bool can_do_fast_forward(); - void clear(); - -protected: - int read_params( const cv::FileStorage& fs ); - void run_func(void); - void set_error_context( const char* condition, - const char* err_msg, - const char* file, int line ); - int test_seq_block_consistence( int _struct_idx, CvSeq* seq, int total ); - void update_progressbar(); - - int struct_count, max_struct_size, iterations, generations; - int min_log_storage_block_size, max_log_storage_block_size; - int min_log_elem_size, max_log_elem_size; - int gen, struct_idx, iter; - int test_progress; - int64 start_time; - double cpu_freq; - vector cxcore_struct; - vector simple_struct; - Ptr storage; -}; - - -Core_DynStructBaseTest::Core_DynStructBaseTest() -{ - struct_count = 2; - max_struct_size = 2000; - min_log_storage_block_size = 7; - max_log_storage_block_size = 12; - min_log_elem_size = 0; - max_log_elem_size = 8; - generations = 10; - iterations = max_struct_size*2; - gen = struct_idx = iter = -1; - test_progress = -1; -} - - -Core_DynStructBaseTest::~Core_DynStructBaseTest() -{ - clear(); -} - - -void Core_DynStructBaseTest::run_func() -{ -} - -bool Core_DynStructBaseTest::can_do_fast_forward() -{ - return false; -} - - -void Core_DynStructBaseTest::clear() -{ - cvtest::BaseTest::clear(); -} - - -int Core_DynStructBaseTest::read_params( const cv::FileStorage& fs ) -{ - int code = cvtest::BaseTest::read_params( fs ); - double sqrt_scale = sqrt(ts->get_test_case_count_scale()); - if( code < 0 ) - return code; - - read( find_param( fs, "struct_count" ), struct_count, struct_count ); - read( find_param( fs, "max_struct_size" ), max_struct_size, max_struct_size ); - read( find_param( fs, "generations" ), generations, generations ); - read( find_param( fs, "iterations" ), iterations, iterations ); - generations = cvRound(generations*sqrt_scale); - iterations = cvRound(iterations*sqrt_scale); - - read( find_param( fs, "min_log_storage_block_size" ), - min_log_storage_block_size, min_log_storage_block_size ); - read( find_param( fs, "max_log_storage_block_size" ), - max_log_storage_block_size, max_log_storage_block_size ); - read( find_param( fs, "min_log_elem_size" ), min_log_elem_size, min_log_elem_size ); - read( find_param( fs, "max_log_elem_size" ), max_log_elem_size, max_log_elem_size ); - - struct_count = cvtest::clipInt( struct_count, 1, 100 ); - max_struct_size = cvtest::clipInt( max_struct_size, 1, 1<<20 ); - generations = cvtest::clipInt( generations, 1, 100 ); - iterations = cvtest::clipInt( iterations, 100, 1<<20 ); - - min_log_storage_block_size = cvtest::clipInt( min_log_storage_block_size, 7, 20 ); - max_log_storage_block_size = cvtest::clipInt( max_log_storage_block_size, - min_log_storage_block_size, 20 ); - - min_log_elem_size = cvtest::clipInt( min_log_elem_size, 0, 8 ); - max_log_elem_size = cvtest::clipInt( max_log_elem_size, min_log_elem_size, 10 ); - - return 0; -} - - -void Core_DynStructBaseTest::update_progressbar() -{ - int64 t; - - if( test_progress < 0 ) - { - test_progress = 0; - cpu_freq = cv::getTickFrequency(); - start_time = cv::getTickCount(); - } - - t = cv::getTickCount(); - test_progress = update_progress( test_progress, 0, 0, (double)(t - start_time)/cpu_freq ); -} - - -void Core_DynStructBaseTest::set_error_context( const char* condition, - const char* err_msg, - const char* filename, int lineno ) -{ - ts->printf( cvtest::TS::LOG, "file %s, line %d: %s\n(\"%s\" failed).\n" - "generation = %d, struct_idx = %d, iter = %d\n", - filename, lineno, err_msg, condition, gen, struct_idx, iter ); - ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT ); -} - - -int Core_DynStructBaseTest::test_seq_block_consistence( int _struct_idx, CvSeq* seq, int total ) -{ - int sum = 0; - struct_idx = _struct_idx; - - CV_TS_SEQ_CHECK_CONDITION( seq != 0, "Null sequence pointer" ); - - if( seq->first ) - { - CvSeqBlock* block = seq->first; - CvSeqBlock* prev_block = block->prev; - - int delta_idx = seq->first->start_index; - - for( ;; ) - { - CV_TS_SEQ_CHECK_CONDITION( sum == block->start_index - delta_idx && - block->count > 0 && block->prev == prev_block && - prev_block->next == block, - "sequence blocks are inconsistent" ); - sum += block->count; - prev_block = block; - block = block->next; - if( block == seq->first ) break; - } - - CV_TS_SEQ_CHECK_CONDITION( block->prev->count * seq->elem_size + - block->prev->data <= seq->block_max, - "block->data or block_max pointer are incorrect" ); - } - - CV_TS_SEQ_CHECK_CONDITION( seq->total == sum && sum == total, - "total number of elements is incorrect" ); - - return 0; -} - - -/////////////////////////////////// sequence tests //////////////////////////////////// - -class Core_SeqBaseTest : public Core_DynStructBaseTest -{ -public: - Core_SeqBaseTest(); - virtual ~Core_SeqBaseTest(); - void clear(); - void run( int ); - -protected: - int test_multi_create(); - int test_get_seq_elem( int _struct_idx, int iters ); - int test_get_seq_reading( int _struct_idx, int iters ); - int test_seq_ops( int iters ); -}; - -Core_SeqBaseTest::Core_SeqBaseTest() -{ -} - -Core_SeqBaseTest::~Core_SeqBaseTest() -{ - clear(); -} - -void Core_SeqBaseTest::clear() -{ - for( size_t i = 0; i < simple_struct.size(); i++ ) - cvTsReleaseSimpleSeq( (CvTsSimpleSeq**)&simple_struct[i] ); - Core_DynStructBaseTest::clear(); -} - - -int Core_SeqBaseTest::test_multi_create() -{ - vector writer(struct_count); - vector pos(struct_count); - vector index(struct_count); - int cur_count, elem_size; - RNG& rng = ts->get_rng(); - - for( int i = 0; i < struct_count; i++ ) - { - double t; - CvTsSimpleSeq* sseq; - - pos[i] = -1; - index[i] = i; - - t = cvtest::randReal(rng)*(max_log_elem_size - min_log_elem_size) + min_log_elem_size; - elem_size = cvRound( exp(t * CV_LOG2) ); - elem_size = MIN( elem_size, (int)(storage->block_size - sizeof(void*) - - sizeof(CvSeqBlock) - sizeof(CvMemBlock)) ); - - cvTsReleaseSimpleSeq( (CvTsSimpleSeq**)&simple_struct[i] ); - simple_struct[i] = sseq = cvTsCreateSimpleSeq( max_struct_size, elem_size ); - cxcore_struct[i] = 0; - sseq->count = cvtest::randInt( rng ) % max_struct_size; - Mat m( 1, MAX(sseq->count,1)*elem_size, CV_8UC1, sseq->array ); - cvtest::randUni( rng, m, Scalar::all(0), Scalar::all(256) ); - } - - for( cur_count = struct_count; cur_count > 0; cur_count-- ) - { - for(;;) - { - int k = cvtest::randInt( rng ) % cur_count; - struct_idx = index[k]; - CvTsSimpleSeq* sseq = (CvTsSimpleSeq*)simple_struct[struct_idx]; - - if( pos[struct_idx] < 0 ) - { - int hdr_size = (cvtest::randInt(rng) % 10)*4 + sizeof(CvSeq); - hdr_size = MIN( hdr_size, (int)(storage->block_size - sizeof(CvMemBlock)) ); - elem_size = sseq->elem_size; - - if( cvtest::randInt(rng) % 2 ) - { - cvStartWriteSeq( 0, hdr_size, elem_size, storage, &writer[struct_idx] ); - } - else - { - CvSeq* s; - s = cvCreateSeq( 0, hdr_size, elem_size, storage ); - cvStartAppendToSeq( s, &writer[struct_idx] ); - } - - cvSetSeqBlockSize( writer[struct_idx].seq, cvtest::randInt( rng ) % 10000 ); - pos[struct_idx] = 0; - } - - update_progressbar(); - if( pos[struct_idx] == sseq->count ) - { - cxcore_struct[struct_idx] = cvEndWriteSeq( &writer[struct_idx] ); - /* del index */ - for( ; k < cur_count-1; k++ ) - index[k] = index[k+1]; - break; - } - - { - schar* el = cvTsSimpleSeqElem( sseq, pos[struct_idx] ); - CV_WRITE_SEQ_ELEM_VAR( el, writer[struct_idx] ); - } - pos[struct_idx]++; - } - } - - return 0; -} - - -int Core_SeqBaseTest::test_get_seq_elem( int _struct_idx, int iters ) -{ - RNG& rng = ts->get_rng(); - - CvSeq* seq = (CvSeq*)cxcore_struct[_struct_idx]; - CvTsSimpleSeq* sseq = (CvTsSimpleSeq*)simple_struct[_struct_idx]; - struct_idx = _struct_idx; - - CV_Assert( seq->total == sseq->count ); - - if( sseq->count == 0 ) - return 0; - - for( int i = 0; i < iters; i++ ) - { - int idx = cvtest::randInt(rng) % (sseq->count*3) - sseq->count*3/2; - int idx0 = (unsigned)idx < (unsigned)(sseq->count) ? idx : idx < 0 ? - idx + sseq->count : idx - sseq->count; - int bad_range = (unsigned)idx0 >= (unsigned)(sseq->count); - schar* elem; - elem = cvGetSeqElem( seq, idx ); - - if( bad_range ) - { - CV_TS_SEQ_CHECK_CONDITION( elem == 0, - "cvGetSeqElem doesn't " - "handle \"out of range\" properly" ); - } - else - { - CV_TS_SEQ_CHECK_CONDITION( elem != 0 && - !memcmp( elem, cvTsSimpleSeqElem(sseq, idx0), sseq->elem_size ), - "cvGetSeqElem returns wrong element" ); - - idx = cvSeqElemIdx(seq, elem ); - CV_TS_SEQ_CHECK_CONDITION( idx >= 0 && idx == idx0, - "cvSeqElemIdx is incorrect" ); - } - } - - return 0; -} - - -int Core_SeqBaseTest::test_get_seq_reading( int _struct_idx, int iters ) -{ - const int max_val = 3*5 + 2; - CvSeq* seq = (CvSeq*)cxcore_struct[_struct_idx]; - CvTsSimpleSeq* sseq = (CvTsSimpleSeq*)simple_struct[_struct_idx]; - int total = seq->total; - RNG& rng = ts->get_rng(); - CvSeqReader reader; - vector _elem(sseq->elem_size); - schar* elem = &_elem[0]; - - CV_Assert( total == sseq->count ); - this->struct_idx = _struct_idx; - - int pos = cvtest::randInt(rng) % 2; - cvStartReadSeq( seq, &reader, pos ); - - if( total == 0 ) - { - CV_TS_SEQ_CHECK_CONDITION( reader.ptr == 0, "Empty sequence reader pointer is not NULL" ); - return 0; - } - - pos = pos ? seq->total - 1 : 0; - - CV_TS_SEQ_CHECK_CONDITION( pos == cvGetSeqReaderPos(&reader), - "initial reader position is wrong" ); - - for( iter = 0; iter < iters; iter++ ) - { - int op = cvtest::randInt(rng) % max_val; - - if( op >= max_val - 2 ) - { - int new_pos, new_pos0; - int bad_range; - int is_relative = op == max_val - 1; - - new_pos = cvtest::randInt(rng) % (total*2) - total; - new_pos0 = new_pos + (is_relative ? pos : 0 ); - - if( new_pos0 < 0 ) new_pos0 += total; - if( new_pos0 >= total ) new_pos0 -= total; - - bad_range = (unsigned)new_pos0 >= (unsigned)total; - cvSetSeqReaderPos( &reader, new_pos, is_relative ); - - if( !bad_range ) - { - CV_TS_SEQ_CHECK_CONDITION( new_pos0 == cvGetSeqReaderPos( &reader ), - "cvset reader position doesn't work" ); - pos = new_pos0; - } - else - { - CV_TS_SEQ_CHECK_CONDITION( pos == cvGetSeqReaderPos( &reader ), - "reader doesn't stay at the current position after wrong positioning" ); - } - } - else - { - int direction = (op % 3) - 1; - memcpy( elem, reader.ptr, sseq->elem_size ); - - if( direction > 0 ) - { - CV_NEXT_SEQ_ELEM( sseq->elem_size, reader ); - } - else if( direction < 0 ) - { - CV_PREV_SEQ_ELEM( sseq->elem_size, reader ); - } - - CV_TS_SEQ_CHECK_CONDITION( memcmp(elem, cvTsSimpleSeqElem(sseq, pos), - sseq->elem_size) == 0, "reading is incorrect" ); - pos += direction; - if( -pos > 0 ) pos += total; - if( pos >= total ) pos -= total; - - CV_TS_SEQ_CHECK_CONDITION( pos == cvGetSeqReaderPos( &reader ), - "reader doesn't move correctly after reading" ); - } - } - - return 0; -} - - -int Core_SeqBaseTest::test_seq_ops( int iters ) -{ - const int max_op = 14; - int max_elem_size = 0; - schar* elem2 = 0; - RNG& rng = ts->get_rng(); - - for( int i = 0; i < struct_count; i++ ) - max_elem_size = MAX( max_elem_size, ((CvSeq*)cxcore_struct[i])->elem_size ); - - vector elem_buf(max_struct_size*max_elem_size); - schar* elem = (schar*)&elem_buf[0]; - Mat elem_mat; - - for( iter = 0; iter < iters; iter++ ) - { - struct_idx = cvtest::randInt(rng) % struct_count; - int op = cvtest::randInt(rng) % max_op; - CvSeq* seq = (CvSeq*)cxcore_struct[struct_idx]; - CvTsSimpleSeq* sseq = (CvTsSimpleSeq*)simple_struct[struct_idx]; - int elem_size = sseq->elem_size; - int whence = 0, pos = 0, count = 0; - - switch( op ) - { - case 0: - case 1: - case 2: // push/pushfront/insert - if( sseq->count == sseq->max_count ) - break; - - elem_mat = Mat(1, elem_size, CV_8U, elem); - cvtest::randUni( rng, elem_mat, cvScalarAll(0), cvScalarAll(255) ); - - whence = op - 1; - if( whence < 0 ) - { - pos = 0; - cvSeqPushFront( seq, elem ); - } - else if( whence > 0 ) - { - pos = sseq->count; - cvSeqPush( seq, elem ); - } - else - { - pos = cvtest::randInt(rng) % (sseq->count + 1); - cvSeqInsert( seq, pos, elem ); - } - - cvTsSimpleSeqShiftAndCopy( sseq, pos, pos + 1, elem ); - elem2 = cvGetSeqElem( seq, pos ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0, "The inserted element could not be retrieved" ); - CV_TS_SEQ_CHECK_CONDITION( seq->total == sseq->count && - memcmp(elem2, cvTsSimpleSeqElem(sseq,pos), elem_size) == 0, - "The inserted sequence element is wrong" ); - break; - - case 3: - case 4: - case 5: // pop/popfront/remove - if( sseq->count == 0 ) - break; - - whence = op - 4; - if( whence < 0 ) - { - pos = 0; - cvSeqPopFront( seq, elem ); - } - else if( whence > 0 ) - { - pos = sseq->count-1; - cvSeqPop( seq, elem ); - } - else - { - pos = cvtest::randInt(rng) % sseq->count; - cvSeqRemove( seq, pos ); - } - - if( whence != 0 ) - CV_TS_SEQ_CHECK_CONDITION( seq->total == sseq->count - 1 && - memcmp( elem, cvTsSimpleSeqElem(sseq,pos), elem_size) == 0, - "The popped sequence element isn't correct" ); - - cvTsSimpleSeqShiftAndCopy( sseq, pos + 1, pos ); - - if( sseq->count > 0 ) - { - elem2 = cvGetSeqElem( seq, pos < sseq->count ? pos : -1 ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0, "GetSeqElem fails after removing the element" ); - - CV_TS_SEQ_CHECK_CONDITION( memcmp( elem2, - cvTsSimpleSeqElem(sseq, pos - (pos == sseq->count)), elem_size) == 0, - "The first shifted element is not correct after removing another element" ); - } - else - { - CV_TS_SEQ_CHECK_CONDITION( seq->first == 0, - "The sequence doesn't become empty after the final remove" ); - } - break; - - case 6: - case 7: - case 8: // push [front] multi/insert slice - if( sseq->count == sseq->max_count ) - break; - - count = cvtest::randInt( rng ) % (sseq->max_count - sseq->count + 1); - elem_mat = Mat(1, MAX(count,1) * elem_size, CV_8U, elem); - cvtest::randUni( rng, elem_mat, cvScalarAll(0), cvScalarAll(255) ); - - whence = op - 7; - pos = whence < 0 ? 0 : whence > 0 ? sseq->count : (int)(cvtest::randInt(rng) % (sseq->count+1)); - if( whence != 0 ) - { - cvSeqPushMulti( seq, elem, count, whence < 0 ); - } - else - { - CvSeq header; - CvSeqBlock block; - cvMakeSeqHeaderForArray( CV_SEQ_KIND_GENERIC, sizeof(CvSeq), - sseq->elem_size, - elem, count, - &header, &block ); - - cvSeqInsertSlice( seq, pos, &header ); - } - cvTsSimpleSeqShiftAndCopy( sseq, pos, pos + count, elem ); - - if( sseq->count > 0 ) - { - // choose the random element among the added - pos = count > 0 ? (int)(cvtest::randInt(rng) % count + pos) : MAX(pos-1,0); - elem2 = cvGetSeqElem( seq, pos ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0, "multi push operation doesn't add elements" ); - CV_TS_SEQ_CHECK_CONDITION( seq->total == sseq->count && - memcmp( elem2, cvTsSimpleSeqElem(sseq,pos), elem_size) == 0, - "One of the added elements is wrong" ); - } - else - { - CV_TS_SEQ_CHECK_CONDITION( seq->total == 0 && seq->first == 0, - "Adding no elements to empty sequence fails" ); - } - break; - - case 9: - case 10: - case 11: // pop [front] multi - if( sseq->count == 0 ) - break; - - count = cvtest::randInt(rng) % (sseq->count+1); - whence = op - 10; - pos = whence < 0 ? 0 : whence > 0 ? sseq->count - count : - (int)(cvtest::randInt(rng) % (sseq->count - count + 1)); - - if( whence != 0 ) - { - cvSeqPopMulti( seq, elem, count, whence < 0 ); - - if( count > 0 ) - { - CV_TS_SEQ_CHECK_CONDITION( memcmp(elem, - cvTsSimpleSeqElem(sseq,pos), elem_size) == 0, - "The first (in the sequence order) removed element is wrong after popmulti" ); - } - } - else - { - cvSeqRemoveSlice( seq, cvSlice(pos, pos + count) ); - } - - CV_TS_SEQ_CHECK_CONDITION( seq->total == sseq->count - count, - "The popmulti left a wrong number of elements in the sequence" ); - - cvTsSimpleSeqShiftAndCopy( sseq, pos + count, pos, 0 ); - if( sseq->count > 0 ) - { - pos = whence < 0 ? 0 : MIN( pos, sseq->count - 1 ); - elem2 = cvGetSeqElem( seq, pos ); - CV_TS_SEQ_CHECK_CONDITION( elem2 && - memcmp( elem2, cvTsSimpleSeqElem(sseq,pos), elem_size) == 0, - "The last sequence element is wrong after POP" ); - } - else - { - CV_TS_SEQ_CHECK_CONDITION( seq->total == 0 && seq->first == 0, - "The sequence doesn't become empty after final POP" ); - } - break; - case 12: // seqslice - { - CvMemStoragePos storage_pos; - cvSaveMemStoragePos( storage, &storage_pos ); - - int copy_data = cvtest::randInt(rng) % 2; - count = cvtest::randInt(rng) % (seq->total + 1); - pos = cvtest::randInt(rng) % (seq->total - count + 1); - CvSeq* seq_slice = cvSeqSlice( seq, cvSlice(pos, pos + count), storage, copy_data ); - - CV_TS_SEQ_CHECK_CONDITION( seq_slice && seq_slice->total == count, - "cvSeqSlice returned incorrect slice" ); - - if( count > 0 ) - { - int test_idx = cvtest::randInt(rng) % count; - elem2 = cvGetSeqElem( seq_slice, test_idx ); - schar* elem3 = cvGetSeqElem( seq, pos + test_idx ); - CV_TS_SEQ_CHECK_CONDITION( elem2 && - memcmp( elem2, cvTsSimpleSeqElem(sseq,pos + test_idx), elem_size) == 0, - "The extracted slice elements are not correct" ); - CV_TS_SEQ_CHECK_CONDITION( (elem2 == elem3) ^ copy_data, - "copy_data flag is handled incorrectly" ); - } - - cvRestoreMemStoragePos( storage, &storage_pos ); - } - break; - case 13: // clear - cvTsClearSimpleSeq( sseq ); - cvClearSeq( seq ); - CV_TS_SEQ_CHECK_CONDITION( seq->total == 0 && seq->first == 0, - "The sequence doesn't become empty after clear" ); - break; - default: - CV_Assert(0); - return -1; - } - - if( test_seq_block_consistence(struct_idx, seq, sseq->count) < 0 ) - return -1; - - if( test_get_seq_elem(struct_idx, 7) < 0 ) - return -1; - - update_progressbar(); - } - - return 0; -} - - -void Core_SeqBaseTest::run( int ) -{ - try - { - RNG& rng = ts->get_rng(); - int i; - double t; - - clear(); - test_progress = -1; - - simple_struct.resize(struct_count, 0); - cxcore_struct.resize(struct_count, 0); - - for( gen = 0; gen < generations; gen++ ) - { - struct_idx = iter = -1; - - if( !storage ) - { - t = cvtest::randReal(rng)*(max_log_storage_block_size - min_log_storage_block_size) - + min_log_storage_block_size; - storage.reset(cvCreateMemStorage( cvRound( exp(t * CV_LOG2) ) )); - } - - iter = struct_idx = -1; - test_multi_create(); - - for( i = 0; i < struct_count; i++ ) - { - if( test_seq_block_consistence(i, (CvSeq*)cxcore_struct[i], - ((CvTsSimpleSeq*)simple_struct[i])->count) < 0 ) - return; - - if( test_get_seq_elem( i, MAX(iterations/3,7) ) < 0 ) - return; - - if( test_get_seq_reading( i, MAX(iterations/3,7) ) < 0 ) - return; - update_progressbar(); - } - - if( test_seq_ops( iterations ) < 0 ) - return; - - if( cvtest::randInt(rng) % 2 ) - storage.release(); - else - cvClearMemStorage( storage ); - } - } - catch(const int &) - { - } -} - - -////////////////////////////// more sequence tests ////////////////////////////////////// - -class Core_SeqSortInvTest : public Core_SeqBaseTest -{ -public: - Core_SeqSortInvTest(); - void run( int ); - -protected: -}; - - -Core_SeqSortInvTest::Core_SeqSortInvTest() -{ -} - - -static int icvCmpSeqElems( const void* a, const void* b, void* userdata ) -{ - return memcmp( a, b, ((CvSeq*)userdata)->elem_size ); -} - -static int icvCmpSeqElems2_elem_size = 0; -static int icvCmpSeqElems2( const void* a, const void* b ) -{ - return memcmp( a, b, icvCmpSeqElems2_elem_size ); -} - - -void Core_SeqSortInvTest::run( int ) -{ - try - { - RNG& rng = ts->get_rng(); - int i, k; - double t; - schar *elem0, *elem, *elem2; - vector buffer; - - clear(); - test_progress = -1; - - simple_struct.resize(struct_count, 0); - cxcore_struct.resize(struct_count, 0); - - for( gen = 0; gen < generations; gen++ ) - { - struct_idx = iter = -1; - - if( !storage ) - { - t = cvtest::randReal(rng)*(max_log_storage_block_size - min_log_storage_block_size) - + min_log_storage_block_size; - storage.reset(cvCreateMemStorage( cvRound( exp(t * CV_LOG2) ) )); - } - - for( iter = 0; iter < iterations/10; iter++ ) - { - int max_size = 0; - test_multi_create(); - - for( i = 0; i < struct_count; i++ ) - { - CvTsSimpleSeq* sseq = (CvTsSimpleSeq*)simple_struct[i]; - max_size = MAX( max_size, sseq->count*sseq->elem_size ); - } - - buffer.resize(max_size); - - for( i = 0; i < struct_count; i++ ) - { - CvSeq* seq = (CvSeq*)cxcore_struct[i]; - CvTsSimpleSeq* sseq = (CvTsSimpleSeq*)simple_struct[i]; - CvSlice slice = CV_WHOLE_SEQ; - - //printf("%d. %d. %d-th size = %d\n", gen, iter, i, sseq->count ); - - cvSeqInvert( seq ); - cvTsSimpleSeqInvert( sseq ); - - if( test_seq_block_consistence( i, seq, sseq->count ) < 0 ) - return; - - if( sseq->count > 0 && cvtest::randInt(rng) % 2 == 0 ) - { - slice.end_index = cvtest::randInt(rng) % sseq->count + 1; - slice.start_index = cvtest::randInt(rng) % (sseq->count - slice.end_index + 1); - slice.end_index += slice.start_index; - } - - cvCvtSeqToArray( seq, &buffer[0], slice ); - - slice.end_index = MIN( slice.end_index, sseq->count ); - CV_TS_SEQ_CHECK_CONDITION( sseq->count == 0 || memcmp( &buffer[0], - sseq->array + slice.start_index*sseq->elem_size, - (slice.end_index - slice.start_index)*sseq->elem_size ) == 0, - "cvSeqInvert returned wrong result" ); - - for( k = 0; k < (sseq->count > 0 ? 10 : 0); k++ ) - { - int idx0 = cvtest::randInt(rng) % sseq->count, idx = 0; - elem0 = cvTsSimpleSeqElem( sseq, idx0 ); - elem = cvGetSeqElem( seq, idx0 ); - elem2 = cvSeqSearch( seq, elem0, k % 2 ? icvCmpSeqElems : 0, 0, &idx, seq ); - - CV_TS_SEQ_CHECK_CONDITION( elem != 0 && - memcmp( elem0, elem, seq->elem_size ) == 0, - "cvSeqInvert gives incorrect result" ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0 && - memcmp( elem0, elem2, seq->elem_size ) == 0 && - elem2 == cvGetSeqElem( seq, idx ), - "cvSeqSearch failed (linear search)" ); - } - - cvSeqSort( seq, icvCmpSeqElems, seq ); - - if( test_seq_block_consistence( i, seq, sseq->count ) < 0 ) - return; - - if( sseq->count > 0 ) - { - // !!! This is not thread-safe !!! - icvCmpSeqElems2_elem_size = sseq->elem_size; - qsort( sseq->array, sseq->count, sseq->elem_size, icvCmpSeqElems2 ); - - if( cvtest::randInt(rng) % 2 == 0 ) - { - slice.end_index = cvtest::randInt(rng) % sseq->count + 1; - slice.start_index = cvtest::randInt(rng) % (sseq->count - slice.end_index + 1); - slice.end_index += slice.start_index; - } - } - - cvCvtSeqToArray( seq, &buffer[0], slice ); - CV_TS_SEQ_CHECK_CONDITION( sseq->count == 0 || memcmp( &buffer[0], - sseq->array + slice.start_index*sseq->elem_size, - (slice.end_index - slice.start_index)*sseq->elem_size ) == 0, - "cvSeqSort returned wrong result" ); - - for( k = 0; k < (sseq->count > 0 ? 10 : 0); k++ ) - { - int idx0 = cvtest::randInt(rng) % sseq->count, idx = 0; - elem0 = cvTsSimpleSeqElem( sseq, idx0 ); - elem = cvGetSeqElem( seq, idx0 ); - elem2 = cvSeqSearch( seq, elem0, icvCmpSeqElems, 1, &idx, seq ); - - CV_TS_SEQ_CHECK_CONDITION( elem != 0 && - memcmp( elem0, elem, seq->elem_size ) == 0, - "cvSeqSort gives incorrect result" ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0 && - memcmp( elem0, elem2, seq->elem_size ) == 0 && - elem2 == cvGetSeqElem( seq, idx ), - "cvSeqSearch failed (binary search)" ); - } - } - - cvClearMemStorage( storage ); - } - - storage.release(); - } - } - catch (const int &) - { - } -} - - -/////////////////////////////////////// set tests /////////////////////////////////////// - -class Core_SetTest : public Core_DynStructBaseTest -{ -public: - Core_SetTest(); - virtual ~Core_SetTest(); - void clear(); - void run( int ); - -protected: - //int test_seq_block_consistence( int struct_idx ); - int test_set_ops( int iters ); -}; - - -Core_SetTest::Core_SetTest() -{ -} - -Core_SetTest::~Core_SetTest() -{ - clear(); -} - -void Core_SetTest::clear() -{ - for( size_t i = 0; i < simple_struct.size(); i++ ) - cvTsReleaseSimpleSet( (CvTsSimpleSet**)&simple_struct[i] ); - Core_DynStructBaseTest::clear(); -} - - -int Core_SetTest::test_set_ops( int iters ) -{ - const int max_op = 4; - int max_elem_size = 0; - int idx, idx0; - CvSetElem *elem = 0, *elem2 = 0, *elem3 = 0; - schar* elem_data = 0; - RNG& rng = ts->get_rng(); - //int max_active_count = 0, mean_active_count = 0; - - for( int i = 0; i < struct_count; i++ ) - max_elem_size = MAX( max_elem_size, ((CvSeq*)cxcore_struct[i])->elem_size ); - - vector elem_buf(max_elem_size); - Mat elem_mat; - - for( iter = 0; iter < iters; iter++ ) - { - struct_idx = cvtest::randInt(rng) % struct_count; - - CvSet* cvset = (CvSet*)cxcore_struct[struct_idx]; - CvTsSimpleSet* sset = (CvTsSimpleSet*)simple_struct[struct_idx]; - int pure_elem_size = sset->elem_size - 1; - int prev_total = cvset->total, prev_count = cvset->active_count; - int op = cvtest::randInt(rng) % (iter <= iters/10 ? 2 : max_op); - int by_ptr = op % 2 == 0; - CvSetElem* first_free = cvset->free_elems; - CvSetElem* next_free = first_free ? first_free->next_free : 0; - int pass_data = 0; - - if( iter > iters/10 && cvtest::randInt(rng)%200 == 0 ) // clear set - { - prev_count = cvset->total; - cvClearSet( cvset ); - cvTsClearSimpleSet( sset ); - - CV_TS_SEQ_CHECK_CONDITION( cvset->active_count == 0 && cvset->total == 0 && - cvset->first == 0 && cvset->free_elems == 0 && - (cvset->free_blocks != 0 || prev_count == 0), - "cvClearSet doesn't remove all the elements" ); - continue; - } - else if( op == 0 || op == 1 ) // add element - { - if( sset->free_count == 0 ) - continue; - - elem_mat = Mat(1, cvset->elem_size, CV_8U, &elem_buf[0]); - cvtest::randUni( rng, elem_mat, cvScalarAll(0), cvScalarAll(255) ); - elem = (CvSetElem*)&elem_buf[0]; - - if( by_ptr ) - { - elem2 = cvSetNew( cvset ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0, "cvSetNew returned NULL pointer" ); - } - else - { - pass_data = cvtest::randInt(rng) % 2; - idx = cvSetAdd( cvset, pass_data ? elem : 0, &elem2 ); - CV_TS_SEQ_CHECK_CONDITION( elem2 != 0 && elem2->flags == idx, - "cvSetAdd returned NULL pointer or a wrong index" ); - } - - elem_data = (schar*)elem + sizeof(int); - - if( !pass_data ) - memcpy( (schar*)elem2 + sizeof(int), elem_data, pure_elem_size ); - - idx = elem2->flags; - idx0 = cvTsSimpleSetAdd( sset, elem_data ); - elem3 = cvGetSetElem( cvset, idx ); - - CV_TS_SEQ_CHECK_CONDITION( CV_IS_SET_ELEM(elem3) && - idx == idx0 && elem3 == elem2 && (!pass_data || - memcmp( (char*)elem3 + sizeof(int), elem_data, pure_elem_size) == 0), - "The added element is not correct" ); - - CV_TS_SEQ_CHECK_CONDITION( (!first_free || elem3 == first_free) && - (!next_free || cvset->free_elems == next_free) && - cvset->active_count == prev_count + 1, - "The free node list is modified incorrectly" ); - } - else if( op == 2 || op == 3 ) // remove element - { - idx = cvtest::randInt(rng) % sset->max_count; - - if( sset->free_count == sset->max_count || idx >= sset->count ) - continue; - - elem_data = cvTsSimpleSetFind(sset, idx); - if( elem_data == 0 ) - continue; - - elem = cvGetSetElem( cvset, idx ); - CV_TS_SEQ_CHECK_CONDITION( CV_IS_SET_ELEM(elem) && elem->flags == idx && - memcmp((char*)elem + sizeof(int), elem_data, pure_elem_size) == 0, - "cvGetSetElem returned wrong element" ); - - if( by_ptr ) - { - cvSetRemoveByPtr( cvset, elem ); - } - else - { - cvSetRemove( cvset, idx ); - } - - cvTsSimpleSetRemove( sset, idx ); - - CV_TS_SEQ_CHECK_CONDITION( !CV_IS_SET_ELEM(elem) && !cvGetSetElem(cvset, idx) && - (elem->flags & CV_SET_ELEM_IDX_MASK) == idx, - "cvSetRemove[ByPtr] didn't release the element properly" ); - - CV_TS_SEQ_CHECK_CONDITION( elem->next_free == first_free && - cvset->free_elems == elem && - cvset->active_count == prev_count - 1, - "The free node list has not been updated properly" ); - } - - //max_active_count = MAX( max_active_count, cvset->active_count ); - //mean_active_count += cvset->active_count; - CV_TS_SEQ_CHECK_CONDITION( cvset->active_count == sset->max_count - sset->free_count && - cvset->total >= cvset->active_count && - (cvset->total == 0 || cvset->total >= prev_total), - "The total number of cvset elements is not correct" ); - - // CvSet and simple set do not necessary have the same "total" (active & free) number, - // so pass "set->total" to skip that check - test_seq_block_consistence( struct_idx, (CvSeq*)cvset, cvset->total ); - update_progressbar(); - } - - return 0; -} - - -void Core_SetTest::run( int ) -{ - try - { - RNG& rng = ts->get_rng(); - double t; - - clear(); - test_progress = -1; - - simple_struct.resize(struct_count, 0); - cxcore_struct.resize(struct_count, 0); - - for( gen = 0; gen < generations; gen++ ) - { - struct_idx = iter = -1; - t = cvtest::randReal(rng)*(max_log_storage_block_size - min_log_storage_block_size) + min_log_storage_block_size; - storage.reset(cvCreateMemStorage( cvRound( exp(t * CV_LOG2) ) )); - - for( int i = 0; i < struct_count; i++ ) - { - t = cvtest::randReal(rng)*(max_log_elem_size - min_log_elem_size) + min_log_elem_size; - int pure_elem_size = cvRound( exp(t * CV_LOG2) ); - int elem_size = pure_elem_size + sizeof(int); - elem_size = (elem_size + sizeof(size_t) - 1) & ~(sizeof(size_t)-1); - elem_size = MAX( elem_size, (int)sizeof(CvSetElem) ); - elem_size = MIN( elem_size, (int)(storage->block_size - sizeof(void*) - sizeof(CvMemBlock) - sizeof(CvSeqBlock)) ); - pure_elem_size = MIN( pure_elem_size, elem_size-(int)sizeof(CvSetElem) ); - - cvTsReleaseSimpleSet( (CvTsSimpleSet**)&simple_struct[i] ); - simple_struct[i] = cvTsCreateSimpleSet( max_struct_size, pure_elem_size ); - cxcore_struct[i] = cvCreateSet( 0, sizeof(CvSet), elem_size, storage ); - } - - if( test_set_ops( iterations*100 ) < 0 ) - return; - - storage.release(); - } - } - catch(const int &) - { - } -} - - -/////////////////////////////////////// graph tests ////////////////////////////////// - -class Core_GraphTest : public Core_DynStructBaseTest -{ -public: - Core_GraphTest(); - virtual ~Core_GraphTest(); - void clear(); - void run( int ); - -protected: - //int test_seq_block_consistence( int struct_idx ); - int test_graph_ops( int iters ); -}; - - -Core_GraphTest::Core_GraphTest() -{ -} - -Core_GraphTest::~Core_GraphTest() -{ - clear(); -} - -void Core_GraphTest::clear() -{ - for( size_t i = 0; i < simple_struct.size(); i++ ) - cvTsReleaseSimpleGraph( (CvTsSimpleGraph**)&simple_struct[i] ); - Core_DynStructBaseTest::clear(); -} - - -int Core_GraphTest::test_graph_ops( int iters ) -{ - const int max_op = 4; - int i, k; - int max_elem_size = 0; - int idx, idx0; - CvGraphVtx *vtx = 0, *vtx2 = 0, *vtx3 = 0; - CvGraphEdge* edge = 0, *edge2 = 0; - RNG& rng = ts->get_rng(); - //int max_active_count = 0, mean_active_count = 0; - - for( i = 0; i < struct_count; i++ ) - { - CvGraph* graph = (CvGraph*)cxcore_struct[i]; - max_elem_size = MAX( max_elem_size, graph->elem_size ); - max_elem_size = MAX( max_elem_size, graph->edges->elem_size ); - } - - vector elem_buf(max_elem_size); - Mat elem_mat; - - for( iter = 0; iter < iters; iter++ ) - { - struct_idx = cvtest::randInt(rng) % struct_count; - CvGraph* graph = (CvGraph*)cxcore_struct[struct_idx]; - CvTsSimpleGraph* sgraph = (CvTsSimpleGraph*)simple_struct[struct_idx]; - CvSet* edges = graph->edges; - schar *vtx_data; - char *edge_data; - int pure_vtx_size = sgraph->vtx->elem_size - 1, - pure_edge_size = sgraph->edge_size - 1; - int prev_vtx_total = graph->total, - prev_edge_total = graph->edges->total, - prev_vtx_count = graph->active_count, - prev_edge_count = graph->edges->active_count; - int op = cvtest::randInt(rng) % max_op; - int pass_data = 0, vtx_degree0 = 0, vtx_degree = 0; - CvSetElem *first_free, *next_free; - - if( cvtest::randInt(rng) % 200 == 0 ) // clear graph - { - int prev_vtx_count2 = graph->total, prev_edge_count2 = graph->edges->total; - - cvClearGraph( graph ); - cvTsClearSimpleGraph( sgraph ); - - CV_TS_SEQ_CHECK_CONDITION( graph->active_count == 0 && graph->total == 0 && - graph->first == 0 && graph->free_elems == 0 && - (graph->free_blocks != 0 || prev_vtx_count2 == 0), - "The graph is not empty after clearing" ); - - CV_TS_SEQ_CHECK_CONDITION( edges->active_count == 0 && edges->total == 0 && - edges->first == 0 && edges->free_elems == 0 && - (edges->free_blocks != 0 || prev_edge_count2 == 0), - "The graph is not empty after clearing" ); - } - else if( op == 0 ) // add vertex - { - if( sgraph->vtx->free_count == 0 ) - continue; - - first_free = graph->free_elems; - next_free = first_free ? first_free->next_free : 0; - - if( pure_vtx_size ) - { - elem_mat = Mat(1, graph->elem_size, CV_8U, &elem_buf[0]); - cvtest::randUni( rng, elem_mat, cvScalarAll(0), cvScalarAll(255) ); - } - - vtx = (CvGraphVtx*)&elem_buf[0]; - idx0 = cvTsSimpleGraphAddVertex( sgraph, vtx + 1 ); - - pass_data = cvtest::randInt(rng) % 2; - idx = cvGraphAddVtx( graph, pass_data ? vtx : 0, &vtx2 ); - - if( !pass_data && pure_vtx_size > 0 ) - memcpy( vtx2 + 1, vtx + 1, pure_vtx_size ); - - vtx3 = cvGetGraphVtx( graph, idx ); - - CV_TS_SEQ_CHECK_CONDITION( (CV_IS_SET_ELEM(vtx3) && vtx3->flags == idx && - vtx3->first == 0) || (idx == idx0 && vtx3 == vtx2 && - (!pass_data || pure_vtx_size == 0 || - memcmp(vtx3 + 1, vtx + 1, pure_vtx_size) == 0)), - "The added element is not correct" ); - - CV_TS_SEQ_CHECK_CONDITION( (!first_free || first_free == (CvSetElem*)vtx3) && - (!next_free || graph->free_elems == next_free) && - graph->active_count == prev_vtx_count + 1, - "The free node list is modified incorrectly" ); - } - else if( op == 1 ) // remove vertex - { - idx = cvtest::randInt(rng) % sgraph->vtx->max_count; - if( sgraph->vtx->free_count == sgraph->vtx->max_count || idx >= sgraph->vtx->count ) - continue; - - vtx_data = cvTsSimpleGraphFindVertex(sgraph, idx); - if( vtx_data == 0 ) - continue; - - vtx_degree0 = cvTsSimpleGraphVertexDegree( sgraph, idx ); - first_free = graph->free_elems; - - vtx = cvGetGraphVtx( graph, idx ); - CV_TS_SEQ_CHECK_CONDITION( CV_IS_SET_ELEM(vtx) && vtx->flags == idx && - (pure_vtx_size == 0 || memcmp( vtx + 1, vtx_data, pure_vtx_size) == 0), - "cvGetGraphVtx returned wrong element" ); - - if( cvtest::randInt(rng) % 2 ) - { - vtx_degree = cvGraphVtxDegreeByPtr( graph, vtx ); - cvGraphRemoveVtxByPtr( graph, vtx ); - } - else - { - vtx_degree = cvGraphVtxDegree( graph, idx ); - cvGraphRemoveVtx( graph, idx ); - } - - cvTsSimpleGraphRemoveVertex( sgraph, idx ); - - CV_TS_SEQ_CHECK_CONDITION( vtx_degree == vtx_degree0, - "Number of incident edges is different in two graph representations" ); - - CV_TS_SEQ_CHECK_CONDITION( !CV_IS_SET_ELEM(vtx) && !cvGetGraphVtx(graph, idx) && - (vtx->flags & CV_SET_ELEM_IDX_MASK) == idx, - "cvGraphRemoveVtx[ByPtr] didn't release the vertex properly" ); - - CV_TS_SEQ_CHECK_CONDITION( graph->edges->active_count == prev_edge_count - vtx_degree, - "cvGraphRemoveVtx[ByPtr] didn't remove all the incident edges " - "(or removed some extra)" ); - - CV_TS_SEQ_CHECK_CONDITION( ((CvSetElem*)vtx)->next_free == first_free && - graph->free_elems == (CvSetElem*)vtx && - graph->active_count == prev_vtx_count - 1, - "The free node list has not been updated properly" ); - } - else if( op == 2 ) // add edge - { - int v_idx[2] = {0,0}, res = 0; - int v_prev_degree[2] = {0,0}, v_degree[2] = {0,0}; - - if( sgraph->vtx->free_count >= sgraph->vtx->max_count-1 ) - continue; - - for( i = 0, k = 0; i < 10; i++ ) - { - int j = cvtest::randInt(rng) % sgraph->vtx->count; - vtx_data = cvTsSimpleGraphFindVertex( sgraph, j ); - if( vtx_data ) - { - v_idx[k] = j; - if( k == 0 ) - k++; - else if( v_idx[0] != v_idx[1] && - cvTsSimpleGraphFindEdge( sgraph, v_idx[0], v_idx[1] ) == 0 ) - { - k++; - break; - } - } - } - - if( k < 2 ) - continue; - - first_free = graph->edges->free_elems; - next_free = first_free ? first_free->next_free : 0; - - edge = cvFindGraphEdge( graph, v_idx[0], v_idx[1] ); - CV_TS_SEQ_CHECK_CONDITION( edge == 0, "Extra edge appeared in the graph" ); - - if( pure_edge_size > 0 ) - { - elem_mat = Mat(1, graph->edges->elem_size, CV_8U, &elem_buf[0]); - cvtest::randUni( rng, elem_mat, cvScalarAll(0), cvScalarAll(255) ); - } - edge = (CvGraphEdge*)&elem_buf[0]; - - // assign some default weight that is easy to check for - // consistensy, 'cause an edge weight is not stored - // in the simple graph - edge->weight = (float)(v_idx[0] + v_idx[1]); - pass_data = cvtest::randInt(rng) % 2; - - vtx = cvGetGraphVtx( graph, v_idx[0] ); - vtx2 = cvGetGraphVtx( graph, v_idx[1] ); - CV_TS_SEQ_CHECK_CONDITION( vtx != 0 && vtx2 != 0 && vtx->flags == v_idx[0] && - vtx2->flags == v_idx[1], "Some of the vertices are missing" ); - - if( cvtest::randInt(rng) % 2 ) - { - v_prev_degree[0] = cvGraphVtxDegreeByPtr( graph, vtx ); - v_prev_degree[1] = cvGraphVtxDegreeByPtr( graph, vtx2 ); - res = cvGraphAddEdgeByPtr(graph, vtx, vtx2, pass_data ? edge : 0, &edge2); - v_degree[0] = cvGraphVtxDegreeByPtr( graph, vtx ); - v_degree[1] = cvGraphVtxDegreeByPtr( graph, vtx2 ); - } - else - { - v_prev_degree[0] = cvGraphVtxDegree( graph, v_idx[0] ); - v_prev_degree[1] = cvGraphVtxDegree( graph, v_idx[1] ); - res = cvGraphAddEdge(graph, v_idx[0], v_idx[1], pass_data ? edge : 0, &edge2); - v_degree[0] = cvGraphVtxDegree( graph, v_idx[0] ); - v_degree[1] = cvGraphVtxDegree( graph, v_idx[1] ); - } - - //edge3 = (CvGraphEdge*)cvGetSetElem( graph->edges, idx ); - CV_TS_SEQ_CHECK_CONDITION( res == 1 && edge2 != 0 && CV_IS_SET_ELEM(edge2) && - ((edge2->vtx[0] == vtx && edge2->vtx[1] == vtx2) || - (!CV_IS_GRAPH_ORIENTED(graph) && edge2->vtx[0] == vtx2 && edge2->vtx[1] == vtx)) && - (!pass_data || pure_edge_size == 0 || memcmp( edge2 + 1, edge + 1, pure_edge_size ) == 0), - "The edge has been added incorrectly" ); - - if( !pass_data ) - { - if( pure_edge_size > 0 ) - memcpy( edge2 + 1, edge + 1, pure_edge_size ); - edge2->weight = edge->weight; - } - - CV_TS_SEQ_CHECK_CONDITION( v_degree[0] == v_prev_degree[0] + 1 && - v_degree[1] == v_prev_degree[1] + 1, - "The vertices lists have not been updated properly" ); - - cvTsSimpleGraphAddEdge( sgraph, v_idx[0], v_idx[1], edge + 1 ); - - CV_TS_SEQ_CHECK_CONDITION( (!first_free || first_free == (CvSetElem*)edge2) && - (!next_free || graph->edges->free_elems == next_free) && - graph->edges->active_count == prev_edge_count + 1, - "The free node list is modified incorrectly" ); - } - else if( op == 3 ) // find & remove edge - { - int v_idx[2] = {0,0}, by_ptr; - int v_prev_degree[2] = {0,0}, v_degree[2] = {0,0}; - - if( sgraph->vtx->free_count >= sgraph->vtx->max_count-1 ) - continue; - - edge_data = 0; - for( i = 0, k = 0; i < 10; i++ ) - { - int j = cvtest::randInt(rng) % sgraph->vtx->count; - vtx_data = cvTsSimpleGraphFindVertex( sgraph, j ); - if( vtx_data ) - { - v_idx[k] = j; - if( k == 0 ) - k++; - else - { - edge_data = cvTsSimpleGraphFindEdge( sgraph, v_idx[0], v_idx[1] ); - if( edge_data ) - { - k++; - break; - } - } - } - } - - if( k < 2 ) - continue; - - by_ptr = cvtest::randInt(rng) % 2; - first_free = graph->edges->free_elems; - - vtx = cvGetGraphVtx( graph, v_idx[0] ); - vtx2 = cvGetGraphVtx( graph, v_idx[1] ); - CV_TS_SEQ_CHECK_CONDITION( vtx != 0 && vtx2 != 0 && vtx->flags == v_idx[0] && - vtx2->flags == v_idx[1], "Some of the vertices are missing" ); - - if( by_ptr ) - { - edge = cvFindGraphEdgeByPtr( graph, vtx, vtx2 ); - v_prev_degree[0] = cvGraphVtxDegreeByPtr( graph, vtx ); - v_prev_degree[1] = cvGraphVtxDegreeByPtr( graph, vtx2 ); - } - else - { - edge = cvFindGraphEdge( graph, v_idx[0], v_idx[1] ); - v_prev_degree[0] = cvGraphVtxDegree( graph, v_idx[0] ); - v_prev_degree[1] = cvGraphVtxDegree( graph, v_idx[1] ); - } - - idx = edge->flags; - - CV_TS_SEQ_CHECK_CONDITION( edge != 0 && edge->weight == v_idx[0] + v_idx[1] && - ((edge->vtx[0] == vtx && edge->vtx[1] == vtx2) || - (!CV_IS_GRAPH_ORIENTED(graph) && edge->vtx[1] == vtx && edge->vtx[0] == vtx2)) && - (pure_edge_size == 0 || memcmp(edge + 1, edge_data, pure_edge_size) == 0), - "An edge is missing or incorrect" ); - - if( by_ptr ) - { - cvGraphRemoveEdgeByPtr( graph, vtx, vtx2 ); - edge2 = cvFindGraphEdgeByPtr( graph, vtx, vtx2 ); - v_degree[0] = cvGraphVtxDegreeByPtr( graph, vtx ); - v_degree[1] = cvGraphVtxDegreeByPtr( graph, vtx2 ); - } - else - { - cvGraphRemoveEdge(graph, v_idx[0], v_idx[1] ); - edge2 = cvFindGraphEdge( graph, v_idx[0], v_idx[1] ); - v_degree[0] = cvGraphVtxDegree( graph, v_idx[0] ); - v_degree[1] = cvGraphVtxDegree( graph, v_idx[1] ); - } - - CV_TS_SEQ_CHECK_CONDITION( !edge2 && !CV_IS_SET_ELEM(edge), - "The edge has not been removed from the edge set" ); - - CV_TS_SEQ_CHECK_CONDITION( v_degree[0] == v_prev_degree[0] - 1 && - v_degree[1] == v_prev_degree[1] - 1, - "The vertices lists have not been updated properly" ); - - cvTsSimpleGraphRemoveEdge( sgraph, v_idx[0], v_idx[1] ); - - CV_TS_SEQ_CHECK_CONDITION( graph->edges->free_elems == (CvSetElem*)edge && - graph->edges->free_elems->next_free == first_free && - graph->edges->active_count == prev_edge_count - 1, - "The free edge list has not been modified properly" ); - } - - //max_active_count = MAX( max_active_count, graph->active_count ); - //mean_active_count += graph->active_count; - - CV_TS_SEQ_CHECK_CONDITION( graph->active_count == sgraph->vtx->max_count - sgraph->vtx->free_count && - graph->total >= graph->active_count && - (graph->total == 0 || graph->total >= prev_vtx_total), - "The total number of graph vertices is not correct" ); - - CV_TS_SEQ_CHECK_CONDITION( graph->edges->total >= graph->edges->active_count && - (graph->edges->total == 0 || graph->edges->total >= prev_edge_total), - "The total number of graph vertices is not correct" ); - - // CvGraph and simple graph do not necessary have the same "total" (active & free) number, - // so pass "graph->total" (or "graph->edges->total") to skip that check - test_seq_block_consistence( struct_idx, (CvSeq*)graph, graph->total ); - test_seq_block_consistence( struct_idx, (CvSeq*)graph->edges, graph->edges->total ); - update_progressbar(); - } - - return 0; -} - - -void Core_GraphTest::run( int ) -{ - try - { - RNG& rng = ts->get_rng(); - int i, k; - double t; - - clear(); - test_progress = -1; - - simple_struct.resize(struct_count, 0); - cxcore_struct.resize(struct_count, 0); - - for( gen = 0; gen < generations; gen++ ) - { - struct_idx = iter = -1; - t = cvtest::randReal(rng)*(max_log_storage_block_size - min_log_storage_block_size) + min_log_storage_block_size; - int block_size = cvRound( exp(t * CV_LOG2) ); - block_size = MAX(block_size, (int)(sizeof(CvGraph) + sizeof(CvMemBlock) + sizeof(CvSeqBlock))); - - storage.reset(cvCreateMemStorage(block_size)); - - for( i = 0; i < struct_count; i++ ) - { - int pure_elem_size[2], elem_size[2]; - int is_oriented = (gen + i) % 2; - for( k = 0; k < 2; k++ ) - { - t = cvtest::randReal(rng)*(max_log_elem_size - min_log_elem_size) + min_log_elem_size; - int pe = cvRound( exp(t * CV_LOG2) ) - 1; // pure_elem_size==0 does also make sense - int delta = k == 0 ? sizeof(CvGraphVtx) : sizeof(CvGraphEdge); - int e = pe + delta; - e = (e + sizeof(size_t) - 1) & ~(sizeof(size_t)-1); - e = MIN( e, (int)(storage->block_size - sizeof(CvMemBlock) - - sizeof(CvSeqBlock) - sizeof(void*)) ); - pe = MIN(pe, e - delta); - pure_elem_size[k] = pe; - elem_size[k] = e; - } - - cvTsReleaseSimpleGraph( (CvTsSimpleGraph**)&simple_struct[i] ); - simple_struct[i] = cvTsCreateSimpleGraph( max_struct_size/4, pure_elem_size[0], - pure_elem_size[1], is_oriented ); - cxcore_struct[i] = cvCreateGraph( is_oriented ? CV_ORIENTED_GRAPH : CV_GRAPH, - sizeof(CvGraph), elem_size[0], elem_size[1], - storage ); - } - - if( test_graph_ops( iterations*10 ) < 0 ) - return; - - storage.release(); - } - } - catch(const int &) - { - } -} - - -//////////// graph scan test ////////////// - -class Core_GraphScanTest : public Core_DynStructBaseTest -{ -public: - Core_GraphScanTest(); - void run( int ); - -protected: - //int test_seq_block_consistence( int struct_idx ); - int create_random_graph( int ); -}; - - -Core_GraphScanTest::Core_GraphScanTest() -{ - iterations = 100; - struct_count = 1; -} - - -int Core_GraphScanTest::create_random_graph( int _struct_idx ) -{ - RNG& rng = ts->get_rng(); - int is_oriented = cvtest::randInt(rng) % 2; - int i, vtx_count = cvtest::randInt(rng) % max_struct_size; - int edge_count = cvtest::randInt(rng) % MAX(vtx_count*20, 1); - CvGraph* graph; - - struct_idx = _struct_idx; - cxcore_struct[_struct_idx] = graph = - cvCreateGraph(is_oriented ? CV_ORIENTED_GRAPH : CV_GRAPH, - sizeof(CvGraph), sizeof(CvGraphVtx), - sizeof(CvGraphEdge), storage ); - - for( i = 0; i < vtx_count; i++ ) - cvGraphAddVtx( graph ); - - CV_Assert( graph->active_count == vtx_count ); - - for( i = 0; i < edge_count; i++ ) - { - int j = cvtest::randInt(rng) % vtx_count; - int k = cvtest::randInt(rng) % vtx_count; - - if( j != k ) - cvGraphAddEdge( graph, j, k ); - } - - CV_Assert( graph->active_count == vtx_count && graph->edges->active_count <= edge_count ); - - return 0; -} - - -void Core_GraphScanTest::run( int ) -{ - CvGraphScanner* scanner = 0; - try - { - RNG& rng = ts->get_rng(); - vector vtx_mask, edge_mask; - double t; - int i; - - clear(); - test_progress = -1; - - cxcore_struct.resize(struct_count, 0); - - for( gen = 0; gen < generations; gen++ ) - { - struct_idx = iter = -1; - t = cvtest::randReal(rng)*(max_log_storage_block_size - min_log_storage_block_size) + min_log_storage_block_size; - int storage_blocksize = cvRound( exp(t * CV_LOG2) ); - storage_blocksize = MAX(storage_blocksize, (int)(sizeof(CvGraph) + sizeof(CvMemBlock) + sizeof(CvSeqBlock))); - storage_blocksize = MAX(storage_blocksize, (int)(sizeof(CvGraphEdge) + sizeof(CvMemBlock) + sizeof(CvSeqBlock))); - storage_blocksize = MAX(storage_blocksize, (int)(sizeof(CvGraphVtx) + sizeof(CvMemBlock) + sizeof(CvSeqBlock))); - storage.reset(cvCreateMemStorage(storage_blocksize)); - - if( gen == 0 ) - { - // special regression test for one sample graph. - // !!! ATTENTION !!! The test relies on the particular order of the inserted edges - // (LIFO: the edge inserted last goes first in the list of incident edges). - // if it is changed, the test will have to be modified. - - int vtx_count = -1, edge_count = 0, edges[][3] = - { - {0,4,'f'}, {0,1,'t'}, {1,4,'t'}, {1,2,'t'}, {2,3,'t'}, {4,3,'c'}, {3,1,'b'}, - {5,7,'t'}, {7,5,'b'}, {5,6,'t'}, {6,0,'c'}, {7,6,'c'}, {6,4,'c'}, {-1,-1,0} - }; - - CvGraph* graph = cvCreateGraph( CV_ORIENTED_GRAPH, sizeof(CvGraph), - sizeof(CvGraphVtx), sizeof(CvGraphEdge), storage ); - - for( i = 0; edges[i][0] >= 0; i++ ) - { - vtx_count = MAX( vtx_count, edges[i][0] ); - vtx_count = MAX( vtx_count, edges[i][1] ); - } - vtx_count++; - - for( i = 0; i < vtx_count; i++ ) - cvGraphAddVtx( graph ); - - for( i = 0; edges[i][0] >= 0; i++ ) - { - CvGraphEdge* edge; - cvGraphAddEdge( graph, edges[i][0], edges[i][1], 0, &edge ); - edge->weight = (float)edges[i][2]; - } - - edge_count = i; - scanner = cvCreateGraphScanner( graph, 0, CV_GRAPH_ALL_ITEMS ); - - for(;;) - { - int code, a = -1, b = -1; - const char* event = ""; - code = cvNextGraphItem( scanner ); - - switch( code ) - { - case CV_GRAPH_VERTEX: - event = "Vertex"; - vtx_count--; - a = cvGraphVtxIdx( graph, scanner->vtx ); - break; - case CV_GRAPH_TREE_EDGE: - event = "Tree Edge"; - edge_count--; - CV_TS_SEQ_CHECK_CONDITION( scanner->edge->weight == (float)'t', - "Invalid edge type" ); - a = cvGraphVtxIdx( graph, scanner->vtx ); - b = cvGraphVtxIdx( graph, scanner->dst ); - break; - case CV_GRAPH_BACK_EDGE: - event = "Back Edge"; - edge_count--; - CV_TS_SEQ_CHECK_CONDITION( scanner->edge->weight == (float)'b', - "Invalid edge type" ); - a = cvGraphVtxIdx( graph, scanner->vtx ); - b = cvGraphVtxIdx( graph, scanner->dst ); - break; - case CV_GRAPH_CROSS_EDGE: - event = "Cross Edge"; - edge_count--; - CV_TS_SEQ_CHECK_CONDITION( scanner->edge->weight == (float)'c', - "Invalid edge type" ); - a = cvGraphVtxIdx( graph, scanner->vtx ); - b = cvGraphVtxIdx( graph, scanner->dst ); - break; - case CV_GRAPH_FORWARD_EDGE: - event = "Forward Edge"; - edge_count--; - CV_TS_SEQ_CHECK_CONDITION( scanner->edge->weight == (float)'f', - "Invalid edge type" ); - a = cvGraphVtxIdx( graph, scanner->vtx ); - b = cvGraphVtxIdx( graph, scanner->dst ); - break; - case CV_GRAPH_BACKTRACKING: - event = "Backtracking"; - a = cvGraphVtxIdx( graph, scanner->vtx ); - break; - case CV_GRAPH_NEW_TREE: - event = "New search tree"; - break; - case CV_GRAPH_OVER: - event = "End of procedure"; - break; - default: - CV_TS_SEQ_CHECK_CONDITION( 0, "Invalid code appeared during graph scan" ); - } - - ts->printf( cvtest::TS::LOG, "%s", event ); - if( a >= 0 ) - { - if( b >= 0 ) - ts->printf( cvtest::TS::LOG, ": (%d,%d)", a, b ); - else - ts->printf( cvtest::TS::LOG, ": %d", a ); - } - - ts->printf( cvtest::TS::LOG, "\n" ); - - if( code < 0 ) - break; - } - - CV_TS_SEQ_CHECK_CONDITION( vtx_count == 0 && edge_count == 0, - "Not every vertex/edge has been visited" ); - update_progressbar(); - - cvReleaseGraphScanner( &scanner ); - } - - // for a random graph the test just checks that every graph vertex and - // every edge is vitisted during the scan - for( iter = 0; iter < iterations; iter++ ) - { - create_random_graph(0); - CvGraph* graph = (CvGraph*)cxcore_struct[0]; - - // iterate twice to check that scanner doesn't damage the graph - for( i = 0; i < 2; i++ ) - { - CvGraphVtx* start_vtx = cvtest::randInt(rng) % 2 || graph->active_count == 0 ? 0 : - cvGetGraphVtx( graph, cvtest::randInt(rng) % graph->active_count ); - - scanner = cvCreateGraphScanner( graph, start_vtx, CV_GRAPH_ALL_ITEMS ); - - vtx_mask.resize(0); - vtx_mask.resize(graph->active_count, 0); - edge_mask.resize(0); - edge_mask.resize(graph->edges->active_count, 0); - - for(;;) - { - int code = cvNextGraphItem( scanner ); - - if( code == CV_GRAPH_OVER ) - break; - else if( code & CV_GRAPH_ANY_EDGE ) - { - int edge_idx = scanner->edge->flags & CV_SET_ELEM_IDX_MASK; - - CV_TS_SEQ_CHECK_CONDITION( edge_idx < graph->edges->active_count && - edge_mask[edge_idx] == 0, - "The edge is not found or visited for the second time" ); - edge_mask[edge_idx] = 1; - } - else if( code & CV_GRAPH_VERTEX ) - { - int vtx_idx = scanner->vtx->flags & CV_SET_ELEM_IDX_MASK; - - CV_TS_SEQ_CHECK_CONDITION( vtx_idx < graph->active_count && - vtx_mask[vtx_idx] == 0, - "The vtx is not found or visited for the second time" ); - vtx_mask[vtx_idx] = 1; - } - } - - cvReleaseGraphScanner( &scanner ); - - CV_TS_SEQ_CHECK_CONDITION( cvtest::norm(Mat(vtx_mask),NORM_L1) == graph->active_count && - cvtest::norm(Mat(edge_mask),NORM_L1) == graph->edges->active_count, - "Some vertices or edges have not been visited" ); - update_progressbar(); - } - cvClearMemStorage( storage ); - } - - storage.release(); - } - } - catch(const int &) - { - } -} - - -TEST(Core_DS_Seq, basic_operations) { Core_SeqBaseTest test; test.safe_run(); } -TEST(Core_DS_Seq, sort_invert) { Core_SeqSortInvTest test; test.safe_run(); } -TEST(Core_DS_Set, basic_operations) { Core_SetTest test; test.safe_run(); } -TEST(Core_DS_Graph, basic_operations) { Core_GraphTest test; test.safe_run(); } -TEST(Core_DS_Graph, scan) { Core_GraphScanTest test; test.safe_run(); } - -}} // namespace diff --git a/modules/core/test/test_dxt.cpp b/modules/core/test/test_dxt.cpp index 2e01f8014d..9bcefbfce9 100644 --- a/modules/core/test/test_dxt.cpp +++ b/modules/core/test/test_dxt.cpp @@ -2,7 +2,8 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" -#include "opencv2/core/core_c.h" + +#define CV_TEST_DXT_MUL_CONJ 8 /**< conjugate the second argument of cvMulSpectrums */ namespace opencv_test { namespace { @@ -450,7 +451,7 @@ static void mulComplex( const Mat& src1, const Mat& src2, Mat& dst, int flags ) const float* b = src2_->ptr(i); float* c = dst.ptr(i); - if( !(flags & CV_DXT_MUL_CONJ) ) + if( !(flags & CV_TEST_DXT_MUL_CONJ) ) for( j = 0; j < cols; j += 2 ) { double re = (double)a[j]*(double)b[j] - (double)a[j+1]*(double)b[j+1]; @@ -475,7 +476,7 @@ static void mulComplex( const Mat& src1, const Mat& src2, Mat& dst, int flags ) const double* b = src2_->ptr(i); double* c = dst.ptr(i); - if( !(flags & CV_DXT_MUL_CONJ) ) + if( !(flags & CV_TEST_DXT_MUL_CONJ) ) for( j = 0; j < cols; j += 2 ) { double re = a[j]*b[j] - a[j+1]*b[j+1]; @@ -547,22 +548,22 @@ void CxCore_DXTBaseTest::get_test_array_types_and_sizes( int test_case_idx, Size size; Base::get_test_array_types_and_sizes( test_case_idx, sizes, types ); - flags = bits & (CV_DXT_INVERSE | CV_DXT_SCALE | CV_DXT_ROWS | CV_DXT_MUL_CONJ); + flags = bits & (cv::DFT_INVERSE | cv::DFT_SCALE | cv::DFT_ROWS | CV_TEST_DXT_MUL_CONJ); if( spectrum_mode ) - flags &= ~CV_DXT_INVERSE; + flags &= ~cv::DFT_INVERSE; types[TEMP][0] = types[TEMP][1] = types[INPUT][0] = types[OUTPUT][0] = CV_MAKETYPE(depth, cn); size = sizes[INPUT][0]; temp_dst = false; - if( flags & CV_DXT_ROWS && (bits&1024) ) + if( flags & cv::DFT_ROWS && (bits&1024) ) { if( bits&16 ) size.width = 1; else size.height = 1; - flags &= ~CV_DXT_ROWS; + flags &= ~cv::DFT_ROWS; } const int P2_MIN_SIZE = 32; @@ -579,12 +580,12 @@ void CxCore_DXTBaseTest::get_test_array_types_and_sizes( int test_case_idx, if( size.width > 1 && (size.width&1) != 0 ) size.width = (size.width + 1) & -2; - if( size.height > 1 && (size.height&1) != 0 && !(flags & CV_DXT_ROWS) ) + if( size.height > 1 && (size.height&1) != 0 && !(flags & cv::DFT_ROWS) ) size.height = (size.height + 1) & -2; } sizes[INPUT][0] = sizes[OUTPUT][0] = size; - sizes[TEMP][0] = sizes[TEMP][1] = cvSize(0,0); + sizes[TEMP][0] = sizes[TEMP][1] = cv::Size(0,0); if( spectrum_mode ) { @@ -600,9 +601,9 @@ void CxCore_DXTBaseTest::get_test_array_types_and_sizes( int test_case_idx, { types[TEMP][0] = CV_MAKETYPE(depth, 2); // CV_??FC2 sizes[TEMP][0] = size; - size = cvSize(size.width/2+1, size.height); + size = cv::Size(size.width/2+1, size.height); - if( flags & CV_DXT_INVERSE ) + if( flags & cv::DFT_INVERSE ) { if( cn == 2 ) { @@ -692,10 +693,10 @@ void CxCore_DFTTest::run_func() Mat& dst = temp_dst ? test_mat[TEMP][1] : test_mat[OUTPUT][0]; const Mat& src = inplace ? dst : test_mat[INPUT][0]; - if(!(flags & CV_DXT_INVERSE)) + if(!(flags & cv::DFT_INVERSE)) cv::dft( src, dst, flags ); else - cv::idft(src, dst, flags & ~CV_DXT_INVERSE); + cv::idft(src, dst, flags & ~cv::DFT_INVERSE); } @@ -712,7 +713,7 @@ void CxCore_DFTTest::prepare_to_validation( int /*test_case_idx*/ ) { tmp_src = &test_mat[TEMP][0]; - if( !(flags & CV_DXT_INVERSE ) ) + if( !(flags & cv::DFT_INVERSE ) ) { Mat& cvdft_dst = test_mat[TEMP][1]; convertFromCCS( cvdft_dst, cvdft_dst, @@ -727,7 +728,7 @@ void CxCore_DFTTest::prepare_to_validation( int /*test_case_idx*/ ) } } - if( src.rows == 1 || (src.cols == 1 && !(flags & CV_DXT_ROWS)) ) + if( src.rows == 1 || (src.cols == 1 && !(flags & cv::DFT_ROWS)) ) DFT_1D( *tmp_src, *tmp_dst, flags ); else DFT_2D( *tmp_src, *tmp_dst, flags ); @@ -757,10 +758,10 @@ void CxCore_DCTTest::run_func() Mat& dst = test_mat[OUTPUT][0]; const Mat& src = inplace ? dst : test_mat[INPUT][0]; - if(!(flags & CV_DXT_INVERSE)) + if(!(flags & cv::DFT_INVERSE)) cv::dct( src, dst, flags ); else - cv::idct( src, dst, flags & ~CV_DXT_INVERSE); + cv::idct( src, dst, flags & ~cv::DFT_INVERSE); } @@ -769,7 +770,7 @@ void CxCore_DCTTest::prepare_to_validation( int /*test_case_idx*/ ) const Mat& src = test_mat[INPUT][0]; Mat& dst = test_mat[REF_OUTPUT][0]; - if( src.rows == 1 || (src.cols == 1 && !(flags & CV_DXT_ROWS)) ) + if( src.rows == 1 || (src.cols == 1 && !(flags & cv::DFT_ROWS)) ) DCT_1D( src, dst, flags ); else DCT_2D( src, dst, flags ); @@ -820,7 +821,7 @@ void CxCore_MulSpectrumsTest::run_func() src1 = &dst; } - cv::mulSpectrums( *src1, *src2, dst, flags, (flags & CV_DXT_MUL_CONJ) != 0 ); + cv::mulSpectrums( *src1, *src2, dst, flags, (flags & CV_TEST_DXT_MUL_CONJ) != 0 ); } diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 60d891c263..77f918df1f 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -2,7 +2,6 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. #include "test_precomp.hpp" -#include "opencv2/core/core_c.h" #include namespace opencv_test { namespace { @@ -94,7 +93,6 @@ protected: RNG& rng = ts->get_rng(); RNG rng0; int progress = 0; - MemStorage storage(cvCreateMemStorage(0)); const char * suffixs[3] = {".yml", ".xml", ".json" }; test_case_count = 6; @@ -103,8 +101,6 @@ protected: ts->update_context( this, idx, false ); progress = update_progress( progress, idx, test_case_count, 0 ); - cvClearMemStorage(storage); - bool mem = (idx % test_case_count) >= (test_case_count >> 1); string filename = tempfile(suffixs[idx % (test_case_count >> 1)]); @@ -611,14 +607,14 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo { /* init */ /* a normal mat */ - _2d_out = cv::Mat(10, 20, CV_8UC3, cvScalar(1U, 2U, 127U)); + _2d_out = cv::Mat(10, 20, CV_8UC3, cv::Scalar(1U, 2U, 127U)); for (int i = 0; i < _2d_out.rows; ++i) for (int j = 0; j < _2d_out.cols; ++j) _2d_out.at(i, j)[1] = (i + j) % 256; /* a 4d mat */ const int Size[] = {4, 4, 4, 4}; - cv::Mat _4d(4, Size, CV_64FC4, cvScalar(0.888, 0.111, 0.666, 0.444)); + cv::Mat _4d(4, Size, CV_64FC4, cv::Scalar(0.888, 0.111, 0.666, 0.444)); const cv::Range ranges[] = { cv::Range(0, 2), cv::Range(0, 2), diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 9f308b9715..e8da583fab 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -11,7 +11,6 @@ #include "opencv2/core/cuda.hpp" #include "opencv2/core/bindings_utils.hpp" -#include "opencv2/core/core_c.h" namespace opencv_test { namespace { @@ -305,9 +304,6 @@ void Core_ReduceTest::run( int ) ts->set_failed_test_info( code ); } - -#define CHECK_C - TEST(Core_PCA, accuracy) { const Size sz(200, 500); @@ -343,11 +339,6 @@ TEST(Core_PCA, accuracy) Mat subEval( maxComponents, 1, eval.type(), eval.ptr() ), subEvec( maxComponents, evec.cols, evec.type(), evec.ptr() ); -#ifdef CHECK_C - Mat prjTestPoints, backPrjTestPoints, cPoints = rPoints.t(), cTestPoints = rTestPoints.t(); - CvMat _points, _testPoints, _avg, _eval, _evec, _prjTestPoints, _backPrjTestPoints; -#endif - // check eigen() double eigenEps = 1e-4; double err; @@ -436,45 +427,6 @@ TEST(Core_PCA, accuracy) err = cvtest::norm(cPCA.backProject(rvPrjTestPoints), rBackPrjTestPoints.t(), NORM_L2 | NORM_RELATIVE); ASSERT_LE(err, diffBackPrjEps) << "bad accuracy of backProject() (PCA::DATA_AS_COL); retainedVariance=" << retainedVariance; -#ifdef CHECK_C - // 4. check C PCA & ROW - _points = cvMat(rPoints); - _testPoints = cvMat(rTestPoints); - _avg = cvMat(avg); - _eval = cvMat(eval); - _evec = cvMat(evec); - prjTestPoints.create(rTestPoints.rows, maxComponents, rTestPoints.type() ); - backPrjTestPoints.create(rPoints.size(), rPoints.type() ); - _prjTestPoints = cvMat(prjTestPoints); - _backPrjTestPoints = cvMat(backPrjTestPoints); - - cvCalcPCA( &_points, &_avg, &_eval, &_evec, PCA::DATA_AS_ROW ); - cvProjectPCA( &_testPoints, &_avg, &_evec, &_prjTestPoints ); - cvBackProjectPCA( &_prjTestPoints, &_avg, &_evec, &_backPrjTestPoints ); - - err = cvtest::norm(prjTestPoints, rPrjTestPoints, NORM_L2 | NORM_RELATIVE); - ASSERT_LE(err, diffPrjEps) << "bad accuracy of cvProjectPCA() (PCA::DATA_AS_ROW)"; - err = cvtest::norm(backPrjTestPoints, rBackPrjTestPoints, NORM_L2 | NORM_RELATIVE); - ASSERT_LE(err, diffBackPrjEps) << "bad accuracy of cvBackProjectPCA() (PCA::DATA_AS_ROW)"; - - // 5. check C PCA & COL - _points = cvMat(cPoints); - _testPoints = cvMat(cTestPoints); - avg = avg.t(); _avg = cvMat(avg); - eval = eval.t(); _eval = cvMat(eval); - evec = evec.t(); _evec = cvMat(evec); - prjTestPoints = prjTestPoints.t(); _prjTestPoints = cvMat(prjTestPoints); - backPrjTestPoints = backPrjTestPoints.t(); _backPrjTestPoints = cvMat(backPrjTestPoints); - - cvCalcPCA( &_points, &_avg, &_eval, &_evec, PCA::DATA_AS_COL ); - cvProjectPCA( &_testPoints, &_avg, &_evec, &_prjTestPoints ); - cvBackProjectPCA( &_prjTestPoints, &_avg, &_evec, &_backPrjTestPoints ); - - err = cvtest::norm(cv::abs(prjTestPoints), cv::abs(rPrjTestPoints.t()), NORM_L2 | NORM_RELATIVE); - ASSERT_LE(err, diffPrjEps) << "bad accuracy of cvProjectPCA() (PCA::DATA_AS_COL)"; - err = cvtest::norm(backPrjTestPoints, rBackPrjTestPoints.t(), NORM_L2 | NORM_RELATIVE); - ASSERT_LE(err, diffBackPrjEps) << "bad accuracy of cvBackProjectPCA() (PCA::DATA_AS_COL)"; -#endif // Test read and write const std::string filename = cv::tempfile("PCA_store.yml"); FileStorage fs( filename, FileStorage::WRITE ); @@ -550,13 +502,6 @@ static double getValue(SparseMat& M, const int* idx, RNG& rng) return !ptr ? 0 : M.type() == CV_32F ? *(float*)ptr : M.type() == CV_64F ? *(double*)ptr : 0; } -static double getValue(const CvSparseMat* M, const int* idx) -{ - int type = 0; - const uchar* ptr = cvPtrND(M, idx, &type, 0); - return !ptr ? 0 : type == CV_32F ? *(float*)ptr : type == CV_64F ? *(double*)ptr : 0; -} - static void eraseValue(SparseMat& M, const int* idx, RNG& rng) { int d = M.dims(); @@ -576,11 +521,6 @@ static void eraseValue(SparseMat& M, const int* idx, RNG& rng) M.erase(idx, phv); } -static void eraseValue(CvSparseMat* M, const int* idx) -{ - cvClearND(M, idx); -} - static void setValue(SparseMat& M, const int* idx, double value, RNG& rng) { int d = M.dims(); @@ -646,39 +586,6 @@ void Core_ArrayOpTest::run( int /* start_from */) { int errcount = 0; - // dense matrix operations - { - int sz3[] = {5, 10, 15}; - MatND A(3, sz3, CV_32F), B(3, sz3, CV_16SC4); - CvMatND matA = cvMatND(A), matB = cvMatND(B); - RNG rng; - rng.fill(A, RNG::UNIFORM, Scalar::all(-10), Scalar::all(10)); - rng.fill(B, RNG::UNIFORM, Scalar::all(-10), Scalar::all(10)); - - int idx0[] = {3,4,5}, idx1[] = {0, 9, 7}; - float val0 = 130; - Scalar val1(-1000, 30, 3, 8); - cvSetRealND(&matA, idx0, val0); - cvSetReal3D(&matA, idx1[0], idx1[1], idx1[2], -val0); - cvSetND(&matB, idx0, cvScalar(val1)); - cvSet3D(&matB, idx1[0], idx1[1], idx1[2], cvScalar(-val1)); - Ptr matC(cvCloneMatND(&matB)); - - if( A.at(idx0[0], idx0[1], idx0[2]) != val0 || - A.at(idx1[0], idx1[1], idx1[2]) != -val0 || - cvGetReal3D(&matA, idx0[0], idx0[1], idx0[2]) != val0 || - cvGetRealND(&matA, idx1) != -val0 || - - Scalar(B.at(idx0[0], idx0[1], idx0[2])) != val1 || - Scalar(B.at(idx1[0], idx1[1], idx1[2])) != -val1 || - Scalar(cvGet3D(matC, idx0[0], idx0[1], idx0[2])) != val1 || - Scalar(cvGetND(matC, idx1)) != -val1 ) - { - ts->printf(cvtest::TS::LOG, "one of cvSetReal3D, cvSetRealND, cvSet3D, cvSetND " - "or the corresponding *Get* functions is not correct\n"); - errcount++; - } - } // test cv::Mat::forEach { const int dims[3] = { 101, 107, 7 }; @@ -856,7 +763,6 @@ void Core_ArrayOpTest::run( int /* start_from */) } } - Ptr M2(cvCreateSparseMat(M)); MatND Md; M.copyTo(Md); SparseMat M3; SparseMat(Md).convertTo(M3, Md.type(), 2); @@ -890,7 +796,7 @@ void Core_ArrayOpTest::run( int /* start_from */) for( i = 0; i < n; i++ ) { - double val1, val2, val3, val0; + double val1, val3, val0; if(i < nz0) { sidx = all_idxs[i]; @@ -905,20 +811,19 @@ void Core_ArrayOpTest::run( int /* start_from */) val0 = M0[sidx]; } val1 = getValue(M, idx, rng); - val2 = getValue(M2, idx); val3 = getValue(M3, idx, rng); - if( val1 != val0 || val2 != val0 || fabs(val3 - val0*2) > fabs(val0*2)*FLT_EPSILON ) + if( val1 != val0 || fabs(val3 - val0*2) > fabs(val0*2)*FLT_EPSILON ) { errcount++; - ts->printf(cvtest::TS::LOG, "SparseMat M[%s] = %g/%g/%g (while it should be %g)\n", sidx.c_str(), val1, val2, val3, val0 ); + ts->printf(cvtest::TS::LOG, "SparseMat M[%s] = %g/%g (while it should be %g)\n", sidx.c_str(), val1, val3, val0 ); break; } } for( i = 0; i < n; i++ ) { - double val1, val2; + double val1; if(i < nz0) { sidx = all_idxs[i]; @@ -931,13 +836,11 @@ void Core_ArrayOpTest::run( int /* start_from */) sidx = idx2string(idx, dims); } eraseValue(M, idx, rng); - eraseValue(M2, idx); val1 = getValue(M, idx, rng); - val2 = getValue(M2, idx); - if( val1 != 0 || val2 != 0 ) + if( val1 != 0 ) { errcount++; - ts->printf(cvtest::TS::LOG, "SparseMat: after deleting M[%s], it is =%g/%g (while it should be 0)\n", sidx.c_str(), val1, val2 ); + ts->printf(cvtest::TS::LOG, "SparseMat: after deleting M[%s], it is = %g (while it should be 0)\n", sidx.c_str(), val1 ); break; } } @@ -2493,21 +2396,6 @@ TEST(Mat1D, basic) EXPECT_NO_THROW(m1.convertTo(row2D, CV_32FC1)); } - { - SCOPED_TRACE("CvMat"); - CvMat c_mat = cvMat(m1); - EXPECT_EQ(100, c_mat.cols); - EXPECT_EQ(1, c_mat.rows); - } - - { - SCOPED_TRACE("CvMatND"); - CvMatND c_mat = cvMatND(m1); - EXPECT_EQ(2, c_mat.dims); - EXPECT_EQ(1, c_mat.dim[0].size); - EXPECT_EQ(100, c_mat.dim[1].size); - } - { SCOPED_TRACE("minMaxLoc"); Point pt; diff --git a/modules/core/test/test_math.cpp b/modules/core/test/test_math.cpp index 9cace09244..5dd891f85f 100644 --- a/modules/core/test/test_math.cpp +++ b/modules/core/test/test_math.cpp @@ -10,7 +10,6 @@ #include #include #include "opencv2/core/softfloat.hpp" -#include "opencv2/core/core_c.h" #include "opencv2/core/hal/intrin.hpp" namespace opencv_test { namespace { @@ -604,24 +603,24 @@ void Core_GEMMTest::get_test_array_types_and_sizes( int test_case_idx, vector(0,0) = cvRealScalar(sqrt(cvtest::crossCorr(test_mat[TEMP][0], test_mat[TEMP][1]))); + test_mat[REF_OUTPUT][0].at(0,0) = cv::Scalar(sqrt(cvtest::crossCorr(test_mat[TEMP][0], test_mat[TEMP][1]))); } @@ -1186,8 +1185,8 @@ void Core_DetTest::get_test_array_types_and_sizes( int test_case_idx, vectorrows, N1 = a->cols, Nm = MIN(N, N1), step = a->step/sizeof(double); - int M = b ? b->cols : 0, b_step = b ? b->step/sizeof(double) : 0; - int x_step = x ? x->step/sizeof(double) : 0; - double *a0 = a->data.db, *b0 = b ? b->data.db : 0; - double *x0 = x ? x->data.db : 0; + CV_Assert(a.type() == CV_64FC1); + int i, j, k, N = a.rows, N1 = a.cols, Nm = MIN(N, N1), step = (int)(a.step/sizeof(double)); + double *a0 = a.ptr(); double t, det = 1.; - CV_Assert( CV_MAT_TYPE(a->type) == CV_64FC1 && - (!b || CV_ARE_TYPES_EQ(a,b)) && (!x || CV_ARE_TYPES_EQ(a,x))); for( i = 0; i < Nm; i++ ) { double max_val = fabs(a0[i*step + i]); - double *a1, *a2, *b1 = 0, *b2 = 0; + double *a1, *a2; k = i; for( j = i+1; j < N; j++ ) @@ -1245,57 +1240,27 @@ static double cvTsLU( CvMat* a, CvMat* b=NULL, CvMat* x=NULL, int* rank=0 ) { for( j = i; j < N1; j++ ) CV_SWAP( a0[i*step + j], a0[k*step + j], t ); - - for( j = 0; j < M; j++ ) - CV_SWAP( b0[i*b_step + j], b0[k*b_step + j], t ); det = -det; } if( max_val == 0 ) { - if( rank ) - *rank = i; return 0.; } a1 = a0 + i*step; a2 = a1 + step; - b1 = b0 + i*b_step; - b2 = b1 + b_step; - for( j = i+1; j < N; j++, a2 += step, b2 += b_step ) + for( j = i+1; j < N; j++, a2 += step ) { t = a2[i]/a1[i]; for( k = i+1; k < N1; k++ ) a2[k] -= t*a1[k]; - - for( k = 0; k < M; k++ ) - b2[k] -= t*b1[k]; } det *= a1[i]; } - if( x ) - { - CV_Assert( b ); - - for( i = N-1; i >= 0; i-- ) - { - double* a1 = a0 + i*step; - double* b1 = b0 + i*b_step; - for( j = 0; j < M; j++ ) - { - t = b1[j]; - for( k = i+1; k < N1; k++ ) - t -= a1[k]*x0[k*x_step + j]; - x0[i*x_step + j] = t/a1[i]; - } - } - } - - if( rank ) - *rank = i; return det; } @@ -1303,8 +1268,7 @@ static double cvTsLU( CvMat* a, CvMat* b=NULL, CvMat* x=NULL, int* rank=0 ) void Core_DetTest::prepare_to_validation( int ) { test_mat[INPUT][0].convertTo(test_mat[TEMP][0], test_mat[TEMP][0].type()); - CvMat temp0 = cvMat(test_mat[TEMP][0]); - test_mat[REF_OUTPUT][0].at(0,0) = cvRealScalar(cvTsLU(&temp0, 0, 0)); + test_mat[REF_OUTPUT][0].at(0,0) = cv::Scalar(cvTsLU(test_mat[TEMP][0])); } @@ -1384,7 +1348,7 @@ int Core_InvertTest::prepare_test_case( int test_case_idx ) if( method == cv::DECOMP_CHOLESKY ) { cvtest::gemm( test_mat[INPUT][0], test_mat[INPUT][0], 1., - Mat(), 0., test_mat[TEMP][0], CV_GEMM_B_T ); + Mat(), 0., test_mat[TEMP][0], cv::GEMM_2_T ); cvtest::copy( test_mat[TEMP][0], test_mat[INPUT][0] ); } } @@ -1396,8 +1360,8 @@ int Core_InvertTest::prepare_test_case( int test_case_idx ) void Core_InvertTest::get_minmax_bounds( int /*i*/, int /*j*/, int /*type*/, Scalar& low, Scalar& high ) { - low = cvScalarAll(-1.); - high = cvScalarAll(1.); + low = cv::Scalar::all(-1.); + high = cv::Scalar::all(1.); } void Core_InvertTest::run_func() @@ -1493,9 +1457,9 @@ void Core_SolveTest::get_test_array_types_and_sizes( int test_case_idx, vector in_sz.height ) - in_sz = cvSize(in_sz.height, in_sz.width); + in_sz = cv::Size(in_sz.height, in_sz.width); Base::get_test_array_types_and_sizes( test_case_idx, sizes, types ); sizes[INPUT][0] = in_sz; int min_size = MIN( sizes[INPUT][0].width, sizes[INPUT][0].height ); @@ -1534,8 +1498,8 @@ int Core_SolveTest::prepare_test_case( int test_case_idx ) void Core_SolveTest::get_minmax_bounds( int /*i*/, int /*j*/, int /*type*/, Scalar& low, Scalar& high ) { - low = cvScalarAll(-1.); - high = cvScalarAll(1.); + low = cv::Scalar::all(-1.); + high = cv::Scalar::all(1.); } @@ -1564,8 +1528,7 @@ void Core_SolveTest::prepare_to_validation( int ) Mat& temp1 = test_mat[TEMP][1]; cvtest::convert(input, temp1, temp1.type()); dst = Scalar::all(0); - CvMat _temp1 = cvMat(temp1); - double det = cvTsLU( &_temp1, 0, 0 ); + double det = cvTsLU( temp1 ); dst0 = Scalar::all(det != 0); return; } @@ -1584,7 +1547,7 @@ void Core_SolveTest::prepare_to_validation( int ) cvtest::gemm( input, test_mat[TEMP][0], 1., test_mat[INPUT][1], -1., *pdst, 0 ); if( pdst != &dst ) - cvtest::gemm( input, *pdst, 1., Mat(), 0., dst, CV_GEMM_A_T ); + cvtest::gemm( input, *pdst, 1., Mat(), 0., dst, cv::GEMM_1_T ); dst0 = Scalar::all(0); } @@ -1624,7 +1587,6 @@ Core_SolvePolyTest::~Core_SolvePolyTest() {} void Core_SolvePolyTest::run( int ) { RNG& rng = cv::theRNG(); - int fig = 100; double range = 50; double err_eps = 1e-4; @@ -1673,10 +1635,8 @@ void Core_SolvePolyTest::run( int ) for (int j = 0; j < n + 1; ++j) a[j] = c[j].real(); - CvMat amat, umat; - cvInitMatHeader(&amat, n + 1, 1, CV_64FC1, &a[0]); - cvInitMatHeader(&umat, n, 1, CV_64FC2, &u[0]); - cvSolvePoly(&amat, &umat, maxiter, fig); + Mat amat(n + 1, 1, CV_64FC1, &a[0]), umat(n, 1, CV_64FC2, &u[0]); + cv::solvePoly(amat, umat, maxiter); for (int j = 0; j < n; ++j) ar[j] = complex_type(u[j * 2], u[j * 2 + 1]); @@ -1689,13 +1649,13 @@ void Core_SolvePolyTest::run( int ) { ar2.resize(n); cv::Mat _umat2(3, 1, CV_64F, &ar2[0]), umat2 = _umat2; - cvFlip(&amat, &amat, 0); + cv::flip(amat, amat, 0); int nr2; if( cubic_case == 0 ) - nr2 = cv::solveCubic(cv::cvarrToMat(&amat),umat2); + nr2 = cv::solveCubic(amat,umat2); else - nr2 = cv::solveCubic(cv::Mat_(cv::cvarrToMat(&amat)), umat2); - cvFlip(&amat, &amat, 0); + nr2 = cv::solveCubic(cv::Mat_(amat), umat2); + cv::flip(amat, amat, 0); if(nr2 > 0) std::sort(ar2.begin(), ar2.begin()+nr2, pred_double()); ar2.resize(nr2); @@ -2317,7 +2277,7 @@ TEST(CovariationMatrixVectorOfMat, accuracy) { unsigned int col_problem_size = 8, row_problem_size = 8, vector_size = 16; cv::Mat src(vector_size, col_problem_size * row_problem_size, CV_32F); - int singleMatFlags = CV_COVAR_ROWS; + int singleMatFlags = cv::COVAR_ROWS; cv::Mat gold; cv::Mat goldMean; @@ -2348,7 +2308,7 @@ TEST(CovariationMatrixVectorOfMatWithMean, accuracy) { unsigned int col_problem_size = 8, row_problem_size = 8, vector_size = 16; cv::Mat src(vector_size, col_problem_size * row_problem_size, CV_32F); - int singleMatFlags = CV_COVAR_ROWS | CV_COVAR_USE_AVG; + int singleMatFlags = cv::COVAR_ROWS | cv::COVAR_USE_AVG; cv::Mat gold; cv::randu(src,cv::Scalar(-128), cv::Scalar(128)); @@ -2448,7 +2408,7 @@ TEST(Core_Cholesky, accuracy64f) for (int i = 0; i < A.rows; i++) for (int j = i + 1; j < A.cols; j++) A.at(i, j) = 0.0; - EXPECT_LE(cvtest::norm(refA, A*A.t(), CV_RELATIVE_L2), FLT_EPSILON); + EXPECT_LE(cvtest::norm(refA, A*A.t(), cv::NORM_L2 | cv::NORM_RELATIVE), FLT_EPSILON); } TEST(Core_QR_Solver, accuracy64f) @@ -2468,7 +2428,7 @@ TEST(Core_QR_Solver, accuracy64f) //solve system with square matrix solve(A, B, solutionQR, DECOMP_QR); - EXPECT_LE(cvtest::norm(A*solutionQR, B, CV_RELATIVE_L2), FLT_EPSILON); + EXPECT_LE(cvtest::norm(A*solutionQR, B, cv::NORM_L2 | cv::NORM_RELATIVE), FLT_EPSILON); A = Mat(m, n, CV_64F); B = Mat(m, n, CV_64F); @@ -2477,13 +2437,13 @@ TEST(Core_QR_Solver, accuracy64f) //solve normal system solve(A, B, solutionQR, DECOMP_QR | DECOMP_NORMAL); - EXPECT_LE(cvtest::norm(A.t()*(A*solutionQR), A.t()*B, CV_RELATIVE_L2), FLT_EPSILON); + EXPECT_LE(cvtest::norm(A.t()*(A*solutionQR), A.t()*B, cv::NORM_L2 | cv::NORM_RELATIVE), FLT_EPSILON); //solve overdeterminated system as a least squares problem Mat solutionSVD; solve(A, B, solutionQR, DECOMP_QR); solve(A, B, solutionSVD, DECOMP_SVD); - EXPECT_LE(cvtest::norm(solutionQR, solutionSVD, CV_RELATIVE_L2), FLT_EPSILON); + EXPECT_LE(cvtest::norm(solutionQR, solutionSVD, cv::NORM_L2 | cv::NORM_RELATIVE), FLT_EPSILON); //solve system with singular matrix A = Mat(10, 10, CV_64F); diff --git a/modules/core/test/test_umat.cpp b/modules/core/test/test_umat.cpp index 332ce7bc1e..bf525c9c61 100644 --- a/modules/core/test/test_umat.cpp +++ b/modules/core/test/test_umat.cpp @@ -41,7 +41,6 @@ #include "test_precomp.hpp" #include "opencv2/ts/ocl_test.hpp" -#include "opencv2/core/core_c.h" using namespace opencv_test; using namespace testing; diff --git a/modules/imgproc/test/test_pc.cpp b/modules/imgproc/test/test_pc.cpp index 81b1ffc30d..40e290322a 100644 --- a/modules/imgproc/test/test_pc.cpp +++ b/modules/imgproc/test/test_pc.cpp @@ -42,7 +42,7 @@ #include "test_precomp.hpp" -#define CV_DXT_MUL_CONJ 8 +#define CV_TEST_DXT_MUL_CONJ 8 namespace opencv_test { namespace { @@ -183,7 +183,7 @@ void CV_DivSpectrumsTest::get_test_array_types_and_sizes( int test_case_idx, vec // Get the flag of the input. const int rand_int_flags = cvtest::randInt(rng); - flags = rand_int_flags & (CV_DXT_MUL_CONJ | DFT_ROWS); + flags = rand_int_flags & (CV_TEST_DXT_MUL_CONJ | DFT_ROWS); // Get input type. const int rand_int_type = cvtest::randInt(rng); @@ -354,7 +354,7 @@ static void div_complex_helper( const Mat& src1, const Mat& src2, Mat& dst, int std::pair result = divide_complex_numbers( src1_data[j], src1_data[j + 1], src2_data[j], src2_data[j + 1], - (flags & CV_DXT_MUL_CONJ) != 0 ); + (flags & CV_TEST_DXT_MUL_CONJ) != 0 ); dst_data[j] = (depth_t)result.first; dst_data[j + 1] = (depth_t)result.second; } @@ -411,7 +411,7 @@ void CV_DivSpectrumsTest::run_func() if ( cn == 1 ) { Mat &dst = test_mat[TEMP][2]; - cv::divSpectrums( src1, src2, dst, flags, (flags & CV_DXT_MUL_CONJ) != 0 ); + cv::divSpectrums( src1, src2, dst, flags, (flags & CV_TEST_DXT_MUL_CONJ) != 0 ); Mat &converted_dst = test_mat[OUTPUT][0]; convert_from_ccs( dst, dst, converted_dst, flags ); } @@ -419,7 +419,7 @@ void CV_DivSpectrumsTest::run_func() else { Mat &dst = test_mat[OUTPUT][0]; - cv::divSpectrums( src1, src2, dst, flags, (flags & CV_DXT_MUL_CONJ) != 0 ); + cv::divSpectrums( src1, src2, dst, flags, (flags & CV_TEST_DXT_MUL_CONJ) != 0 ); } }