"Fossies" - the Fresh Open Source Software Archive

Member "scintilla/test/unit/testSplitVector.cxx" (24 Oct 2019, 8065 Bytes) of package /windows/misc/scite421.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "testSplitVector.cxx": 410_vs_411.

    1 // Unit Tests for Scintilla internal data structures
    2 
    3 #include <cstddef>
    4 #include <cstring>
    5 
    6 #include <stdexcept>
    7 #include <string_view>
    8 #include <vector>
    9 #include <algorithm>
   10 #include <memory>
   11 
   12 #include "Platform.h"
   13 
   14 #include "Position.h"
   15 #include "SplitVector.h"
   16 
   17 #include "catch.hpp"
   18 
   19 using namespace Scintilla;
   20 
   21 // Test SplitVector.
   22 
   23 struct StringSetHolder {
   24     SplitVector<std::string> sa;
   25     bool Check() {
   26         for (int i = 0; i < sa.Length(); i++) {
   27             if (sa[i].empty()) {
   28                 return false;
   29             }
   30         }
   31         return true;
   32     }
   33 };
   34 
   35 const int lengthTestArray = 4;
   36 static const int testArray[4] = {3, 4, 5, 6};
   37 
   38 TEST_CASE("SplitVector") {
   39 
   40     SplitVector<int> sv;
   41 
   42     SECTION("IsEmptyInitially") {
   43         REQUIRE(0 == sv.Length());
   44     }
   45 
   46     SECTION("InsertOne") {
   47         sv.InsertValue(0, 10, 0);
   48         sv.Insert(5, 3);
   49         REQUIRE(11 == sv.Length());
   50         for (int i=0; i<sv.Length(); i++) {
   51             REQUIRE(((i == 5) ? 3 : 0) == sv.ValueAt(i));
   52         }
   53     }
   54 
   55     SECTION("Insertion") {
   56         sv.InsertValue(0, 10, 0);
   57         REQUIRE(10 == sv.Length());
   58         for (int i=0; i<sv.Length(); i++) {
   59             REQUIRE(0 == sv.ValueAt(i));
   60         }
   61     }
   62 
   63     SECTION("InsertionString") {
   64         // This test failed an earlier version of SplitVector that copied backwards incorrectly
   65         StringSetHolder ssh;
   66         ssh.sa.Insert(0, "Alpha");
   67         REQUIRE(ssh.Check());
   68         ssh.sa.Insert(0, "Beta");
   69         REQUIRE(ssh.Check());
   70         ssh.sa.Insert(0, "Cat");
   71         REQUIRE(ssh.Check());
   72         ssh.sa.Insert(1, "Dog");
   73         REQUIRE(ssh.Check());
   74         ssh.sa.Insert(0, "Elephant");
   75         REQUIRE(ssh.Check());
   76         ssh.sa.Insert(1, "Fox");
   77         REQUIRE(ssh.Check());
   78         ssh.sa.Insert(0, "Grass");
   79         REQUIRE(ssh.Check());
   80         ssh.sa.Insert(1, "Hat");
   81         REQUIRE(ssh.Check());
   82         ssh.sa.Delete(4);
   83         REQUIRE(ssh.Check());
   84         ssh.sa.Insert(0, "Indigo");
   85         REQUIRE(ssh.Check());
   86         ssh.sa.Insert(1, "Jackal");
   87         REQUIRE(ssh.Check());
   88         ssh.sa.Insert(0, "Kanga");
   89         REQUIRE(ssh.Check());
   90         ssh.sa.Insert(1, "Lion");
   91         REQUIRE(ssh.Check());
   92         ssh.sa.Insert(0, "Mango");
   93         REQUIRE(ssh.Check());
   94         ssh.sa.Insert(1, "Neon");
   95         REQUIRE(ssh.Check());
   96     }
   97 
   98     SECTION("InsertionPattern") {
   99         sv.Insert(0, 1);    // 1
  100         sv.Insert(0, 2);    // 21
  101         sv.Insert(0, 3);    // 321
  102         sv.Insert(1, 4);    // 3421
  103         sv.Insert(0, 5);    // 53421
  104         sv.Insert(1, 6);    // 563421
  105         sv.Insert(0, 7);    // 7563421
  106         sv.Insert(1, 8);    // 78563421
  107 
  108         REQUIRE(8 == sv.Length());
  109 
  110         REQUIRE(7 == sv.ValueAt(0));
  111         REQUIRE(8 == sv.ValueAt(1));
  112         REQUIRE(5 == sv.ValueAt(2));
  113         REQUIRE(6 == sv.ValueAt(3));
  114         REQUIRE(3 == sv.ValueAt(4));
  115         REQUIRE(4 == sv.ValueAt(5));
  116         REQUIRE(2 == sv.ValueAt(6));
  117         REQUIRE(1 == sv.ValueAt(7));
  118 
  119         sv.Delete(4);   // 7856421
  120 
  121         REQUIRE(7 == sv.Length());
  122 
  123         REQUIRE(7 == sv.ValueAt(0));
  124         REQUIRE(8 == sv.ValueAt(1));
  125         REQUIRE(5 == sv.ValueAt(2));
  126         REQUIRE(6 == sv.ValueAt(3));
  127         REQUIRE(4 == sv.ValueAt(4));
  128         REQUIRE(2 == sv.ValueAt(5));
  129         REQUIRE(1 == sv.ValueAt(6));
  130 
  131         sv.Insert(0, 9);        // 97856421
  132         sv.Insert(1, 0xa);  // 9a7856421
  133         sv.Insert(0, 0xb);  // b9a7856421
  134         sv.Insert(1, 0xc);  // bc9a7856421
  135         sv.Insert(0, 0xd);  // dbc9a7856421
  136         sv.Insert(1, 0xe);  // debc9a7856421
  137 
  138         REQUIRE(13 == sv.Length());
  139 
  140         REQUIRE(0xd == sv.ValueAt(0));
  141         REQUIRE(0xe == sv.ValueAt(1));
  142         REQUIRE(0xb == sv.ValueAt(2));
  143         REQUIRE(0xc == sv.ValueAt(3));
  144         REQUIRE(9 == sv.ValueAt(4));
  145         REQUIRE(0xa == sv.ValueAt(5));
  146         REQUIRE(7 == sv.ValueAt(6));
  147         REQUIRE(8 == sv.ValueAt(7));
  148         REQUIRE(5 == sv.ValueAt(8));
  149         REQUIRE(6 == sv.ValueAt(9));
  150         REQUIRE(4 == sv.ValueAt(10));
  151         REQUIRE(2 == sv.ValueAt(11));
  152         REQUIRE(1 == sv.ValueAt(12));
  153     }
  154 
  155     SECTION("EnsureLength") {
  156         sv.EnsureLength(4);
  157         REQUIRE(4 == sv.Length());
  158         for (int i=0; i<sv.Length(); i++) {
  159             REQUIRE(0 == sv.ValueAt(i));
  160         }
  161     }
  162 
  163     SECTION("InsertFromArray") {
  164         sv.InsertFromArray(0, testArray, 0, lengthTestArray);
  165         REQUIRE(lengthTestArray == sv.Length());
  166         for (int i=0; i<sv.Length(); i++) {
  167             REQUIRE((i+3) == sv.ValueAt(i));
  168         }
  169     }
  170 
  171     SECTION("SetValue") {
  172         sv.InsertValue(0, 10, 0);
  173         sv.SetValueAt(5, 3);
  174         REQUIRE(10 == sv.Length());
  175         for (int i=0; i<sv.Length(); i++) {
  176             REQUIRE(((i == 5) ? 3 : 0) == sv.ValueAt(i));
  177         }
  178         // Move the gap
  179         sv.InsertValue(7, 1, 17);
  180         REQUIRE(17 == sv.ValueAt(7));
  181         REQUIRE(0 == sv.ValueAt(8));
  182         // Set after the gap
  183         sv.SetValueAt(8, 19);
  184         REQUIRE(19 == sv.ValueAt(8));
  185     }
  186 
  187     SECTION("Indexing") {
  188         sv.InsertValue(0, 10, 0);
  189         sv.SetValueAt(5, 3);
  190         REQUIRE(10 == sv.Length());
  191         for (int i=0; i<sv.Length(); i++) {
  192             REQUIRE(((i == 5) ? 3 : 0) == sv[i]);
  193         }
  194 }
  195 
  196     SECTION("Fill") {
  197         sv.InsertValue(0, 10, 0);
  198         REQUIRE(10 == sv.Length());
  199         sv.InsertValue(7, 1, 1);
  200         REQUIRE(11 == sv.Length());
  201         for (int i=0; i<sv.Length(); i++) {
  202             REQUIRE(((i == 7) ? 1 : 0) == sv.ValueAt(i));
  203         }
  204     }
  205 
  206     SECTION("DeleteOne") {
  207         sv.InsertFromArray(0, testArray, 0, lengthTestArray);
  208         sv.Delete(2);
  209         REQUIRE((lengthTestArray-1) == sv.Length());
  210         REQUIRE(3 == sv[0]);
  211         REQUIRE(4 == sv[1]);
  212         REQUIRE(6 == sv[2]);
  213     }
  214 
  215     SECTION("DeleteRange") {
  216         sv.InsertValue(0, 10, 0);
  217         REQUIRE(10 == sv.Length());
  218         sv.InsertValue(7, 1, 1);
  219         REQUIRE(11 == sv.Length());
  220         sv.DeleteRange(2, 3);
  221         REQUIRE(8 == sv.Length());
  222         for (int i=0; i<sv.Length(); i++) {
  223             REQUIRE(((i == 4) ? 1 : 0) == sv.ValueAt(i));
  224         }
  225     }
  226 
  227     SECTION("DeleteAll") {
  228         sv.InsertValue(0, 10, 0);
  229         sv.InsertValue(7, 1, 1);
  230         sv.DeleteRange(2, 3);
  231         sv.DeleteAll();
  232         REQUIRE(0 == sv.Length());
  233     }
  234 
  235     SECTION("GetRange") {
  236         sv.InsertValue(0, 10, 0);
  237         sv.InsertValue(7, 1, 1);
  238         int retrieveArray[11] = {0};
  239         sv.GetRange(retrieveArray, 0, 11);
  240         for (int i=0; i<sv.Length(); i++) {
  241             REQUIRE(((i==7) ? 1 : 0) == retrieveArray[i]);
  242         }
  243     }
  244 
  245     SECTION("GetRangeOverGap") {
  246         sv.InsertFromArray(0, testArray, 0, lengthTestArray);
  247         REQUIRE(lengthTestArray == sv.Length());
  248         int retrieveArray[lengthTestArray] = {0};
  249         sv.GetRange(retrieveArray, 0, lengthTestArray);
  250         for (int i=0; i<sv.Length(); i++) {
  251             REQUIRE((i+3) == retrieveArray[i]);
  252         }
  253     }
  254 
  255     SECTION("ReplaceUp") {
  256         // Replace each element by inserting and then deleting the displaced element
  257         // This should perform many moves
  258         const int testLength=105;
  259         sv.EnsureLength(testLength);
  260         for (int i=0; i<testLength; i++)
  261             sv.SetValueAt(i, i+2);
  262         REQUIRE(testLength == sv.Length());
  263         for (int i=0; i<sv.Length(); i++) {
  264             sv.InsertValue(i, 1, i+9);
  265             sv.Delete(i+1);
  266         }
  267         for (int i=0; i<sv.Length(); i++)
  268             REQUIRE((i+9) == sv.ValueAt(i));
  269     }
  270 
  271     SECTION("ReplaceDown") {
  272         // From the end, replace each element by inserting and then deleting the displaced element
  273         // This should perform many moves
  274         const int testLength=24;
  275         sv.EnsureLength(testLength);
  276         for (int i=0; i<testLength; i++)
  277             sv.SetValueAt(i, i+12);
  278         REQUIRE(testLength == sv.Length());
  279         for (ptrdiff_t i=sv.Length()-1; i>=0; i--) {
  280             sv.InsertValue(i, 1, static_cast<int>(i+5));
  281             sv.Delete(i+1);
  282         }
  283         for (int i=0; i<sv.Length(); i++)
  284             REQUIRE((i+5) == sv.ValueAt(i));
  285     }
  286 
  287     SECTION("BufferPointer") {
  288         // Low-level access to the data
  289         sv.InsertFromArray(0, testArray, 0, lengthTestArray);
  290         sv.Insert(0, 99);   // This moves the gap so that BufferPointer() must also move
  291         REQUIRE(1 == sv.GapPosition());
  292         const int lengthAfterInsertion = 1 + lengthTestArray;
  293         REQUIRE(lengthAfterInsertion == (sv.Length()));
  294         int *retrievePointer = sv.BufferPointer();
  295         for (int i=1; i<sv.Length(); i++) {
  296             REQUIRE((i+3-1) == retrievePointer[i]);
  297         }
  298         REQUIRE(lengthAfterInsertion == sv.Length());
  299         // Gap was moved to end.
  300         REQUIRE(lengthAfterInsertion == sv.GapPosition());
  301     }
  302 
  303     SECTION("DeleteBackAndForth") {
  304         sv.InsertValue(0, 10, 87);
  305         for (int i=0; i<10; i+=2) {
  306             int len = 10 - i;
  307             REQUIRE(len == sv.Length());
  308             for (int j=0; j<sv.Length(); j++) {
  309                 REQUIRE(87 == sv.ValueAt(j));
  310             }
  311             sv.Delete(len-1);
  312             sv.Delete(0);
  313         }
  314     }
  315 
  316     SECTION("GrowSize") {
  317         sv.SetGrowSize(5);
  318         REQUIRE(5 == sv.GetGrowSize());
  319     }
  320 
  321     SECTION("OutsideBounds") {
  322         sv.InsertValue(0, 10, 87);
  323         REQUIRE(0 == sv.ValueAt(-1));
  324         REQUIRE(0 == sv.ValueAt(10));
  325 
  326         /* Could be a death test as this asserts:
  327         sv.SetValueAt(-1,98);
  328         sv.SetValueAt(10,99);
  329         REQUIRE(0 == sv.ValueAt(-1));
  330         REQUIRE(0 == sv.ValueAt(10));
  331         */
  332     }
  333 
  334 }