diff options
Diffstat (limited to 'src/system_wrappers/test')
-rw-r--r-- | src/system_wrappers/test/Test.cpp | 65 | ||||
-rw-r--r-- | src/system_wrappers/test/TestSort/TestSort.cpp | 265 | ||||
-rw-r--r-- | src/system_wrappers/test/list/list.cc | 174 | ||||
-rw-r--r-- | src/system_wrappers/test/map/map.cc | 112 |
4 files changed, 616 insertions, 0 deletions
diff --git a/src/system_wrappers/test/Test.cpp b/src/system_wrappers/test/Test.cpp new file mode 100644 index 0000000000..7a34166b0d --- /dev/null +++ b/src/system_wrappers/test/Test.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include <cassert> +#include <iostream> + +#ifdef _WIN32 + #include <windows.h> + #include <tchar.h> +#else + #include <stdio.h> + #define Sleep(x) usleep(x*1000) +#endif + +#include "common_types.h" +#include "trace.h" +#include "cpu_wrapper.h" + + +#ifdef _WIN32 +int _tmain(int argc, _TCHAR* argv[]) +#else +int main(int argc, char* argv[]) +#endif +{ + Trace::CreateTrace(); + Trace::SetTraceFile("testTrace.txt"); + Trace::SetLevelFilter(webrtc::kTraceAll); + + printf("Start system wrapper test\n"); + + printf("Number of cores detected:%u\n", (unsigned int)CpuWrapper::DetectNumberOfCores()); + + CpuWrapper* cpu = CpuWrapper::CreateCpu(); + + WebRtc_UWord32 numCores; + WebRtc_UWord32* cores; + + for(int i = 0; i< 10;i++) + { + WebRtc_Word32 total = cpu->CpuUsageMultiCore(numCores, cores); + + printf("\nNumCores:%d\n", (int)numCores); + printf("Total cpu:%d\n", (int)total); + + for (WebRtc_UWord32 i = 0; i< numCores;i++) + { + printf("Core:%lu CPU:%lu \n", i, cores[i]); + } + Sleep(1000); + } + + printf("Done system wrapper test\n"); + + delete cpu; + + Trace::ReturnTrace(); +}; diff --git a/src/system_wrappers/test/TestSort/TestSort.cpp b/src/system_wrappers/test/TestSort/TestSort.cpp new file mode 100644 index 0000000000..6846a71356 --- /dev/null +++ b/src/system_wrappers/test/TestSort/TestSort.cpp @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include <cstdio> +#include <algorithm> +#include <cstring> + +#include "sort.h" +#include "tick_util.h" + +// Excellent work polluting the global namespace Visual Studio... +#undef max +#undef min +#include <limits> + +template<typename KeyType> +struct LotsOfData +{ + KeyType key; + char data[64]; +}; + +template<typename DataType> +int Compare(const void* dataX, const void* dataY) +{ + DataType dataX = (DataType)*(const DataType*)dataX; + DataType dataY = (DataType)*(const DataType*)dataY; + if (dataX > dataY) + { + return 1; + } + else if (dataX < dataY) + { + return -1; + } + + return 0; +}; + +template<typename DataType, typename KeyType> +int CompareKey(const void* dataX, const void* dataY) +{ + KeyType keyX = ((const DataType*)dataX)->key; + KeyType keyY = ((const DataType*)dataY)->key; + if (keyX > keyY) + { + return 1; + } + else if (keyX < keyY) + { + return -1; + } + + return 0; +} + +template<typename DataType> +struct KeyLessThan +{ + bool operator()(const DataType &dataX, const DataType &dataY) const + { + return dataX.key < dataY.key; + } +}; + +const char* TypeEnumToString(webrtc::Type type) +{ + switch (type) + { + using namespace webrtc; + case TYPE_Word8: + return "Word8"; + case TYPE_UWord8: + return "UWord8"; + case TYPE_Word16: + return "Word16"; + case TYPE_UWord16: + return "UWord16"; + case TYPE_Word32: + return "Word32"; + case TYPE_UWord32: + return "UWord32"; + case TYPE_Word64: + return "Word64"; + case TYPE_UWord64: + return "UWord64"; + case TYPE_Float32: + return "Float32"; + case TYPE_Float64: + return "Float64"; + default: + return "Unrecognized"; + } +} + +template<typename Type> +Type TypedRand() +{ + if (std::numeric_limits<Type>::is_integer) + { + double floatRand = static_cast<double>(rand()) / RAND_MAX; + if (std::numeric_limits<Type>::is_signed) + { + floatRand -= 0.5; + } + + // Uniform [-max()/2, max()/2] for signed + // [0, max()] for unsigned + return static_cast<Type>(floatRand * std::numeric_limits<Type>::max()); + } + else // Floating point + { + // Uniform [-0.5, 0.5] + // The outer cast is to remove template warnings. + return static_cast<Type>((static_cast<Type>(rand()) / RAND_MAX) - 0.5); + } +} + +template<typename KeyType> +void RunSortTest(webrtc::Type sortType, bool keySort) +{ + enum { DataLength = 1000 }; + enum { NumOfTests = 10000 }; + KeyType key[DataLength]; + KeyType keyRef[DataLength]; + LotsOfData<KeyType> data[DataLength]; + LotsOfData<KeyType> dataRef[DataLength]; + WebRtc_Word32 retVal = 0; + + if (keySort) + { + printf("Running %s KeySort() tests...\n", TypeEnumToString(sortType)); + } + else + { + printf("Running %s Sort() tests...\n", TypeEnumToString(sortType)); + } + + TickInterval accTicks; + for (int i = 0; i < NumOfTests; i++) + { + for (int j = 0; j < DataLength; j++) + { + key[j] = TypedRand<KeyType>(); + data[j].key = key[j]; + // Write index to payload. We use this later for verification. + sprintf(data[j].data, "%d", j); + } + + memcpy(dataRef, data, sizeof(data)); + memcpy(keyRef, key, sizeof(key)); + + retVal = 0; + TickTime t0 = TickTime::Now(); + if (keySort) + { + retVal = webrtc::KeySort(data, key, DataLength, sizeof(LotsOfData<KeyType>), + sortType); + + //std::sort(data, data + DataLength, KeyLessThan<KeyType>()); + //qsort(data, DataLength, sizeof(LotsOfData<KeyType>), + // CompareKey<LotsOfData<KeyType>, KeyType>); + } + else + { + retVal = webrtc::Sort(key, DataLength, sortType); + + //std::sort(key, key + DataLength); + //qsort(key, DataLength, sizeof(KeyType), Compare<KeyType>); + } + TickTime t1 = TickTime::Now(); + accTicks += (t1 - t0); + + if (retVal != 0) + { + printf("Test failed at iteration %d:\n", i); + printf("Sort returned an error. "); + printf("It likely does not support the requested type\nExiting...\n"); + exit(0); + } + + // Reference sort. + if (!keySort) + { + std::sort(keyRef, keyRef + DataLength); + } + + if (keySort) + { + for (int j = 0; j < DataLength - 1; j++) + { + if (data[j].key > data[j + 1].key) + { + printf("Test failed at iteration %d:\n", i); + printf("Keys are not monotonically increasing\nExiting...\n"); + exit(0); + } + + int index = atoi(data[j].data); + if (index < 0 || index >= DataLength || data[j].key != dataRef[index].key) + { + printf("Test failed at iteration %d:\n", i); + printf("Payload data is corrupt\nExiting...\n"); + exit(0); + } + } + } + else + { + for (int j = 0; j < DataLength - 1; j++) + { + if (key[j] > key[j + 1]) + { + printf("Test failed at iteration %d:\n", i); + printf("Data is not monotonically increasing\nExiting...\n"); + exit(0); + } + } + + if (memcmp(key, keyRef, sizeof(key)) != 0) + { + printf("Test failed at iteration %d:\n", i); + printf("Sort data differs from std::sort reference\nExiting...\n"); + exit(0); + } + } + } + + printf("Compliance test passed over %d iterations\n", NumOfTests); + + WebRtc_Word64 executeTime = accTicks.Milliseconds(); + printf("Execute time: %.2f s\n\n", (float)executeTime / 1000); +} + +int main() +{ + // Seed rand(). + srand(42); + bool keySort = false; + for (int i = 0; i < 2; i++) { + RunSortTest<WebRtc_Word8>(webrtc::TYPE_Word8, keySort); + RunSortTest<WebRtc_UWord8>(webrtc::TYPE_UWord8, keySort); + RunSortTest<WebRtc_Word16>(webrtc::TYPE_Word16, keySort); + RunSortTest<WebRtc_UWord16>(webrtc::TYPE_UWord16, keySort); + RunSortTest<WebRtc_Word32>(webrtc::TYPE_Word32, keySort); + RunSortTest<WebRtc_UWord32>(webrtc::TYPE_UWord32, keySort); + RunSortTest<WebRtc_Word64>(webrtc::TYPE_Word64, keySort); + RunSortTest<WebRtc_UWord64>(webrtc::TYPE_UWord64, keySort); + RunSortTest<float>(webrtc::TYPE_Float32, keySort); + RunSortTest<double>(webrtc::TYPE_Float64, keySort); + + keySort = !keySort; + } + + printf("All tests passed\n"); + + return 0; +} diff --git a/src/system_wrappers/test/list/list.cc b/src/system_wrappers/test/list/list.cc new file mode 100644 index 0000000000..5c4f0b96ec --- /dev/null +++ b/src/system_wrappers/test/list/list.cc @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "list_wrapper.h" + +const int kNumberOfElements = 10; + +void FailTest(bool failed) +{ + if (failed) + { + printf("Test failed!\n"); + printf("Press enter to continue:"); + getchar(); + exit(0); + } +} + +int GetStoredIntegerValue(ListItem* list_item) +{ + void* list_item_pointer = list_item->GetItem(); + if (list_item_pointer != NULL) + { + return *(reinterpret_cast<int*>(list_item_pointer)); + } + return static_cast<int>(list_item->GetUnsignedItem()); +} + +void PrintList(ListWrapper& list) +{ + ListItem* list_item = list.First(); + printf("List: "); + while (list_item != NULL) + { + int item_value = GetStoredIntegerValue(list_item); + FailTest(item_value < 0); + printf(" %d",item_value); + list_item = list.Next(list_item); + } + printf("\n"); +} + +// The list should always be in ascending order +void ListSanity(ListWrapper& list) +{ + if(list.Empty()) + { + return; + } + ListItem* item_iter = list.First(); + // Fake a previous value for the first iteration + int previous_value = GetStoredIntegerValue(item_iter) - 1; + while (item_iter != NULL) + { + const int value = GetStoredIntegerValue(item_iter); + FailTest(value != previous_value + 1); + previous_value = value; + item_iter = list.Next(item_iter); + } +} + +int main(int /*argc*/, char* /*argv*/[]) +{ + printf("List Test:\n"); + int element_array[kNumberOfElements]; + for (int i = 0; i < kNumberOfElements; i++) + { + element_array[i] = i; + } + // Test PushBack 1 + ListWrapper test_list; + for (int i = 2; i < kNumberOfElements - 2; i++) + { + FailTest(test_list.PushBack((void*)&element_array[i]) != 0); + } + // Test PushBack 2 + FailTest(test_list.PushBack(element_array[kNumberOfElements - 2]) != 0); + FailTest(test_list.PushBack(element_array[kNumberOfElements - 1]) != 0); + // Test PushFront 2 + FailTest(test_list.PushFront(element_array[1]) != 0); + // Test PushFront 1 + FailTest(test_list.PushFront((void*)&element_array[0]) != 0); + // Test GetSize + FailTest(test_list.GetSize() != kNumberOfElements); + PrintList(test_list); + //Test PopFront + FailTest(test_list.PopFront() != 0); + //Test PopBack + FailTest(test_list.PopBack() != 0); + // Test GetSize + FailTest(test_list.GetSize() != kNumberOfElements - 2); + // Test Empty + FailTest(test_list.Empty()); + // Test First + ListItem* first_item = test_list.First(); + FailTest(first_item == NULL); + // Test Last + ListItem* last_item = test_list.Last(); + FailTest(last_item == NULL); + // Test Next + ListItem* second_item = test_list.Next(first_item); + FailTest(second_item == NULL); + FailTest(test_list.Next(last_item) != NULL); + FailTest(test_list.Next(NULL) != NULL); + // Test Previous + ListItem* second_to_last_item = test_list.Previous(last_item); + FailTest(second_to_last_item == NULL); + FailTest(test_list.Previous(first_item) != NULL); + FailTest(test_list.Previous(NULL) != NULL); + // Test GetUnsignedItem + FailTest(last_item->GetUnsignedItem() != + kNumberOfElements - 2); + FailTest(last_item->GetItem() != + NULL); + // Test GetItem + FailTest(GetStoredIntegerValue(second_to_last_item) != + kNumberOfElements - 3); + FailTest(second_to_last_item->GetUnsignedItem() != 0); + // Pop last and first since they are pushed as unsigned items. + FailTest(test_list.PopFront() != 0); + FailTest(test_list.PopBack() != 0); + // Test Insert. Please note that old iterators are no longer valid at + // this point. + ListItem* insert_item_last = new ListItem(reinterpret_cast<void*>(&element_array[kNumberOfElements - 2])); + FailTest(test_list.Insert(test_list.Last(),insert_item_last) != 0); + FailTest(test_list.Insert(NULL,insert_item_last) == 0); + ListItem* insert_item_last2 = new ListItem(reinterpret_cast<void*>(&element_array[kNumberOfElements - 2])); + FailTest(test_list.Insert(insert_item_last2,NULL) == 0); + // test InsertBefore + ListItem* insert_item_first = new ListItem(reinterpret_cast<void*>(&element_array[1])); + FailTest(test_list.InsertBefore(test_list.First(),insert_item_first) != 0); + FailTest(test_list.InsertBefore(NULL,insert_item_first) == 0); + ListItem* insert_item_first2 = new ListItem(reinterpret_cast<void*>(&element_array[1])); + FailTest(test_list.InsertBefore(insert_item_first2,NULL) == 0); + PrintList(test_list); + ListSanity(test_list); + // Erase the whole list + int counter = 0; + while (test_list.PopFront() == 0) + { + FailTest(counter++ > kNumberOfElements); + } + PrintList(test_list); + // Test APIs when list is empty + FailTest(test_list.GetSize() != 0); + FailTest(test_list.PopFront() != -1); + FailTest(test_list.PopBack() != -1); + FailTest(!test_list.Empty()); + FailTest(test_list.First() != NULL); + FailTest(test_list.Last() != NULL); + FailTest(test_list.Next(NULL) != NULL); + FailTest(test_list.Previous(NULL) != NULL); + FailTest(test_list.Erase(NULL) != -1); + // Test Insert APIs when list is empty + ListItem* new_item = new ListItem(reinterpret_cast<void*>(&element_array[0])); + FailTest(test_list.Insert(NULL,new_item) != 0); + FailTest(test_list.Empty()); + FailTest(test_list.PopFront() != 0); + ListItem* new_item2 = new ListItem(reinterpret_cast<void*>(&element_array[0])); + FailTest(test_list.InsertBefore(NULL,new_item2) != 0); + FailTest(test_list.Empty()); + + printf("Tests passed successfully!\n"); +} diff --git a/src/system_wrappers/test/map/map.cc b/src/system_wrappers/test/map/map.cc new file mode 100644 index 0000000000..8a0d3e31d7 --- /dev/null +++ b/src/system_wrappers/test/map/map.cc @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "map_wrapper.h" + +const int kNumberOfElements = 10; + +void FailTest(bool failed) +{ + if (failed) + { + printf("Test failed!\n"); + printf("Press enter to continue:"); + getchar(); + exit(0); + } +} + +int GetStoredIntegerValue(MapItem* map_item) +{ + void* map_item_pointer = map_item->GetItem(); + if (map_item_pointer != NULL) + { + return *(reinterpret_cast<int*>(map_item_pointer)); + } + return static_cast<int>(map_item->GetUnsignedId()); +} + +void PrintMap(MapWrapper& map) +{ + MapItem* map_item = map.First(); + printf("Map: "); + while (map_item != NULL) + { + int item_value = GetStoredIntegerValue(map_item); + FailTest(item_value < 0); + printf(" %d",item_value); + map_item = map.Next(map_item); + } + printf("\n"); +} + +int main(int /*argc*/, char* /*argv*/[]) +{ + int element_array[kNumberOfElements]; + for (int i = 0; i < kNumberOfElements; i++) + { + element_array[i] = i; + } + // Test insert + MapWrapper test_map; + for (int i = 0; i < kNumberOfElements; i++) + { + test_map.Insert(i,(void*)&element_array[i]); + } + // Test Erase1 + MapItem* remove_item = test_map.Find(2); + FailTest(remove_item == NULL); + FailTest(test_map.Erase(remove_item) != 0); + FailTest(test_map.Find(2) != NULL); + remove_item = NULL; + FailTest(test_map.Erase(remove_item) != -1); + // Test Erase2 + FailTest(test_map.Erase(1) != 0); + FailTest(test_map.Find(1) != NULL); + FailTest(test_map.Erase(1) != -1); + // Test Size + FailTest(test_map.Size() != kNumberOfElements - 2); + PrintMap(test_map); + // Test First + MapItem* first_item = test_map.First(); + FailTest(first_item == NULL); + FailTest(GetStoredIntegerValue(first_item) != 0); + // Test Last + MapItem* last_item = test_map.Last(); + FailTest(last_item == NULL); + FailTest(GetStoredIntegerValue(last_item) != 9); + // Test Next + MapItem* second_item = test_map.Next(first_item); + FailTest(second_item == NULL); + FailTest(GetStoredIntegerValue(second_item) != 3); + FailTest(test_map.Next(last_item) != NULL); + // Test Previous + MapItem* second_to_last_item = test_map.Previous(last_item); + FailTest(second_to_last_item == NULL); + FailTest(GetStoredIntegerValue(second_to_last_item) != 8); + FailTest(test_map.Previous(first_item) != NULL); + // Test Find (only improper usage untested) + FailTest(test_map.Find(kNumberOfElements + 2) != NULL); + // Test GetId + FailTest(*(reinterpret_cast<int*>(second_to_last_item->GetItem())) != + second_to_last_item->GetId()); + FailTest(second_to_last_item->GetUnsignedId() != + static_cast<unsigned int>(second_to_last_item->GetId())); + // Test SetItem + int swapped_item = kNumberOfElements; + last_item->SetItem(reinterpret_cast<void*>(&swapped_item)); + FailTest(GetStoredIntegerValue(last_item) != + swapped_item); + + printf("Tests passed successfully!\n"); +} |