Author: aljen Date: 2010-05-19 04:48:23 +0200 (Wed, 19 May 2010) New Revision: 36862 Changeset: http://dev.haiku-os.org/changeset/36862/haiku Added: haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/idr_test.cpp Modified: haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/Jamfile Log: Added idr test Modified: haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/Jamfile =================================================================== --- haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/Jamfile 2010-05-18 22:09:57 UTC (rev 36861) +++ haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/Jamfile 2010-05-19 02:48:23 UTC (rev 36862) @@ -4,10 +4,19 @@ UsePrivateHeaders shared ; UseHeaders [ FDirName $(HAIKU_TOP) headers compatibility bsd ] : true ; UseHeaders [ FDirName $(HAIKU_TOP) src libs drm ] : true ; +UseHeaders [ FDirName $(HAIKU_TOP) src add-ons kernel drm ] : true ; UseLibraryHeaders drm ; +SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src add-ons kernel drm ] ; + SimpleTest drm_version : drm_version.c : libdrm.so ; +SimpleTest idr_test : + idr_test.cpp + idr.cpp + : $(TARGET_LIBSTDC++) +; + Added: haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/idr_test.cpp =================================================================== --- haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/idr_test.cpp (rev 0) +++ haiku/branches/components/gallium3d/src/tests/add-ons/kernel/drm/idr_test.cpp 2010-05-19 02:48:23 UTC (rev 36862) @@ -0,0 +1,141 @@ +#include <stdio.h> +#include <stdlib.h> +#include <iostream> + +#include "idr.h" + + +using namespace std; + +#define TESTERS_COUNT 1024 * 1024 +#define FAILED(args...) cout << "ERROR at line " << __LINE__ << ": " \ + << args << endl; + + +static uint32 sForeachCounter = 0; + +typedef struct test_data { + uint32 id; + void* ptr; +} test_data; + + +status_t dummy_foreach(idr_id_t id, void *ptr, void *data) +{ + (void)data; + test_data *tester = static_cast<test_data *>(ptr); +// cout << __FUNCTION__ << "(id: " << id << ", ptr: " << ptr << ", data: " +// << data << ") tester->id: " << tester->id << ", tester->ptr: " << tester->ptr << endl; + if (id != tester->id || ptr != tester->ptr) { + cout << __FUNCTION__ << " ERROR! id != tester->id || ptr != tester->ptr" << endl; + } + sForeachCounter++; + return 0; +} + + +int main(int argc, const char **argv) +{ + (void)argc; + (void)argv; + + struct idr_t *idp = NULL; + status_t error = 0; + idr_id_t id = 0; + void *ptr1 = NULL, *ptr2 = NULL; + + // generating test data + test_data *testers = (test_data *)malloc(TESTERS_COUNT * sizeof(test_data)); + if (!testers) { + FAILED("generating test data"); + goto cleanup; + } + + for (uint32 i = 0; i < TESTERS_COUNT; i++) { + testers[i].id = i; + testers[i].ptr = &testers[i]; + } + + // allocating idr + idp = (idr_t *)malloc(sizeof(*idp)); + IDR_INIT_PTR(idp); + if (!idp) { + FAILED("allocating idr"); + goto cleanup; + } + + // initializing idr + idr_init_with_size(idp, TESTERS_COUNT); + if (!idp->root) { + FAILED("initializing idr"); + goto cleanup; + } + +// cout << "idr size: " << ((idp->root_size * sizeof(idr_node_t)) / 1024) +// << " KiB" << endl; + + // filling test data + for (int i = 0; i < TESTERS_COUNT; i++) { + idr_id_t id = idr_get_new(idp, (void*)&testers[i]); + if (id == IDR_ID_NONE) { + FAILED("something went wrong, can't allocate new id"); + goto cleanup; + } + } + + // fetching pointer for id 5 and from oryginal test data + ptr1 = idr_find(idp, 5); + // fetching oryginal pointer for id 5 + ptr2 = &testers[5]; + + if (ptr1 != ptr2) { + FAILED("ptr for id 5 doesn't match original ptr"); + goto cleanup; + } + + // removing one element from idr (id#5) and fetching ptr for it + idr_remove(idp, 5); + // fetching pointer for id 5 + ptr1 = idr_find(idp, 5); + if (ptr1 != NULL) { + FAILED("ptr for element 5 should be NULL, it was removed"); + goto cleanup; + } + + // allocating new id for ptr &testers[5] + id = idr_get_new(idp, &testers[5]); + if (id != 5) { + FAILED("id should be 5, there was only one id avail.(#5)"); + goto cleanup; + } + + // fetching pointer for id 5 and from oryginal test data + ptr1 = idr_find(idp, 5); + // fetching oryginal pointer for id 5 + ptr2 = &testers[5]; + + if (ptr1 != ptr2) { + FAILED("ptr for id 5 doesn't match original ptr"); + goto cleanup; + } + + // doing foreach on " << TESTERS_COUNT << " elements + error = idr_for_each(idp, dummy_foreach, NULL); + if (error < 0) { + FAILED("dummy foreach ret code is < 0"); + goto cleanup; + } + + if (sForeachCounter != TESTERS_COUNT) { + FAILED("foreach counter != TESTERS_COUNT"); + goto cleanup; + } + + cout << "all tests passed :)" << endl; + +cleanup: + idr_destroy(idp); + free(testers); + + return 0; +}