Home | History | Annotate | Download | only in map
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 #include "map_wrapper.h"
     15 
     16 const int kNumberOfElements = 10;
     17 
     18 void FailTest(bool failed)
     19 {
     20     if (failed)
     21     {
     22         printf("Test failed!\n");
     23         printf("Press enter to continue:");
     24         getchar();
     25         exit(0);
     26     }
     27 }
     28 
     29 int GetStoredIntegerValue(MapItem* map_item)
     30 {
     31     void* map_item_pointer = map_item->GetItem();
     32     if (map_item_pointer != NULL)
     33     {
     34         return *(reinterpret_cast<int*>(map_item_pointer));
     35     }
     36     return static_cast<int>(map_item->GetUnsignedId());
     37 }
     38 
     39 void PrintMap(MapWrapper& map)
     40 {
     41     MapItem* map_item = map.First();
     42     printf("Map: ");
     43     while (map_item != NULL)
     44     {
     45       int item_value = GetStoredIntegerValue(map_item);
     46         FailTest(item_value < 0);
     47         printf(" %d",item_value);
     48         map_item = map.Next(map_item);
     49     }
     50     printf("\n");
     51 }
     52 
     53 int main(int /*argc*/, char* /*argv*/[])
     54 {
     55     int element_array[kNumberOfElements];
     56     for (int i = 0; i < kNumberOfElements; i++)
     57     {
     58         element_array[i] = i;
     59     }
     60     // Test insert
     61     MapWrapper test_map;
     62     for (int i = 0; i < kNumberOfElements; i++)
     63     {
     64         test_map.Insert(i,(void*)&element_array[i]);
     65     }
     66     // Test Erase1
     67     MapItem* remove_item = test_map.Find(2);
     68     FailTest(remove_item == NULL);
     69     FailTest(test_map.Erase(remove_item) != 0);
     70     FailTest(test_map.Find(2) != NULL);
     71     remove_item = NULL;
     72     FailTest(test_map.Erase(remove_item) != -1);
     73     // Test Erase2
     74     FailTest(test_map.Erase(1) != 0);
     75     FailTest(test_map.Find(1) != NULL);
     76     FailTest(test_map.Erase(1) != -1);
     77     // Test Size
     78     FailTest(test_map.Size() != kNumberOfElements - 2);
     79     PrintMap(test_map);
     80     // Test First
     81     MapItem* first_item = test_map.First();
     82     FailTest(first_item == NULL);
     83     FailTest(GetStoredIntegerValue(first_item) != 0);
     84     // Test Last
     85     MapItem* last_item = test_map.Last();
     86     FailTest(last_item == NULL);
     87     FailTest(GetStoredIntegerValue(last_item) != 9);
     88     // Test Next
     89     MapItem* second_item = test_map.Next(first_item);
     90     FailTest(second_item == NULL);
     91     FailTest(GetStoredIntegerValue(second_item) != 3);
     92     FailTest(test_map.Next(last_item) != NULL);
     93     // Test Previous
     94     MapItem* second_to_last_item = test_map.Previous(last_item);
     95     FailTest(second_to_last_item == NULL);
     96     FailTest(GetStoredIntegerValue(second_to_last_item) != 8);
     97     FailTest(test_map.Previous(first_item) != NULL);
     98     // Test Find (only improper usage untested)
     99     FailTest(test_map.Find(kNumberOfElements + 2) != NULL);
    100     // Test GetId
    101     FailTest(*(reinterpret_cast<int*>(second_to_last_item->GetItem())) !=
    102          second_to_last_item->GetId());
    103     FailTest(second_to_last_item->GetUnsignedId() !=
    104              static_cast<unsigned int>(second_to_last_item->GetId()));
    105     // Test SetItem
    106     int swapped_item = kNumberOfElements;
    107     last_item->SetItem(reinterpret_cast<void*>(&swapped_item));
    108     FailTest(GetStoredIntegerValue(last_item) !=
    109              swapped_item);
    110 
    111     printf("Tests passed successfully!\n");
    112 }
    113