test_list.cpp
Go to the documentation of this file.
00001 #include <gtest/gtest.h>
00002 #include <cmath>
00003 
00004 #include <megatree/list.h>
00005 
00006 using namespace megatree;
00007 
00008 
00009 TEST(MegaTreeList, ListConstruction)
00010 {
00011   //create a list of ints for this test
00012   List<int> list;
00013 
00014   EXPECT_FALSE(list.getFrontPointer());
00015   EXPECT_FALSE(list.getBackPointer());
00016 
00017   list.push_back(1);
00018 
00019   //make sure the list has a front and a back
00020   EXPECT_EQ(list.back(), 1);
00021   EXPECT_EQ(list.front(), 1);
00022 
00023   //now, we'll push something onto the front
00024   list.push_front(2);
00025 
00026   EXPECT_EQ(list.back(), 1);
00027   EXPECT_EQ(list.front(), 2);
00028 
00029   //now, we'll push something onto the back again
00030   list.push_back(3);
00031   EXPECT_EQ(list.back(), 3);
00032   EXPECT_EQ(list.front(), 2);
00033 
00034   //now, we'll move that something to the front
00035   list.moveToFront(list.getBackPointer());
00036   EXPECT_EQ(list.back(), 1);
00037   EXPECT_EQ(list.front(), 3);
00038 
00039   //now, we'll move that thing to the back
00040   list.moveToBack(list.getFrontPointer());
00041   EXPECT_EQ(list.back(), 3);
00042   EXPECT_EQ(list.front(), 2);
00043 
00044   //and now, move something from the middle of the list
00045   list.push_back(4);
00046   list.moveToFront(list.getBackPointer()->previous);
00047   EXPECT_EQ(list.back(), 4);
00048   EXPECT_EQ(list.front(), 3);
00049 
00050   //pop things off the list and make sure back and front end up ok
00051   EXPECT_EQ(list.front(), 3);
00052   list.pop_front();
00053   EXPECT_EQ(list.front(), 2);
00054   list.pop_front();
00055   EXPECT_EQ(list.front(), 1);
00056   list.pop_front();
00057 
00058   EXPECT_EQ(list.getFrontPointer(), list.getBackPointer());
00059   EXPECT_EQ(list.back(), 4);
00060 
00061   //pop the last item of the list and make sure things work
00062   EXPECT_EQ(list.front(), 4);
00063   list.pop_front();
00064 
00065   EXPECT_FALSE(list.getFrontPointer());
00066   EXPECT_FALSE(list.getBackPointer());
00067 
00068   //build a new list to test pop_back
00069   list.push_front(5);
00070   list.push_front(6);
00071   EXPECT_EQ(list.back(), 5);
00072   EXPECT_EQ(list.front(), 6);
00073 
00074   EXPECT_EQ(list.back(), 5);
00075   list.pop_back();
00076 
00077   EXPECT_EQ(list.getFrontPointer(), list.getBackPointer());
00078   EXPECT_EQ(list.front(), 6);
00079   EXPECT_EQ(list.back(), 6);
00080 
00081   list.pop_back();
00082   EXPECT_FALSE(list.getFrontPointer());
00083   EXPECT_FALSE(list.getBackPointer());
00084 
00085 }
00086 
00087 TEST(MegaTreeList, ListIteration)
00088 {
00089   //create a list of ints for this test
00090   List<int> list;
00091 
00092   list.push_back(1);
00093   list.push_back(2);
00094   list.push_back(3);
00095   list.push_back(4);
00096   list.push_back(5);
00097 
00098   int count = 0;
00099   for(ListIterator<int> it = list.frontIterator(); !it.finished(); it.next())
00100   {
00101     EXPECT_EQ(it.get(), ++count);
00102   }
00103 
00104   for(ListIterator<int> it = list.backIterator(); !it.finished(); it.previous())
00105   {
00106     EXPECT_EQ(it.get(), count--);
00107   }
00108 
00109 }
00110 
00111 TEST(MegaTreeList, ListSplice)
00112 {
00113   //create a list of ints for this test
00114   List<int> list1, list2;
00115 
00116   list1.push_back(1);
00117   list1.push_back(2);
00118   list1.push_back(3);
00119 
00120   list2.push_back(4);
00121   list2.push_back(5);
00122   list2.push_back(6);
00123 
00124   list1.spliceToBack(list2);
00125 
00126   int count = 0;
00127   for(ListIterator<int> it = list1.frontIterator(); !it.finished(); it.next())
00128   {
00129     EXPECT_EQ(it.get(), ++count);
00130   }
00131 
00132   EXPECT_TRUE(list2.empty());
00133 
00134   //test splicing empty lists
00135   List<int> list3, list4;
00136 
00137   list3.spliceToBack(list4);
00138   EXPECT_TRUE(list3.empty());
00139   EXPECT_TRUE(list4.empty());
00140 
00141   //test splicing to an empty list
00142   list3.spliceToBack(list1);
00143   count = 0;
00144   for(ListIterator<int> it = list3.frontIterator(); !it.finished(); it.next())
00145   {
00146     EXPECT_EQ(it.get(), ++count);
00147   }
00148   EXPECT_TRUE(list1.empty());
00149 
00150   //test splicing an empty list onto the back
00151   list3.spliceToBack(list4);
00152   count = 0;
00153   for(ListIterator<int> it = list3.frontIterator(); !it.finished(); it.next())
00154   {
00155     EXPECT_EQ(it.get(), ++count);
00156   }
00157   EXPECT_TRUE(list4.empty());
00158 
00159 
00160 }
00161 
00162 // Run all the tests that were declared with TEST()
00163 int main(int argc, char **argv){
00164   testing::InitGoogleTest(&argc, argv);
00165   return RUN_ALL_TESTS();
00166 }


megatree_cpp
Author(s): Stuart Glaser
autogenerated on Mon Dec 2 2013 13:01:28