Ejemplo n.º 1
0
void u0051_hidlookup__create_file__numbers(
	SG_context* pCtx,
	SG_pathname* pPath,
	const char* pszName,
	const SG_uint32 countLines
	)
{
	SG_pathname* pPathFile = NULL;
	SG_uint32 i;
	SG_file* pFile = NULL;

	VERIFY_ERR_CHECK(  SG_PATHNAME__ALLOC__PATHNAME_SZ(pCtx, &pPathFile, pPath, pszName)  );
	VERIFY_ERR_CHECK(  SG_file__open__pathname(pCtx, pPathFile, SG_FILE_CREATE_NEW | SG_FILE_RDWR, 0600, &pFile)  );
	for (i=0; i<countLines; i++)
	{
		char buf[64];

		VERIFY_ERR_CHECK(  SG_sprintf(pCtx, buf, sizeof(buf), "%d\n", i)  );
		VERIFY_ERR_CHECK(  SG_file__write(pCtx, pFile, (SG_uint32) strlen(buf), (SG_byte*) buf, NULL)  );
	}
	VERIFY_ERR_CHECK(  SG_file__close(pCtx, &pFile)  );

	SG_PATHNAME_NULLFREE(pCtx, pPathFile);

	return;
fail:
	SG_FILE_NULLCLOSE(pCtx, pFile);
	SG_PATHNAME_NULLFREE(pCtx, pPathFile);
}
void MyFn(test_W2771)(SG_context * pCtx)
{
	SG_vector_i64 * pVec = NULL;
	SG_int64 i64 = 0x123456789abcdefLL;
	SG_uint32 len;

	// allocate a vector with a hint of at least 1 cell.
	// (the minimum chunk size will override this, but we don't care.)
	VERIFY_ERR_CHECK(  SG_vector_i64__alloc(pCtx,&pVec,1)  );

	// verify size-in-use is 0.
	VERIFY_ERR_CHECK(  SG_vector_i64__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==0));

	// do a hard-reserve with zero-fill of the vector.
	// (we pick a size larger than any pre-defined chunk size.)
	VERIFY_ERR_CHECK(  SG_vector_i64__reserve(pCtx, pVec, 1000)  );
	VERIFY_ERR_CHECK(  SG_vector_i64__get(pCtx, pVec, 999, &i64)  );
	VERIFY_COND("get[1000]", (i64 == 0));

	VERIFY_ERR_CHECK(  SG_vector_i64__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==1000));

fail:
	SG_VECTOR_I64_NULLFREE(pCtx, pVec);
}
Ejemplo n.º 3
0
MyMain()
{
	SG_repo * pRepo = NULL;
	SG_pathname * pPathnameTempDir = NULL;

	TEMPLATE_MAIN_START;

	VERIFY_ERR_CHECK(  MyFn(create_repo)(pCtx,&pRepo)  );
	VERIFY_ERR_CHECK(  MyFn(create_tmp_src_dir)(pCtx,&pPathnameTempDir)  );

	BEGIN_TEST(  MyFn(create_some_blobs_from_bytes)(pCtx, pRepo)  );
	BEGIN_TEST(  MyFn(create_some_blobs_from_files)(pCtx, pRepo,pPathnameTempDir)  );

	BEGIN_TEST(  MyFn(create_zero_byte_blob)(pCtx, pRepo)  );

	//////////////////////////////////////////////////////////////////
	// TODO delete repo directory and everything we created under it.
	// TODO delete temp directory and everything we created under it.

	// fall-thru to common cleanup

fail:
	SG_REPO_NULLFREE(pCtx, pRepo);
	SG_PATHNAME_NULLFREE(pCtx, pPathnameTempDir);

	TEMPLATE_MAIN_END;
}
void u0026_jsonparser__test_jsonparser_vhash_2(SG_context * pCtx)
{
	SG_string* pstr1 = NULL;
	SG_string* pstr2 = NULL;
	SG_vhash* pvh = NULL;

	VERIFY_ERR_CHECK(  SG_STRING__ALLOC(pCtx, &pstr1)  );
	VERIFY_ERR_CHECK(  SG_STRING__ALLOC(pCtx, &pstr2)  );

	VERIFY_ERR_CHECK(  u0026_jsonparser__create_2(pCtx, pstr1)  );

	SG_VHASH__ALLOC__FROM_JSON__SZ(pCtx, &pvh, SG_string__sz(pstr1));
	VERIFY_COND("from_json", !SG_context__has_err(pCtx));
	VERIFY_COND("from_json", pvh);

	SG_context__err_reset(pCtx);
	SG_vhash__to_json(pCtx, pvh, pstr2);
	VERIFY_COND("from_json", !SG_context__has_err(pCtx));

	// TODO do some checks

fail:
	SG_STRING_NULLFREE(pCtx, pstr1);
	SG_STRING_NULLFREE(pCtx, pstr2);
	SG_VHASH_NULLFREE(pCtx, pvh);
}
void MyFn(alloc_random_buffer)(SG_context * pCtx, SG_byte** ppBuf, SG_uint32* lenBuf)
{
	char  bufGidRandom[SG_GID_BUFFER_LENGTH];
	SG_byte* pBuf = NULL;
	const char* pszTest =
		"Hail! to the victors valiant\n"
		"Hail! to the conqu'ring heroes\n"
		"Hail! Hail! to Michigan\n"
		"The leaders and best!\n"
		"Hail! to the victors valiant\n"
		"Hail! to the conqu'ring heroes\n"
		"Hail! Hail! to Michigan,\n"
		"The champions of the West!\n";
	SG_uint32 lenTest = SG_STRLEN(pszTest);
	SG_uint32 lenTotal = lenTest + SG_GID_BUFFER_LENGTH;

	// Add some random data so we don't accidentally add duplicate blobs.
	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, bufGidRandom, sizeof(bufGidRandom))  );
	VERIFY_ERR_CHECK(  SG_alloc(pCtx, 1, lenTotal, &pBuf)  );
	memcpy(pBuf, pszTest, lenTest);
	memcpy(pBuf + lenTest, bufGidRandom, sizeof(bufGidRandom));

	*ppBuf = pBuf;
	*lenBuf = lenTotal;

	return;

fail:
	SG_NULLFREE(pCtx, pBuf);
}
void MyFn(remove__value)(SG_context* pCtx)
{
	SG_vector* pVector = NULL;
	SG_uint32  uSize   = 0u;
	SG_uint32  uIndex  = 0u;

	// create a test vector
	VERIFY_ERR_CHECK(  MyFn(_create_test_vector)(pCtx, &pVector, &uSize)  );

	// run through each item and remove it by value
	// verify that we remove the number of values expected
	// also verify that the resulting size is as expected
	for (uIndex = 0u; uIndex < SG_NrElements(gaTestItems); ++uIndex)
	{
		test_item* pTestItem = gaTestItems + uIndex;
		SG_uint32  uRemoved  = 0u;

		VERIFY_ERR_CHECK(  SG_vector__remove__value(pCtx, pVector, (void*)pTestItem->szValue, &uRemoved, NULL)  );
		VERIFYP_COND("Removed wrong number of items.", uRemoved == pTestItem->uCount, ("Value(%s) Expected(%u) Removed(%u)", pTestItem->szValue, pTestItem->uCount, uRemoved));
		uSize -= uRemoved;
		VERIFY_ERR_CHECK(  MyFn(_verify_size)(pCtx, pVector, uSize)  );
	}

fail:
	SG_VECTOR_NULLFREE(pCtx, pVector);
	return;
}
// Generates a vector from gaTestItems.
void MyFn(_create_test_vector)(
	SG_context* pCtx,
	SG_vector** ppVector,
	SG_uint32*  pSize
	)
{
	SG_vector* pVector        = NULL;
	SG_uint32  uIndex         = 0u;
	SG_uint32  uExpectedTotal = 0u;
	SG_uint32  uPass          = 0u;
	SG_uint32  uPassTotal     = 1u;
	SG_uint32  uActualTotal   = 0u;

	// check how many big the final vector will be
	for (uIndex = 0u; uIndex < SG_NrElements(gaTestItems); ++uIndex)
	{
		test_item* pTestItem = gaTestItems + uIndex;
		uExpectedTotal += pTestItem->uCount;
	}

	// allocate the vector
	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC(pCtx, &pVector, uExpectedTotal)  );
	
	// add the items in passes rather than each item sequentially
	// this way values appended multiple times won't be sequential
	while (uPassTotal > 0u)
	{
		uPassTotal = 0u;
		for (uIndex = 0u; uIndex < SG_NrElements(gaTestItems); ++uIndex)
		{
			test_item* pTestItem = gaTestItems + uIndex;

			if (pTestItem->uCount > uPass)
			{
				VERIFY_ERR_CHECK(  SG_vector__append(pCtx, pVector, (void*)pTestItem->szValue, NULL)  );
				uPassTotal += 1u;
			}
		}
		uActualTotal += uPassTotal;
		uPass        += 1u;
	}
	VERIFY_ERR_CHECK(  MyFn(_verify_size)(pCtx, pVector, uExpectedTotal)  );
	VERIFY_ERR_CHECK(  MyFn(_verify_size)(pCtx, pVector, uActualTotal)  );

	// return the vector
	if (ppVector != NULL)
	{
		*ppVector = pVector;
		pVector = NULL;
	}
	if (pSize != NULL)
	{
		*pSize = uActualTotal;
	}

fail:
	SG_VECTOR_NULLFREE(pCtx, pVector);
}
Ejemplo n.º 8
0
void u0048_multidag__add_dagnode(SG_context * pCtx,
								 char** ppszid, const char* pszidParent, SG_uint32 iDagNum, SG_repo* pRepo)
{
	char buf_tid[SG_TID_MAX_BUFFER_LENGTH];
    SG_dagnode* pdn = NULL;
	SG_repo_tx_handle* pTx;

	// create a TID just to get some random data.  use it to create a HID.
	// use the HID as the HID of a hypothetical changeset so that we can create the dagnode.

	VERIFY_ERR_CHECK(  SG_tid__generate(pCtx, buf_tid, sizeof(buf_tid))  );
	VERIFY_ERR_CHECK(  SG_repo__alloc_compute_hash__from_bytes(pCtx,
															   pRepo,
															   sizeof(buf_tid),
															   (SG_byte *)buf_tid,
															   ppszid)  );

    VERIFY_ERR_CHECK(  SG_dagnode__alloc(pCtx, &pdn,*ppszid, pszidParent ? 2: 1 )  );
    if (pszidParent)
    {
        VERIFY_ERR_CHECK(  SG_dagnode__add_parent(pCtx, pdn,pszidParent)  );
    }
    VERIFY_ERR_CHECK(  SG_dagnode__freeze(pCtx, pdn)  );
	VERIFY_ERR_CHECK(  SG_repo__begin_tx(pCtx, pRepo, &pTx)  );
	VERIFY_ERR_CHECK(  SG_repo__store_dagnode(pCtx, pRepo, pTx, iDagNum, pdn)  );
    pdn = NULL;
    VERIFY_ERR_CHECK(  SG_repo__commit_tx(pCtx, pRepo,&pTx)  );

    return;

fail:
    return;
}
static void MyFn(free_uint32)(SG_context* pCtx, void* pValue)
{
	VERIFY_ERR_CHECK(  SG_free(pCtx, pValue)  );

fail:
	return;
}
Ejemplo n.º 10
0
void u0048_multidag__new_repo(
	SG_context * pCtx,
	const char* pszRidescName,
    SG_repo** ppResult
	)
{
	SG_vhash* pvhPartialDescriptor = NULL;
	SG_repo* pRepo = NULL;
	const SG_vhash* pvhActualDescriptor = NULL;
	SG_changeset* pcsFirst = NULL;
	char* pszidGidActualRoot = NULL;
	const char* pszidFirstChangeset = NULL;
    char buf_repo_id[SG_GID_BUFFER_LENGTH];
    char buf_admin_id[SG_GID_BUFFER_LENGTH];

	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, buf_repo_id, sizeof(buf_repo_id))  );
	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, buf_admin_id, sizeof(buf_admin_id))  );

	VERIFY_ERR_CHECK(  SG_closet__get_partial_repo_instance_descriptor_for_new_local_repo(pCtx, &pvhPartialDescriptor)  );

    /* This test case writes dag nodes which are not real.  They don't have a
     * changeset associated with them.  So, if we use a caching repo, the
     * caching code will fail because it tries to load a changeset
     * which doesn't exist.  So, we strip down to a raw repo here. */

	VERIFY_ERR_CHECK(  SG_repo__create_repo_instance(pCtx,pvhPartialDescriptor,SG_TRUE,NULL,buf_repo_id,buf_admin_id,&pRepo)  );
    VERIFY_ERR_CHECK(  sg_zing__init_new_repo(pCtx, pRepo)  );

	VERIFY_ERR_CHECK(  SG_repo__create_user_root_directory(pCtx, pRepo, "@", &pcsFirst, &pszidGidActualRoot)  );

	VERIFY_ERR_CHECK(  SG_changeset__get_id_ref(pCtx, pcsFirst, &pszidFirstChangeset)  );

	VERIFY_ERR_CHECK(  SG_repo__get_descriptor(pCtx, pRepo, &pvhActualDescriptor)  );

	/* TODO should this give an error if the ridesc name already exists? */

	VERIFY_ERR_CHECK(  SG_closet__descriptors__add(pCtx, pszRidescName, pvhActualDescriptor)  );

	SG_NULLFREE(pCtx, pszidGidActualRoot);
	SG_CHANGESET_NULLFREE(pCtx, pcsFirst);
	SG_VHASH_NULLFREE(pCtx, pvhPartialDescriptor);

    *ppResult = pRepo;

	return;

fail:
	SG_VHASH_NULLFREE(pCtx, pvhPartialDescriptor);
	SG_REPO_NULLFREE(pCtx, pRepo);

	return;
}
Ejemplo n.º 11
0
MyMain()
{
	TEMPLATE_MAIN_START;

#ifdef SG_NIGHTLY_BUILD
	VERIFY_ERR_CHECK(  MyFn(test__big_changeset)(pCtx)  );
#endif
	VERIFY_ERR_CHECK(  MyFn(test__simple)(pCtx)  );
 	VERIFY_ERR_CHECK(  MyFn(test__long_dag)(pCtx)  );
 	VERIFY_ERR_CHECK(  MyFn(test__wide_dag)(pCtx)  );

	// Fall through to common cleanup.

fail:

	TEMPLATE_MAIN_END;
}
void MyFn(test1)(SG_context * pCtx)
{
	SG_vector * pVec = NULL;
	SG_uint32 k, ndx, len;
	void * pValue;
	SG_uint32 variable_1 = 0;
	SG_uint32 variable_2 = 0;
#define ADDR_1(k)		((void *)((&variable_1)+k))
#define ADDR_2(k)		((void *)((&variable_2)+k))

	VERIFY_ERR_CHECK(  SG_vector__alloc(pCtx,&pVec,20)  );

	VERIFY_ERR_CHECK(  SG_vector__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==0));

	for (k=0; k<100; k++)
	{
		// fabricate a bogus pointer from a constant and stuff it into the vector.

		VERIFY_ERR_CHECK(  SG_vector__append(pCtx,pVec,ADDR_1(k),&ndx)  );
		VERIFY_COND("append",(ndx==k));
	}
	for (k=0; k<100; k++)
	{
		VERIFY_ERR_CHECK(  SG_vector__get(pCtx,pVec,k,&pValue)  );
		VERIFY_COND("get1",(pValue == ADDR_1(k)));
	}

	for (k=0; k<100; k++)
	{
		VERIFY_ERR_CHECK(  SG_vector__set(pCtx,pVec,k,ADDR_2(k))  );
	}
	for (k=0; k<100; k++)
	{
		VERIFY_ERR_CHECK(  SG_vector__get(pCtx,pVec,k,&pValue)  );
		VERIFY_COND("get2",(pValue == ADDR_2(k)));
	}

	VERIFY_ERR_CHECK(  SG_vector__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==100));

	VERIFY_ERR_CHECK(  SG_vector__clear(pCtx,pVec)  );

	VERIFY_ERR_CHECK(  SG_vector__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==0));

	// fall thru to common cleanup

fail:
	SG_VECTOR_NULLFREE(pCtx, pVec);
}
Ejemplo n.º 13
0
void MyFn(create_tmp_src_dir)(SG_context * pCtx, SG_pathname ** ppPathnameTempDir)
{
	// create a temp directory in the current directory to be the
	// home of some userfiles.
	// caller must free returned value.

	SG_pathname * pPathnameTempDir = NULL;

	VERIFY_ERR_CHECK(  unittest__alloc_unique_pathname_in_cwd(pCtx,&pPathnameTempDir)  );
	VERIFY_ERR_CHECK(  SG_fsobj__mkdir_recursive__pathname(pCtx, pPathnameTempDir)  );

	INFOP("mktmpdir",("Temp Src Dir is [%s]",SG_pathname__sz(pPathnameTempDir)));

	*ppPathnameTempDir = pPathnameTempDir;
	return;

fail:
	SG_PATHNAME_NULLFREE(pCtx, pPathnameTempDir);
}
Ejemplo n.º 14
0
void MyFn(test1)(SG_context * pCtx)
{
	SG_vector_i64 * pVec = NULL;
	SG_uint32 k, ndx, len;

	VERIFY_ERR_CHECK(  SG_vector_i64__alloc(pCtx,&pVec,20)  );

	VERIFY_ERR_CHECK(  SG_vector_i64__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==0));

	for (k=0; k<100; k++)
	{
		SG_int64 kValue = (SG_int64)k;
		VERIFY_ERR_CHECK(  SG_vector_i64__append(pCtx,pVec,kValue,&ndx)  );
		VERIFY_COND("append",(ndx==k));
	}
	for (k=0; k<100; k++)
	{
		SG_int64 value;
		VERIFY_ERR_CHECK(  SG_vector_i64__get(pCtx,pVec,k,&value)  );
		VERIFY_COND("get1",(value == ((SG_int64)k)));
	}

	for (k=0; k<100; k++)
	{
		SG_int64 kValue = (SG_int64)k+10000;
		VERIFY_ERR_CHECK(  SG_vector_i64__set(pCtx,pVec,k,kValue)  );
	}
	for (k=0; k<100; k++)
	{
		SG_int64 value;
		VERIFY_ERR_CHECK(  SG_vector_i64__get(pCtx,pVec,k,&value)  );
		VERIFY_COND("get2",(value == ((SG_int64)(k+10000))));
	}

	VERIFY_ERR_CHECK(  SG_vector_i64__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==100));

	VERIFY_ERR_CHECK(  SG_vector_i64__clear(pCtx,pVec)  );

	VERIFY_ERR_CHECK(  SG_vector_i64__length(pCtx,pVec,&len)  );
	VERIFY_COND("test1",(len==0));

	// fall thru to common cleanup

fail:
	SG_VECTOR_I64_NULLFREE(pCtx, pVec);
}
// Store a blob.  Make sure it doesn't show up until/unless the repo tx is committed.
void MyFn(one_blob)(SG_context * pCtx, SG_repo* pRepo)
{
	SG_byte* pBufIn = NULL;
	SG_uint32 lenBufIn = 0;

	SG_byte* pBufOut = NULL;
	SG_uint64 lenBufOut = 0;

	SG_repo_tx_handle* pTx;
	char* pszHidReturned = NULL;

	VERIFY_ERR_CHECK(  MyFn(alloc_random_buffer)(pCtx, &pBufIn, &lenBufIn)  );

	// Start writing blob.
	VERIFY_ERR_CHECK(  SG_repo__begin_tx(pCtx, pRepo, &pTx)  );
	VERIFY_ERR_CHECK(  SG_repo__store_blob_from_memory(pCtx, pRepo, pTx, SG_FALSE, pBufIn, lenBufIn, &pszHidReturned)  );

	// Should fail: tx not committed.
	VERIFY_ERR_CHECK_ERR_EQUALS_DISCARD(  SG_repo__fetch_blob_into_memory(pCtx, pRepo, pszHidReturned, &pBufOut, &lenBufOut),
										  SG_ERR_BLOB_NOT_FOUND  );			// Blob visible before repo tx committed
	SG_NULLFREE(pCtx, pBufOut);

	// Abort repo tx.
	VERIFY_ERR_CHECK(  SG_repo__abort_tx(pCtx, pRepo, &pTx)  );
	VERIFY_COND("SG_repo__abort_tx should null/free the repo transaction.", !pTx);

	// Should fail: tx aborted.
	VERIFY_ERR_CHECK_ERR_EQUALS_DISCARD(  SG_repo__fetch_blob_into_memory(pCtx, pRepo, pszHidReturned, &pBufOut, &lenBufOut),
										  SG_ERR_BLOB_NOT_FOUND  );			// Blob exists after repo tx abort
	SG_NULLFREE(pCtx, pBufOut);

	SG_NULLFREE(pCtx, pszHidReturned);

	// Write blob, commit tx.
	VERIFY_ERR_CHECK(  SG_repo__begin_tx(pCtx, pRepo, &pTx)  );
	VERIFY_ERR_CHECK(  SG_repo__store_blob_from_memory(pCtx, pRepo, pTx, SG_FALSE, pBufIn, lenBufIn, &pszHidReturned)  );
	VERIFY_ERR_CHECK(  SG_repo__commit_tx(pCtx, pRepo, &pTx)  );
	VERIFY_COND("SG_repo__commit_tx should null/free the repo transaction.", !pTx);

	// Read back the blob.  It should exist now.
	VERIFY_ERR_CHECK(  SG_repo__fetch_blob_into_memory(pCtx, pRepo, pszHidReturned, &pBufOut, &lenBufOut)  );
	// Just verify the length.  It's another test's job to roundtrip blobs and verify data.
	VERIFY_COND(  "blob length mismatch", lenBufOut == lenBufIn  );

	 // Fall through to common cleanup.

fail:
	SG_NULLFREE(pCtx, pszHidReturned);
	SG_NULLFREE(pCtx, pBufIn);
	SG_NULLFREE(pCtx, pBufOut);
}
/**
 * This test requires the 'crash' command be defined/enabled in 'vv'.
 * This command will always take a SIGSEGV.
 */
void MyFn(test1)(SG_context * pCtx)
{
    SG_exit_status exitStatusChild;
    SG_exec_argvec * pArgVec = NULL;

    VERIFY_ERR_CHECK(  SG_exec_argvec__alloc(pCtx,&pArgVec)  );
    VERIFY_ERR_CHECK(  SG_exec_argvec__append__sz(pCtx,pArgVec,"crash")  );

    // we use the easy version and only get back an exit status.
    // this should have caught the crash and tried to run gdb on
    // the coredump.  we didn't divert the gdb output, so we'll
    // just eyeball the test log for now.

    SG_exec__exec_sync__files(pCtx, "vv" ,pArgVec,NULL,NULL,NULL,&exitStatusChild);

    VERIFY_CTX_ERR_EQUALS("child status", pCtx, SG_ERR_ABNORMAL_TERMINATION);
    SG_context__err_reset(pCtx);

fail:
    SG_EXEC_ARGVEC_NULLFREE(pCtx, pArgVec);
}
void u0026_jsonparser__test_jsonparser(SG_context * pCtx)
{
	SG_string* pstr = NULL;

	VERIFY_ERR_CHECK(  SG_STRING__ALLOC(pCtx, &pstr)  );

	u0026_jsonparser__create_1(pCtx, pstr);
	VERIFY_ERR_CHECK_DISCARD(  u0026_jsonparser__verify(pCtx, pstr)  );

fail:
	SG_STRING_NULLFREE(pCtx, pstr);
}
void MyFn(clear__with_assoc)(SG_context * pCtx)
{
	static const SG_uint32 uSize = 100u;
	
	SG_vector* pVector = NULL;
	SG_uint32  uIndex  = 0u;
	SG_uint32  uOutput = 0u;

	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC(pCtx, &pVector, uSize)  );

	// add some allocated data to the vector
	for (uIndex = 0u; uIndex < uSize; ++uIndex)
	{
		SG_uint32* pValue = NULL;
		VERIFY_ERR_CHECK(  SG_alloc1(pCtx, pValue)  );
		*pValue = uIndex;
		VERIFY_ERR_CHECK(  SG_vector__append(pCtx, pVector, pValue, &uOutput)  );
		VERIFY_COND("Added item has unexpected index.", uOutput == uIndex);
	}

	// verify that the length is what we expect
	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pVector, &uOutput)  );
	VERIFY_COND("Vector's length doesn't match added item count.", uOutput == uSize);

	// clear the vector using our free callback
	VERIFY_ERR_CHECK(  SG_vector__clear__with_assoc(pCtx, pVector, MyFn(free_uint32))  );
	// if we get memory leaks, then the callback wasn't properly called to free the elements

	// verify that the vector is now empty
	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pVector, &uOutput)  );
	VERIFY_COND("Vector's length is non-zero after being cleared.", uOutput == 0u);

fail:
	SG_VECTOR_NULLFREE(pCtx, pVector);
}
/**
 * List all of the installed repo implementations.
 *
 * NOTE: I don't have a way to verify that the list is complete or verify
 * NOTE: what it should contain, so I just print the list.
 */
void MyFn(list_vtables)(SG_context * pCtx)
{
	SG_vhash * pvh_vtables = NULL;
	SG_uint32 count_vtables;
	SG_uint32 k;

	VERIFY_ERR_CHECK(  SG_repo__query_implementation(pCtx,NULL,
													 SG_REPO__QUESTION__VHASH__LIST_REPO_IMPLEMENTATIONS,
													 NULL,NULL,NULL,0,
													 &pvh_vtables)  );
	VERIFY_ERR_CHECK(  SG_vhash__count(pCtx,pvh_vtables,&count_vtables)  );
	for (k=0; k<count_vtables; k++)
	{
		const char * pszKey_vtable_k;

		VERIFY_ERR_CHECK(  SG_vhash__get_nth_pair(pCtx,pvh_vtables,k,&pszKey_vtable_k,NULL)  );
		INFOP("vtable",("Repo Implementation[%d]: [%s]",k,pszKey_vtable_k));
	}

fail:
	SG_VHASH_NULLFREE(pCtx, pvh_vtables);
}
static void MyFn(copy_uint32)(SG_context* pCtx, void* pInput, void** pOutput)
{
	SG_uint32* pInputInt  = (SG_uint32*)pInput;
	SG_uint32* pOutputInt = NULL;

	VERIFY_ERR_CHECK(  SG_alloc1(pCtx, pOutputInt)  );
	*pOutputInt = *pInputInt;

	*pOutput = pOutputInt;

fail:
	return;
}
void MyFn(remove__if)(SG_context* pCtx)
{
	static SG_uint32   uItemCount    = 20u;
	static const char* szItemValue   = "abc";
	static SG_uint32   uStartDivisor = 5u;

	SG_vector* pVector  = NULL;
	SG_uint32  uIndex   = 0u;
	SG_uint32  uCount   = uItemCount;
	SG_uint32  uDivisor = 0u;
	SG_uint32  uRemoved = 0u;

	// create a vector with test data
	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC(pCtx, &pVector, 10u)  );
	for (uIndex = 0u; uIndex < uItemCount; ++uIndex)
	{
		VERIFY_ERR_CHECK(  SG_vector__append(pCtx, pVector, (void*)szItemValue, NULL)  );
	}

	// run tests, removing every Nth, then every (N-1)th item, etc
	// the last iteration (every 1th item) will remove every remaining item
	for (uDivisor = uStartDivisor; uDivisor > 0u; --uDivisor)
	{
		SG_uint32 uExpected = uCount / uDivisor;

		VERIFY_ERR_CHECK(  SG_vector__remove__if(pCtx, pVector, MyFn(remove__if__predicate), (void*)&uDivisor, &uRemoved, NULL)  );
		VERIFYP_COND("Wrong number of items removed.", uRemoved == uExpected, ("Removed(%u) Expected(%u)", uRemoved, uExpected));

		uCount -= uExpected;
	}
	VERIFY_COND("Expected size should be zero.", uCount == 0u);

	// verify that the vector is empty
	VERIFY_ERR_CHECK(  MyFn(_verify_size)(pCtx, pVector, 0u)  );

fail:
	SG_VECTOR_NULLFREE(pCtx, pVector);
	return;
}
void MyFn(alloc__copy__deep)(SG_context * pCtx)
{
	static const SG_uint32 uSize = 100u;
	
	SG_vector* pVector  = NULL;
	SG_vector* pCopy    = NULL;
	SG_uint32  uIndex   = 0u;
	SG_uint32  uOutput1 = 0u;
	SG_uint32  uOutput2 = 0u;
	void*      pOutput1 = NULL;
	void*      pOutput2 = NULL;

	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC(pCtx, &pVector, uSize)  );

	// add some allocated data to the vector
	for (uIndex = 0u; uIndex < uSize; ++uIndex)
	{
		SG_uint32* pValue = NULL;
		VERIFY_ERR_CHECK(  SG_alloc1(pCtx, pValue)  );
		*pValue = uIndex;
		VERIFY_ERR_CHECK(  SG_vector__append(pCtx, pVector, pValue, &uOutput1)  );
		VERIFY_COND("Added item has unexpected index.", uOutput1 == uIndex);
	}

	// copy the vector
	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC__COPY(pCtx, pVector, MyFn(copy_uint32), MyFn(free_uint32), &pCopy)  );

	// verify that the copy matches the original
	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pVector, &uOutput1)  );
	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pCopy,   &uOutput2)  );
	VERIFY_COND("Copied vector's length doesn't match added item count.", uOutput1 == uSize);
	VERIFY_COND("Copied vector's length doesn't match original.", uOutput1 == uOutput2);
	for (uIndex = 0u; uIndex < uOutput1; ++uIndex)
	{
		VERIFY_ERR_CHECK(  SG_vector__get(pCtx, pVector, uIndex, &pOutput1)  );
		VERIFY_ERR_CHECK(  SG_vector__get(pCtx, pCopy,   uIndex, &pOutput2)  );
		VERIFYP_COND("Copied vector's pointer value matches original after deep copy.", pOutput1 != pOutput2, ("index(%d)", uIndex));
		uOutput1 = *((SG_uint32*)pOutput1);
		uOutput2 = *((SG_uint32*)pOutput2);
		VERIFYP_COND("Copied vector's pointed-to value doesn't match original after deep copy.", uOutput1 == uOutput2, ("index(%d)", uIndex));
	}

fail:
	SG_context__push_level(pCtx);
	SG_vector__free__with_assoc(pCtx, pVector, MyFn(free_uint32));
	SG_vector__free__with_assoc(pCtx, pCopy,   MyFn(free_uint32));
	SG_context__pop_level(pCtx);
}
/**
 * List the hashes supported by each repo implementation.
 *
 * NOTE: I don't have a way to verify that the list is complete or verify
 * NOTE: what it should contain, so I just print the list.
 */
void MyFn(list_hashes)(SG_context * pCtx)
{
	SG_repo * pRepo = NULL;
	SG_vhash * pvh_vtables = NULL;
	SG_vhash * pvh_HashMethods = NULL;
	SG_uint32 k, count_vtables;

	VERIFY_ERR_CHECK(  SG_repo__query_implementation(pCtx,NULL,
													 SG_REPO__QUESTION__VHASH__LIST_REPO_IMPLEMENTATIONS,
													 NULL,NULL,NULL,0,
													 &pvh_vtables)  );
	VERIFY_ERR_CHECK(  SG_vhash__count(pCtx,pvh_vtables,&count_vtables)  );
	for (k=0; k<count_vtables; k++)
	{
		const char * pszKey_vtable_k;
		SG_uint32 j, count_HashMethods;

		VERIFY_ERR_CHECK(  SG_vhash__get_nth_pair(pCtx,pvh_vtables,k,&pszKey_vtable_k,NULL)  );
		INFOP("vtable",("Repo Implementation[%d]: [%s]",k,pszKey_vtable_k));

		VERIFY_ERR_CHECK(  SG_repo__alloc(pCtx,&pRepo,pszKey_vtable_k)  );
		VERIFY_ERR_CHECK(  SG_repo__query_implementation(pCtx,pRepo,
														 SG_REPO__QUESTION__VHASH__LIST_HASH_METHODS,
														 NULL,NULL,NULL,0,
														 &pvh_HashMethods)  );
		VERIFY_ERR_CHECK(  SG_vhash__count(pCtx,pvh_HashMethods,&count_HashMethods)  );
		for (j=0; j<count_HashMethods; j++)
		{
			const char * pszKey_HashMethod_j;
			const SG_variant * pVariant;
			SG_int64 i64;
			SG_uint32 strlen_Hash_j;

			VERIFY_ERR_CHECK(  SG_vhash__get_nth_pair(pCtx,pvh_HashMethods,j,&pszKey_HashMethod_j,&pVariant)  );
			VERIFY_ERR_CHECK(  SG_variant__get__int64(pCtx,pVariant,&i64)  );
			strlen_Hash_j = (SG_uint32)i64;
			INFOP("vtable.hash_method",("Repo [%s] Hash [%s] Length [%d]",pszKey_vtable_k,pszKey_HashMethod_j,strlen_Hash_j));
		}

		SG_VHASH_NULLFREE(pCtx, pvh_HashMethods);
		SG_REPO_NULLFREE(pCtx, pRepo);
	}

fail:
	SG_VHASH_NULLFREE(pCtx, pvh_HashMethods);
	SG_REPO_NULLFREE(pCtx, pRepo);
	SG_VHASH_NULLFREE(pCtx, pvh_vtables);
}
Ejemplo n.º 24
0
void u0020_utf8pathnames__mkdir_tmp_dir(SG_context * pCtx, SG_pathname ** ppPathnameTmpDir)
{
	// create a temporary directory using a random name in the
	// current directory or in TMP.
	//
	// return the name of the pathname.

	char bufTid[SG_TID_MAX_BUFFER_LENGTH];
	SG_pathname * pPathnameTmpDir = NULL;

	VERIFY_ERR_CHECK(  SG_tid__generate2(pCtx, bufTid, sizeof(bufTid), 32)  );

	VERIFY_ERR_CHECK(  SG_PATHNAME__ALLOC__USER_TEMP_DIRECTORY(pCtx, &pPathnameTmpDir)  );
	VERIFY_ERR_CHECK(  SG_pathname__append__from_sz(pCtx, pPathnameTmpDir,bufTid)  );

	VERIFY_ERR_CHECK(  SG_fsobj__mkdir_recursive__pathname(pCtx, pPathnameTmpDir)  );

	*ppPathnameTmpDir = pPathnameTmpDir;
	return;

fail:
	SG_PATHNAME_NULLFREE(pCtx, pPathnameTmpDir);
}
void MyFn(_verify_size)(
	SG_context* pCtx,
	SG_vector*  pVector,
	SG_uint32   uExpected
	)
{
	SG_uint32 uSize = 0u;

	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pVector, &uSize)  );
	VERIFYP_COND("Incorrect size.", uSize == uExpected, ("Expected(%u) Actual(%u)", uExpected, uSize));

fail:
	return;
}
void MyFn(match_value__append)(SG_context* pCtx)
{
	SG_vector* pVector = NULL;
	SG_uint32  uSize   = 0u;
	SG_uint32  uIndex  = 0u;
	SG_vector* pTarget = NULL;

	// create a test vector
	VERIFY_ERR_CHECK(  MyFn(_create_test_vector)(pCtx, &pVector, &uSize)  );

	// run through each test item and copy all indices with that value to another vector
	// verify that the other vector receives the correct number of items
	for (uIndex = 0u; uIndex < SG_NrElements(gaTestItems); ++uIndex)
	{
		test_item* pTestItem = gaTestItems + uIndex;

		// allocate a target vector
		VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC(pCtx, &pTarget, uSize)  );

		// find all values that match the current item
		// add them to the target vector
		VERIFY_ERR_CHECK(  SG_vector__find__all(pCtx, pVector, SG_vector__predicate__match_value, (void*)pTestItem->szValue, SG_vector__callback__append, pTarget)  );

		// verify the size of the target vector
		VERIFY_ERR_CHECK(  MyFn(_verify_size)(pCtx, pTarget, pTestItem->uCount)  );

		// free the target vector
		SG_VECTOR_NULLFREE(pCtx, pTarget);
		pTarget = NULL;
	}

fail:
	SG_VECTOR_NULLFREE(pCtx, pVector);
	SG_VECTOR_NULLFREE(pCtx, pTarget);
	return;
}
Ejemplo n.º 27
0
void MyFn(create_repo)(SG_context * pCtx, SG_repo ** ppRepo)
{
	// caller must free returned value.

	SG_repo * pRepo;
	SG_pathname * pPathnameRepoDir = NULL;
	SG_vhash* pvhPartialDescriptor = NULL;
    char buf_repo_id[SG_GID_BUFFER_LENGTH];
    char buf_admin_id[SG_GID_BUFFER_LENGTH];
	char* pszRepoImpl = NULL;

	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, buf_repo_id, sizeof(buf_repo_id))  );
	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, buf_admin_id, sizeof(buf_admin_id))  );

	VERIFY_ERR_CHECK(  SG_PATHNAME__ALLOC(pCtx, &pPathnameRepoDir)  );
	VERIFY_ERR_CHECK(  SG_pathname__set__from_cwd(pCtx, pPathnameRepoDir)  );

	VERIFY_ERR_CHECK(  SG_VHASH__ALLOC(pCtx, &pvhPartialDescriptor)  );

	VERIFY_ERR_CHECK_DISCARD(  SG_localsettings__get__sz(pCtx, SG_LOCALSETTING__NEWREPO_DRIVER, NULL, &pszRepoImpl, NULL)  );
	VERIFY_ERR_CHECK_DISCARD(  SG_vhash__add__string__sz(pCtx, pvhPartialDescriptor, SG_RIDESC_KEY__STORAGE, pszRepoImpl)  );

	VERIFY_ERR_CHECK(  SG_vhash__add__string__sz(pCtx, pvhPartialDescriptor, SG_RIDESC_FSLOCAL__PATH_PARENT_DIR, SG_pathname__sz(pPathnameRepoDir))  );

	VERIFY_ERR_CHECK(  SG_repo__create_repo_instance(pCtx,pvhPartialDescriptor,SG_TRUE,NULL,buf_repo_id,buf_admin_id,&pRepo)  );

	SG_VHASH_NULLFREE(pCtx, pvhPartialDescriptor);

	{
		const SG_vhash * pvhRepoDescriptor = NULL;
		VERIFY_ERR_CHECK(  SG_repo__get_descriptor(pCtx, pRepo,&pvhRepoDescriptor)  );

		//INFOP("open_repo",("Repo is [%s]",SG_string__sz(pstrRepoDescriptor)));
	}

	*ppRepo = pRepo;

fail:
	SG_VHASH_NULLFREE(pCtx, pvhPartialDescriptor);
	SG_PATHNAME_NULLFREE(pCtx, pPathnameRepoDir);

    SG_NULLFREE(pCtx, pszRepoImpl);
}
void MyFn(create_repo)(SG_context * pCtx, SG_repo** ppRepo)
{
	SG_repo* pRepo = NULL;
	SG_pathname* pPath_repo = NULL;
	char buf_repo_id[SG_GID_BUFFER_LENGTH];
	char buf_admin_id[SG_GID_BUFFER_LENGTH];
	SG_vhash* pvhPartialDescriptor = NULL;
	char* pszRepoImpl = NULL;

	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, buf_repo_id, sizeof(buf_repo_id))  );
	VERIFY_ERR_CHECK(  SG_gid__generate(pCtx, buf_admin_id, sizeof(buf_admin_id))  );

	/* Get our paths fixed up */
	VERIFY_ERR_CHECK(  SG_PATHNAME__ALLOC(pCtx, &pPath_repo)  );
	VERIFY_ERR_CHECK(  SG_pathname__set__from_cwd(pCtx, pPath_repo)  );
	VERIFY_ERR_CHECK(  SG_pathname__append__from_sz(pCtx, pPath_repo, "repo")  );

	SG_fsobj__mkdir__pathname(pCtx, pPath_repo);
	SG_context__err_reset(pCtx);

	// Create the repo
	VERIFY_ERR_CHECK(  SG_VHASH__ALLOC(pCtx, &pvhPartialDescriptor)  );
	VERIFY_ERR_CHECK_DISCARD(  SG_localsettings__get__sz(pCtx, SG_LOCALSETTING__NEWREPO_DRIVER, NULL, &pszRepoImpl, NULL)  );
    if (pszRepoImpl)
    {
        VERIFY_ERR_CHECK_DISCARD(  SG_vhash__add__string__sz(pCtx, pvhPartialDescriptor, SG_RIDESC_KEY__STORAGE, pszRepoImpl)  );
    }

	VERIFY_ERR_CHECK(  SG_vhash__add__string__sz(pCtx, pvhPartialDescriptor, SG_RIDESC_FSLOCAL__PATH_PARENT_DIR, SG_pathname__sz(pPath_repo))  );
	VERIFY_ERR_CHECK(  SG_repo__create_repo_instance(pCtx,NULL,pvhPartialDescriptor,SG_TRUE,NULL,buf_repo_id,buf_admin_id,&pRepo)  );

	*ppRepo = pRepo;

	// Fall through to common cleanup

fail:
	SG_VHASH_NULLFREE(pCtx, pvhPartialDescriptor);
	SG_PATHNAME_NULLFREE(pCtx, pPath_repo);

    SG_NULLFREE(pCtx, pszRepoImpl);
}
void MyFn(_verify_offset_values)(
	SG_context* pCtx,
	SG_vector*  pVector,
	SG_uint32   uBegin,
	SG_uint32   uEnd,
	SG_int32    iOffset
	)
{
	SG_uint32 uIndex = 0u;

	for (uIndex = uBegin; uIndex < uEnd; ++uIndex)
	{
		SG_uint32* pValue    = NULL;
		SG_uint32  uExpected = uIndex + iOffset;

		VERIFY_ERR_CHECK(  SG_vector__get(pCtx, pVector, uIndex, (void**)&pValue)  );
		VERIFYP_COND("Incorrect value.", *pValue == uExpected, ("Expected(%u) Actual (%u) Index(%u)", uExpected, *pValue, uIndex));
	}

fail:
	return;
}
void MyFn(alloc__copy__shallow)(SG_context * pCtx)
{
	static const SG_uint32 uSize = 100u;
	
	SG_vector* pVector  = NULL;
	SG_vector* pCopy    = NULL;
	SG_uint32  uIndex   = 0u;
	SG_uint32  uOutput1 = 0u;
	SG_uint32  uOutput2 = 0u;
	void*      pOutput1 = NULL;
	void*      pOutput2 = NULL;

	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC(pCtx, &pVector, uSize)  );

	// add some random stack data to the vector
	for (uIndex = 0u; uIndex < uSize; ++uIndex)
	{
		VERIFY_ERR_CHECK(  SG_vector__append(pCtx, pVector, &uIndex + uIndex, &uOutput1)  );
		VERIFY_COND("Added item has unexpected index.", uOutput1 == uIndex);
	}

	// copy the vector
	VERIFY_ERR_CHECK(  SG_VECTOR__ALLOC__COPY(pCtx, pVector, NULL, NULL, &pCopy)  );

	// verify that the copy matches the original
	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pVector, &uOutput1)  );
	VERIFY_ERR_CHECK(  SG_vector__length(pCtx, pCopy,   &uOutput2)  );
	VERIFY_COND("Copied vector's length doesn't match added item count.", uOutput1 == uSize);
	VERIFY_COND("Copied vector's length doesn't match original.", uOutput1 == uOutput2);
	for (uIndex = 0u; uIndex < uOutput1; ++uIndex)
	{
		VERIFY_ERR_CHECK(  SG_vector__get(pCtx, pVector, uIndex, &pOutput1)  );
		VERIFY_ERR_CHECK(  SG_vector__get(pCtx, pCopy,   uIndex, &pOutput2)  );
		VERIFYP_COND("Copied vector's value doesn't match original.", pOutput1 == pOutput2, ("index(%d", uIndex));
	}

fail:
	SG_VECTOR_NULLFREE(pCtx, pVector);
	SG_VECTOR_NULLFREE(pCtx, pCopy);
}