From a4cbd3f81b0a520bdc007afe96ecda6cd471fb47 Mon Sep 17 00:00:00 2001 From: miloyip Date: Sun, 17 May 2015 20:44:11 +0800 Subject: [PATCH] Refactor SchemaValidator.TestSuite to use MemoryPoolAllocator explicitly --- test/unittest/schematest.cpp | 37 ++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 5cdb6ca..2a224bf 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -913,7 +913,8 @@ TEST(SchemaValidator, ValidateMetaSchema_UTF16) { free(json); } -class RemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider { +template +class RemoteSchemaDocumentProvider : public IGenericRemoteSchemaDocumentProvider { public: RemoteSchemaDocumentProvider() : documentAllocator_(), schemaAllocator_() { const char* filenames[kCount] = { @@ -932,9 +933,9 @@ public: ADD_FAILURE(); } else { - Document d(&documentAllocator_); + DocumentType d(&documentAllocator_); d.Parse(json); - sd_[i] = new SchemaDocument(d, 0, &schemaAllocator_); + sd_[i] = new SchemaDocumentType(d, 0, &schemaAllocator_); free(json); } }; @@ -945,7 +946,7 @@ public: delete sd_[i]; } - virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) { + virtual const SchemaDocumentType* GetRemoteDocument(const char* uri, SizeType length) { const char* uris[kCount] = { "http://localhost:1234/integer.json", "http://localhost:1234/subSchemas.json", @@ -960,13 +961,15 @@ public: } private: + typedef GenericDocument > DocumentType; + RemoteSchemaDocumentProvider(const RemoteSchemaDocumentProvider&); RemoteSchemaDocumentProvider& operator=(const RemoteSchemaDocumentProvider&); static const size_t kCount = 4; - SchemaDocument* sd_[kCount]; - Document::AllocatorType documentAllocator_; - SchemaDocument::AllocatorType schemaAllocator_; + SchemaDocumentType* sd_[kCount]; + typename DocumentType::AllocatorType documentAllocator_; + typename SchemaDocumentType::AllocatorType schemaAllocator_; }; TEST(SchemaValidator, TestSuite) { @@ -1007,12 +1010,17 @@ TEST(SchemaValidator, TestSuite) { unsigned testCount = 0; unsigned passCount = 0; - RemoteSchemaDocumentProvider provider; + typedef GenericSchemaDocument > SchemaDocumentType; + RemoteSchemaDocumentProvider provider; char documentBuffer[65536]; + char documentStackBuffer[65536]; char schemaBuffer[65536]; - Document::AllocatorType documentAllocator(documentBuffer, sizeof(documentBuffer)); - SchemaDocument::AllocatorType schemaAllocator(schemaBuffer, sizeof(schemaBuffer)); + char validatorBuffer[65536]; + MemoryPoolAllocator<> documentAllocator(documentBuffer, sizeof(documentBuffer)); + MemoryPoolAllocator<> documentStackAllocator(documentStackBuffer, sizeof(documentStackBuffer)); + MemoryPoolAllocator<> schemaAllocator(schemaBuffer, sizeof(schemaBuffer)); + MemoryPoolAllocator<> validatorAllocator(validatorBuffer, sizeof(validatorBuffer)); for (size_t i = 0; i < sizeof(filenames) / sizeof(filenames[0]); i++) { char filename[FILENAME_MAX]; @@ -1023,7 +1031,7 @@ TEST(SchemaValidator, TestSuite) { ADD_FAILURE(); } else { - Document d(&documentAllocator); + GenericDocument, MemoryPoolAllocator<>, MemoryPoolAllocator<> > d(&documentAllocator, 1024, &documentStackAllocator); d.Parse(json); if (d.HasParseError()) { printf("json test suite file %s has parse error", filename); @@ -1032,8 +1040,8 @@ TEST(SchemaValidator, TestSuite) { else { for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) { { - SchemaDocument schema((*schemaItr)["schema"], &provider, &schemaAllocator); - SchemaValidator validator(schema); + SchemaDocumentType schema((*schemaItr)["schema"], &provider, &schemaAllocator); + GenericSchemaValidator >, MemoryPoolAllocator<> > validator(schema, &validatorAllocator); const char* description1 = (*schemaItr)["description"].GetString(); const Value& tests = (*schemaItr)["tests"]; for (Value::ConstValueIterator testItr = tests.Begin(); testItr != tests.End(); ++testItr) { @@ -1050,9 +1058,10 @@ TEST(SchemaValidator, TestSuite) { passCount++; } } - //printf("%zu %zu\n", documentAllocator.Size(), schemaAllocator.Size()); + //printf("%zu %zu %zu\n", documentAllocator.Size(), schemaAllocator.Size(), validatorAllocator.Size()); } schemaAllocator.Clear(); + validatorAllocator.Clear(); } } }