libStatGen Software  1
NonOverlapRegionsTest.cpp
00001 /*
00002  *  Copyright (C) 2011  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include "NonOverlapRegions.h"
00018 #include "NonOverlapRegionsTest.h"
00019 #include <assert.h>
00020 #include <iostream>
00021 
00022 int main(int argc, char ** argv)
00023 {
00024 
00025    NonOverlapRegionsTest myTest;
00026 
00027    myTest.test();
00028 }
00029 
00030 void NonOverlapRegionsTest::test()
00031 {
00032     testPos();
00033     testChrom();
00034 }
00035 
00036 
00037 void NonOverlapRegionsTest::testChrom()
00038 {
00039     NonOverlapRegions reg;
00040 
00041     // Assert that the regions are empty.
00042     assert(reg.myRegions.size() == 0);
00043     // Verify no regions.
00044     for(int i = 0; i < 30; i++)
00045     {
00046         assert(reg.inRegion("a", i) == false);
00047         assert(reg.inRegion("3", i) == false);
00048     }
00049     // The chromosomes checked for were added.
00050     assert(reg.myRegions.size() == 2);
00051     assert(reg.myRegions["a"].myRegions.size() == 0);
00052     assert(reg.myRegions["a"].myRegionIter == 
00053            reg.myRegions["a"].myRegions.end());
00054     assert(reg.myRegions["3"].myRegions.size() == 0);
00055     assert(reg.myRegions["3"].myRegionIter == 
00056            reg.myRegions["3"].myRegions.end());
00057 
00058     // Add a region.
00059     reg.add("3", 13, 15);
00060     // Verify regions.
00061     assert(reg.myRegions.size() == 2);
00062     for(int i = 0; i < 30; i++)
00063     {
00064         assert(reg.inRegion("a", i) == false);
00065         if((i >= 13) && (i < 15))
00066         {
00067             assert(reg.inRegion("3", i) == true);
00068         }
00069         else
00070         {
00071             assert(reg.inRegion("3", i) == false);
00072         }
00073     }
00074     
00075     // Add a region.
00076     reg.add("a", 1, 5);
00077     // Verify regions.
00078     assert(reg.myRegions.size() == 2);
00079     for(int i = 0; i < 30; i++)
00080     {
00081         if((i >= 1) && (i < 5))
00082         {
00083             assert(reg.inRegion("a", i) == true);
00084         }
00085         else
00086         {
00087             assert(reg.inRegion("a", i) == false);
00088         }
00089         if((i >= 13) && (i < 15))
00090         {
00091             assert(reg.inRegion("3", i) == true);
00092         }
00093         else
00094         {
00095             assert(reg.inRegion("3", i) == false);
00096         }
00097     }
00098     
00099 
00100 }
00101 
00102 void NonOverlapRegionsTest::testPos()
00103 {
00104     NonOverlapRegionPos reg;
00105     std::list< std::pair<int32_t, int32_t> >::iterator iter;
00106 
00107     // Assert that the regions are empty.
00108     assert(reg.myRegions.empty());
00109     assert(reg.myRegionIter == reg.myRegions.end());
00110     assert(reg.myTmpIter == reg.myRegions.end());
00111     // Verify regions.
00112     for(int i = 0; i < 30; i++)
00113     {
00114         assert(reg.inRegion(i) == false);
00115     }
00116 
00117 
00118     // Add a region
00119     reg.add(13, 15);
00120     // Verify regions.
00121     assert(reg.myRegions.size() == 1);
00122     assert(reg.myRegionIter->first == 13);
00123     assert(reg.myRegionIter->second == 15);
00124     for(int i = 0; i < 30; i++)
00125     {
00126         if((i >= 13) && (i < 15))
00127         {
00128             assert(reg.inRegion(i) == true);
00129         }
00130         else
00131         {
00132             assert(reg.inRegion(i) == false);
00133         }
00134     }
00135 
00136     // Insert before this.
00137     reg.add(4,6);
00138     assert(reg.myRegions.size() == 2);
00139     assert(reg.myRegionIter->first == 4);
00140     assert(reg.myRegionIter->second == 6);
00141     iter = reg.myRegions.begin();
00142     assert(iter->first == 4);
00143     assert(iter->second == 6);
00144     ++iter;
00145     assert(iter->first == 13);
00146     assert(iter->second == 15);
00147     ++iter;
00148     assert(iter == reg.myRegions.end());
00149     for(int i = 0; i < 30; i++)
00150     {
00151         if(((i >= 4) && (i < 6)) || ((i >= 13) && (i < 15)))
00152         {
00153             assert(reg.inRegion(i) == true);
00154         }
00155         else
00156         {
00157             assert(reg.inRegion(i) == false);
00158         }
00159     }
00160 
00161     // Insert at the end.
00162     reg.add(22,26);
00163     assert(reg.myRegions.size() == 3);
00164     assert(reg.myRegionIter->first == 22);
00165     assert(reg.myRegionIter->second == 26);
00166     iter = reg.myRegions.begin();
00167     assert(iter->first == 4);
00168     assert(iter->second == 6);
00169     ++iter;
00170     assert(iter->first == 13);
00171     assert(iter->second == 15);
00172     ++iter;
00173     assert(iter->first == 22);
00174     assert(iter->second == 26);
00175     ++iter;
00176     assert(iter == reg.myRegions.end());
00177     for(int i = 0; i < 30; i++)
00178     {
00179         if(((i >= 4) && (i < 6)) || ((i >= 13) && (i < 15)) || 
00180            ((i >= 22) && (i < 26)))
00181         {
00182             assert(reg.inRegion(i) == true);
00183         }
00184         else
00185         {
00186             assert(reg.inRegion(i) == false);
00187         }
00188     }
00189 
00190     // Insert in the middle.
00191     reg.add(8,9);
00192     assert(reg.myRegions.size() == 4);
00193     assert(reg.myRegionIter->first == 8);
00194     assert(reg.myRegionIter->second == 9);
00195     iter = reg.myRegions.begin();
00196     assert(iter->first == 4);
00197     assert(iter->second == 6);
00198     ++iter;
00199     assert(iter->first == 8);
00200     assert(iter->second == 9);
00201     ++iter;
00202     assert(iter->first == 13);
00203     assert(iter->second == 15);
00204     ++iter;
00205     assert(iter->first == 22);
00206     assert(iter->second == 26);
00207     ++iter;
00208     assert(iter == reg.myRegions.end());
00209     for(int i = 0; i < 30; i++)
00210     {
00211         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00212            ((i >= 13) && (i < 15)) || ((i >= 22) && (i < 26)))
00213         {
00214             assert(reg.inRegion(i) == true);
00215         }
00216         else
00217         {
00218             assert(reg.inRegion(i) == false);
00219         }
00220     }
00221 
00222     // Insert start does not overlap, but the end does.
00223     reg.add(20,24);
00224     assert(reg.myRegions.size() == 4);
00225     assert(reg.myRegionIter->first == 20);
00226     assert(reg.myRegionIter->second == 26);
00227     iter = reg.myRegions.begin();
00228     assert(iter->first == 4);
00229     assert(iter->second == 6);
00230     ++iter;
00231     assert(iter->first == 8);
00232     assert(iter->second == 9);
00233     ++iter;
00234     assert(iter->first == 13);
00235     assert(iter->second == 15);
00236     ++iter;
00237     assert(iter->first == 20);
00238     assert(iter->second == 26);
00239     ++iter;
00240     assert(iter == reg.myRegions.end());
00241     for(int i = 0; i < 30; i++)
00242     {
00243         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00244            ((i >= 13) && (i < 15)) || ((i >= 20) && (i < 26)))
00245         {
00246             assert(reg.inRegion(i) == true);
00247         }
00248         else
00249         {
00250             assert(reg.inRegion(i) == false);
00251         }
00252     }
00253 
00254     // Add another region
00255     reg.add(18,19);
00256     assert(reg.myRegions.size() == 5);
00257     assert(reg.myRegionIter->first == 18);
00258     assert(reg.myRegionIter->second == 19);
00259     iter = reg.myRegions.begin();
00260     assert(iter->first == 4);
00261     assert(iter->second == 6);
00262     ++iter;
00263     assert(iter->first == 8);
00264     assert(iter->second == 9);
00265     ++iter;
00266     assert(iter->first == 13);
00267     assert(iter->second == 15);
00268     ++iter;
00269     assert(iter->first == 18);
00270     assert(iter->second == 19);
00271     ++iter;
00272     assert(iter->first == 20);
00273     assert(iter->second == 26);
00274     ++iter;
00275     assert(iter == reg.myRegions.end());
00276     for(int i = 0; i < 30; i++)
00277     {
00278         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00279            ((i >= 13) && (i < 15)) || ((i >= 18) && (i < 19)) ||
00280            ((i >= 20) && (i < 26)))
00281         {
00282             assert(reg.inRegion(i) == true);
00283         }
00284         else
00285         {
00286             assert(reg.inRegion(i) == false);
00287         }
00288     }
00289 
00290     // Start is not in, but overlap two others (ending not at the end).
00291     reg.add(12,19);
00292     assert(reg.myRegions.size() == 4);
00293     assert(reg.myRegionIter->first == 12);
00294     assert(reg.myRegionIter->second == 19);
00295     iter = reg.myRegions.begin();
00296     assert(iter->first == 4);
00297     assert(iter->second == 6);
00298     ++iter;
00299     assert(iter->first == 8);
00300     assert(iter->second == 9);
00301     ++iter;
00302     assert(iter->first == 12);
00303     assert(iter->second == 19);
00304     ++iter;
00305     assert(iter->first == 20);
00306     assert(iter->second == 26);
00307     ++iter;
00308     assert(iter == reg.myRegions.end());
00309     for(int i = 0; i < 30; i++)
00310     {
00311         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00312            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)))
00313         {
00314             assert(reg.inRegion(i) == true);
00315         }
00316         else
00317         {
00318             assert(reg.inRegion(i) == false);
00319         }
00320     }
00321 
00322     // Completely in region to left.
00323     reg.add(5,6);
00324     assert(reg.myRegions.size() == 4);
00325     assert(reg.myRegionIter->first == 4);
00326     assert(reg.myRegionIter->second == 6);
00327     iter = reg.myRegions.begin();
00328     assert(iter->first == 4);
00329     assert(iter->second == 6);
00330     ++iter;
00331     assert(iter->first == 8);
00332     assert(iter->second == 9);
00333     ++iter;
00334     assert(iter->first == 12);
00335     assert(iter->second == 19);
00336     ++iter;
00337     assert(iter->first == 20);
00338     assert(iter->second == 26);
00339     ++iter;
00340     assert(iter == reg.myRegions.end());
00341     for(int i = 0; i < 30; i++)
00342     {
00343         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00344            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)))
00345         {
00346             assert(reg.inRegion(i) == true);
00347         }
00348         else
00349         {
00350             assert(reg.inRegion(i) == false);
00351         }
00352     }
00353 
00354     // Completely in region to right.
00355     reg.add(22,24);
00356     assert(reg.myRegions.size() == 4);
00357     assert(reg.myRegionIter->first == 20);
00358     assert(reg.myRegionIter->second == 26);
00359     iter = reg.myRegions.begin();
00360     assert(iter->first == 4);
00361     assert(iter->second == 6);
00362     ++iter;
00363     assert(iter->first == 8);
00364     assert(iter->second == 9);
00365     ++iter;
00366     assert(iter->first == 12);
00367     assert(iter->second == 19);
00368     ++iter;
00369     assert(iter->first == 20);
00370     assert(iter->second == 26);
00371     ++iter;
00372     assert(iter == reg.myRegions.end());
00373     for(int i = 0; i < 30; i++)
00374     {
00375         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00376            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)))
00377         {
00378             assert(reg.inRegion(i) == true);
00379         }
00380         else
00381         {
00382             assert(reg.inRegion(i) == false);
00383         }
00384     }
00385 
00386     // Add region to right.
00387     reg.add(28,29);
00388     assert(reg.myRegions.size() == 5);
00389     assert(reg.myRegionIter->first == 28);
00390     assert(reg.myRegionIter->second == 29);
00391     iter = reg.myRegions.begin();
00392     assert(iter->first == 4);
00393     assert(iter->second == 6);
00394     ++iter;
00395     assert(iter->first == 8);
00396     assert(iter->second == 9);
00397     ++iter;
00398     assert(iter->first == 12);
00399     assert(iter->second == 19);
00400     ++iter;
00401     assert(iter->first == 20);
00402     assert(iter->second == 26);
00403     ++iter;
00404     assert(iter->first == 28);
00405     assert(iter->second == 29);
00406     ++iter;
00407     assert(iter == reg.myRegions.end());
00408     for(int i = 0; i < 30; i++)
00409     {
00410         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 9)) ||
00411            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)) ||
00412            ((i >= 28) && (i < 29)))
00413         {
00414             assert(reg.inRegion(i) == true);
00415         }
00416         else
00417         {
00418             assert(reg.inRegion(i) == false);
00419         }
00420     }
00421 
00422     // Add region to left, start is in the region, and end extends past.
00423     reg.add(8,10);
00424     assert(reg.myRegions.size() == 5);
00425     assert(reg.myRegionIter->first == 8);
00426     assert(reg.myRegionIter->second == 10);
00427     iter = reg.myRegions.begin();
00428     assert(iter->first == 4);
00429     assert(iter->second == 6);
00430     ++iter;
00431     assert(iter->first == 8);
00432     assert(iter->second == 10);
00433     ++iter;
00434     assert(iter->first == 12);
00435     assert(iter->second == 19);
00436     ++iter;
00437     assert(iter->first == 20);
00438     assert(iter->second == 26);
00439     ++iter;
00440     assert(iter->first == 28);
00441     assert(iter->second == 29);
00442     ++iter;
00443     assert(iter == reg.myRegions.end());
00444     for(int i = 0; i < 30; i++)
00445     {
00446         if(((i >= 4) && (i < 6)) || ((i >= 8) && (i < 10)) ||
00447            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)) ||
00448            ((i >= 28) && (i < 29)))
00449         {
00450             assert(reg.inRegion(i) == true);
00451         }
00452         else
00453         {
00454             assert(reg.inRegion(i) == false);
00455         }
00456     }
00457 
00458     // Add region  start is in the region, and end extends past and overlaps
00459     // the next region.
00460     reg.add(5,9);
00461     assert(reg.myRegions.size() == 4);
00462     assert(reg.myRegionIter->first == 4);
00463     assert(reg.myRegionIter->second == 10);
00464     iter = reg.myRegions.begin();
00465     assert(iter->first == 4);
00466     assert(iter->second == 10);
00467     ++iter;
00468     assert(iter->first == 12);
00469     assert(iter->second == 19);
00470     ++iter;
00471     assert(iter->first == 20);
00472     assert(iter->second == 26);
00473     ++iter;
00474     assert(iter->first == 28);
00475     assert(iter->second == 29);
00476     ++iter;
00477     assert(iter == reg.myRegions.end());
00478     for(int i = 0; i < 30; i++)
00479     {
00480         if(((i >= 4) && (i < 10)) ||
00481            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)) ||
00482            ((i >= 28) && (i < 29)))
00483         {
00484             assert(reg.inRegion(i) == true);
00485         }
00486         else
00487         {
00488             assert(reg.inRegion(i) == false);
00489         }
00490     }
00491 
00492     // Add region  start is in the region, and end extends past and overlaps
00493     // the next region.
00494     reg.add(10,11);
00495     assert(reg.myRegions.size() == 5);
00496     assert(reg.myRegionIter->first == 10);
00497     assert(reg.myRegionIter->second == 11);
00498     iter = reg.myRegions.begin();
00499     assert(iter->first == 4);
00500     assert(iter->second == 10);
00501     ++iter;
00502     assert(iter->first == 10);
00503     assert(iter->second == 11);
00504     ++iter;
00505     assert(iter->first == 12);
00506     assert(iter->second == 19);
00507     ++iter;
00508     assert(iter->first == 20);
00509     assert(iter->second == 26);
00510     ++iter;
00511     assert(iter->first == 28);
00512     assert(iter->second == 29);
00513     ++iter;
00514     assert(iter == reg.myRegions.end());
00515     for(int i = 0; i < 30; i++)
00516     {
00517         if(((i >= 4) && (i < 10)) || ((i >= 10) && (i < 11)) ||
00518            ((i >= 12) && (i < 19)) || ((i >= 20) && (i < 26)) ||
00519            ((i >= 28) && (i < 29)))
00520         {
00521             assert(reg.inRegion(i) == true);
00522         }
00523         else
00524         {
00525             assert(reg.inRegion(i) == false);
00526         }
00527     }
00528 
00529     // Add region start is in the region, and end extends past and overlaps
00530     // the next 2 regions.
00531     reg.add(10,24);
00532     assert(reg.myRegions.size() == 3);
00533     assert(reg.myRegionIter->first == 10);
00534     assert(reg.myRegionIter->second == 26);
00535     iter = reg.myRegions.begin();
00536     assert(iter->first == 4);
00537     assert(iter->second == 10);
00538     ++iter;
00539     assert(iter->first == 10);
00540     assert(iter->second == 26);
00541     ++iter;
00542     assert(iter->first == 28);
00543     assert(iter->second == 29);
00544     ++iter;
00545     assert(iter == reg.myRegions.end());
00546     for(int i = 0; i < 30; i++)
00547     {
00548         if(((i >= 4) && (i < 10)) || ((i >= 10) && (i < 26)) ||
00549            ((i >= 28) && (i < 29)))
00550         {
00551             assert(reg.inRegion(i) == true);
00552         }
00553         else
00554         {
00555             assert(reg.inRegion(i) == false);
00556         }
00557     }
00558 
00559     // Add region start outside of a region and ends at the end.
00560     reg.add(2,30);
00561     assert(reg.myRegions.size() == 1);
00562     assert(reg.myRegionIter->first == 2);
00563     assert(reg.myRegionIter->second == 30);
00564     iter = reg.myRegions.begin();
00565     assert(iter->first == 2);
00566     assert(iter->second == 30);
00567     ++iter;
00568     assert(iter == reg.myRegions.end());
00569     for(int i = 0; i < 50; i++)
00570     {
00571         if(((i >= 2) && (i < 30)))
00572         {
00573             assert(reg.inRegion(i) == true);
00574         }
00575         else
00576         {
00577             assert(reg.inRegion(i) == false);
00578         }
00579     }
00580 
00581     // Add invalid region (start = end)
00582     reg.add(40,40);
00583     assert(reg.myRegions.size() == 1);
00584     iter = reg.myRegions.begin();
00585     assert(iter->first == 2);
00586     assert(iter->second == 30);
00587     ++iter;
00588     assert(iter == reg.myRegions.end());
00589     for(int i = 0; i < 50; i++)
00590     {
00591         if(((i >= 2) && (i < 30)))
00592         {
00593             assert(reg.inRegion(i) == true);
00594         }
00595         else
00596         {
00597             assert(reg.inRegion(i) == false);
00598         }
00599     }
00600 
00601     // Add invalid region (start < end)
00602     reg.add(40, 38);
00603     assert(reg.myRegions.size() == 1);
00604     iter = reg.myRegions.begin();
00605     assert(iter->first == 2);
00606     assert(iter->second == 30);
00607     ++iter;
00608     assert(iter == reg.myRegions.end());
00609     for(int i = 0; i < 50; i++)
00610     {
00611         if(((i >= 2) && (i < 30)))
00612         {
00613             assert(reg.inRegion(i) == true);
00614         }
00615         else
00616         {
00617             assert(reg.inRegion(i) == false);
00618         }
00619     }
00620 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends