1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
|
/** @file testCellBuffer.cxx
** Unit Tests for Scintilla internal data structures
**/
#include <cstddef>
#include <cassert>
#include <cstring>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <optional>
#include <algorithm>
#include <memory>
#include "ScintillaTypes.h"
#include "Debugging.h"
#include "Position.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
#include "SparseVector.h"
#include "ChangeHistory.h"
#include "CellBuffer.h"
#include "UndoHistory.h"
#include "catch.hpp"
using namespace Scintilla;
using namespace Scintilla::Internal;
// Test CellBuffer.
bool Equal(const char *ptr, std::string_view sv) noexcept {
return memcmp(ptr, sv.data(), sv.length()) == 0;
}
TEST_CASE("ScrapStack") {
ScrapStack ss;
SECTION("Push") {
const char *t = ss.Push("abc", 3);
REQUIRE(memcmp(t, "abc", 3) == 0);
ss.MoveBack(3);
const char *text = ss.CurrentText();
REQUIRE(memcmp(text, "abc", 3) == 0);
ss.MoveForward(1);
const char *text2 = ss.CurrentText();
REQUIRE(memcmp(text2, "bc", 2) == 0);
ss.SetCurrent(1);
const char *text3 = ss.CurrentText();
REQUIRE(memcmp(text3, "bc", 2) == 0);
const char *text4 = ss.TextAt(2);
REQUIRE(memcmp(text4, "c", 1) == 0);
ss.Clear();
const char *text5 = ss.Push("1", 1);
REQUIRE(memcmp(text5, "1", 1) == 0);
}
}
TEST_CASE("CellBuffer") {
constexpr std::string_view sText = "Scintilla";
constexpr Sci::Position sLength = sText.length();
CellBuffer cb(true, false);
SECTION("InsertOneLine") {
bool startSequence = false;
const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence);
REQUIRE(startSequence);
REQUIRE(sLength == cb.Length());
REQUIRE(Equal(cpChange, sText));
REQUIRE(1 == cb.Lines());
REQUIRE(0 == cb.LineStart(0));
REQUIRE(0 == cb.LineFromPosition(0));
REQUIRE(sLength == cb.LineStart(1));
REQUIRE(0 == cb.LineFromPosition(static_cast<int>(sLength)));
REQUIRE(cb.CanUndo());
REQUIRE(!cb.CanRedo());
}
SECTION("InsertTwoLines") {
constexpr std::string_view sText2 = "Two\nLines";
constexpr Sci::Position sLength2 = sText2.length();
bool startSequence = false;
const char *cpChange = cb.InsertString(0, sText2.data(), sLength2, startSequence);
REQUIRE(startSequence);
REQUIRE(sLength2 == cb.Length());
REQUIRE(Equal(cpChange, sText2));
REQUIRE(2 == cb.Lines());
REQUIRE(0 == cb.LineStart(0));
REQUIRE(0 == cb.LineFromPosition(0));
REQUIRE(4 == cb.LineStart(1));
REQUIRE(1 == cb.LineFromPosition(5));
REQUIRE(sLength2 == cb.LineStart(2));
REQUIRE(1 == cb.LineFromPosition(sLength2));
REQUIRE(cb.CanUndo());
REQUIRE(!cb.CanRedo());
}
SECTION("LineEnds") {
// Check that various line ends produce correct result from LineEnd.
cb.SetLineEndTypes(LineEndType::Unicode);
bool startSequence = false;
{
// Unix \n
constexpr std::string_view sText2 = "Two\nLines";
constexpr Sci::Position sLength2 = sText2.length();
cb.InsertString(0, sText2.data(), sLength2, startSequence);
REQUIRE(3 == cb.LineEnd(0));
REQUIRE(sLength2 == cb.LineEnd(1));
cb.DeleteChars(0, sLength2, startSequence);
}
{
// Windows \r\n
constexpr std::string_view sText2 = "Two\r\nLines";
constexpr Sci::Position sLength2 = sText2.length();
cb.InsertString(0, sText2.data(), sLength2, startSequence);
REQUIRE(3 == cb.LineEnd(0));
REQUIRE(sLength2 == cb.LineEnd(1));
cb.DeleteChars(0, sLength2, startSequence);
}
{
// Old macOS \r
constexpr std::string_view sText2 = "Two\rLines";
constexpr Sci::Position sLength2 = sText2.length();
cb.InsertString(0, sText2.data(), sLength2, startSequence);
REQUIRE(3 == cb.LineEnd(0));
REQUIRE(sLength2 == cb.LineEnd(1));
cb.DeleteChars(0, sLength2, startSequence);
}
{
// Unicode NEL is U+0085 \xc2\x85
constexpr std::string_view sText2 = "Two\xc2\x85Lines";
constexpr Sci::Position sLength2 = sText2.length();
cb.InsertString(0, sText2.data(), sLength2, startSequence);
REQUIRE(3 == cb.LineEnd(0));
REQUIRE(sLength2 == cb.LineEnd(1));
cb.DeleteChars(0, sLength2, startSequence);
}
{
// Unicode LS line separator is U+2028 \xe2\x80\xa8
constexpr std::string_view sText2 = "Two\xe2\x80\xa8Lines";
constexpr Sci::Position sLength2 = sText2.length();
cb.InsertString(0, sText2.data(), sLength2, startSequence);
REQUIRE(3 == cb.LineEnd(0));
REQUIRE(sLength2 == cb.LineEnd(1));
cb.DeleteChars(0, sLength2, startSequence);
}
cb.SetLineEndTypes(LineEndType::Default);
}
SECTION("UndoOff") {
REQUIRE(cb.IsCollectingUndo());
cb.SetUndoCollection(false);
REQUIRE(!cb.IsCollectingUndo());
bool startSequence = false;
const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence);
REQUIRE(!startSequence);
REQUIRE(sLength == cb.Length());
REQUIRE(Equal(cpChange, sText));
REQUIRE(!cb.CanUndo());
REQUIRE(!cb.CanRedo());
}
SECTION("UndoRedo") {
constexpr std::string_view sTextDeleted = "ci";
constexpr std::string_view sTextAfterDeletion = "Sntilla";
bool startSequence = false;
const char *cpChange = cb.InsertString(0, sText.data(), sLength, startSequence);
REQUIRE(startSequence);
REQUIRE(sLength == cb.Length());
REQUIRE(Equal(cpChange, sText));
REQUIRE(Equal(cb.BufferPointer(), sText));
REQUIRE(cb.CanUndo());
REQUIRE(!cb.CanRedo());
const char *cpDeletion = cb.DeleteChars(1, 2, startSequence);
REQUIRE(startSequence);
REQUIRE(Equal(cpDeletion, sTextDeleted));
REQUIRE(Equal(cb.BufferPointer(), sTextAfterDeletion));
REQUIRE(cb.CanUndo());
REQUIRE(!cb.CanRedo());
int steps = cb.StartUndo();
REQUIRE(steps == 1);
cb.PerformUndoStep();
REQUIRE(Equal(cb.BufferPointer(), sText));
REQUIRE(cb.CanUndo());
REQUIRE(cb.CanRedo());
steps = cb.StartUndo();
REQUIRE(steps == 1);
cb.PerformUndoStep();
REQUIRE(cb.Length() == 0);
REQUIRE(!cb.CanUndo());
REQUIRE(cb.CanRedo());
steps = cb.StartRedo();
REQUIRE(steps == 1);
cb.PerformRedoStep();
REQUIRE(Equal(cb.BufferPointer(), sText));
REQUIRE(cb.CanUndo());
REQUIRE(cb.CanRedo());
steps = cb.StartRedo();
REQUIRE(steps == 1);
cb.PerformRedoStep();
REQUIRE(Equal(cb.BufferPointer(), sTextAfterDeletion));
REQUIRE(cb.CanUndo());
REQUIRE(!cb.CanRedo());
cb.DeleteUndoHistory();
REQUIRE(!cb.CanUndo());
REQUIRE(!cb.CanRedo());
}
SECTION("LineEndTypes") {
REQUIRE(cb.GetLineEndTypes() == LineEndType::Default);
cb.SetLineEndTypes(LineEndType::Unicode);
REQUIRE(cb.GetLineEndTypes() == LineEndType::Unicode);
cb.SetLineEndTypes(LineEndType::Default);
REQUIRE(cb.GetLineEndTypes() == LineEndType::Default);
}
SECTION("ReadOnly") {
REQUIRE(!cb.IsReadOnly());
cb.SetReadOnly(true);
REQUIRE(cb.IsReadOnly());
bool startSequence = false;
cb.InsertString(0, sText.data(), sLength, startSequence);
REQUIRE(cb.Length() == 0);
}
}
bool Equal(const Action &a, ActionType at, Sci::Position position, std::string_view value) noexcept {
// Currently ignores mayCoalesce since this is not set consistently when following
// start action implies it.
if (a.at != at)
return false;
if (a.position != position)
return false;
if (a.lenData != static_cast<Sci::Position>(value.length()))
return false;
if (memcmp(a.data, value.data(), a.lenData) != 0)
return false;
return true;
}
bool EqualContainerAction(const Action &a, Sci::Position token) noexcept {
// Currently ignores mayCoalesce
if (a.at != ActionType::container)
return false;
if (a.position != token)
return false;
if (a.lenData != 0)
return false;
if (a.data)
return false;
return true;
}
void TentativeUndo(UndoHistory &uh) noexcept {
const int steps = uh.TentativeSteps();
for (int step = 0; step < steps; step++) {
/* const Action &actionStep = */ uh.GetUndoStep();
uh.CompletedUndoStep();
}
uh.TentativeCommit();
}
TEST_CASE("ScaledVector") {
ScaledVector sv;
SECTION("ScalingUp") {
sv.ReSize(1);
REQUIRE(sv.SizeInBytes() == 1);
REQUIRE(sv.ValueAt(0) == 0);
sv.SetValueAt(0, 1);
REQUIRE(sv.ValueAt(0) == 1);
REQUIRE(sv.SignedValueAt(0) == 1);
sv.ClearValueAt(0);
REQUIRE(sv.ValueAt(0) == 0);
// Check boundary of 1-byte values
sv.SetValueAt(0, 0xff);
REQUIRE(sv.ValueAt(0) == 0xff);
REQUIRE(sv.SizeInBytes() == 1);
// Require expansion to 2 byte elements
sv.SetValueAt(0, 0x100);
REQUIRE(sv.ValueAt(0) == 0x100);
REQUIRE(sv.SizeInBytes() == 2);
// Only ever expands, never diminishes element size
sv.SetValueAt(0, 0xff);
REQUIRE(sv.ValueAt(0) == 0xff);
REQUIRE(sv.SizeInBytes() == 2);
// Check boundary of 2-byte values
sv.SetValueAt(0, 0xffff);
REQUIRE(sv.ValueAt(0) == 0xffff);
REQUIRE(sv.SizeInBytes() == 2);
// Require expansion to 2 byte elements
sv.SetValueAt(0, 0x10000);
REQUIRE(sv.ValueAt(0) == 0x10000);
REQUIRE(sv.SizeInBytes() == 3);
// Check that its not just simple bit patterns that work
sv.SetValueAt(0, 0xd4381);
REQUIRE(sv.ValueAt(0) == 0xd4381);
// Add a second item
sv.ReSize(2);
REQUIRE(sv.SizeInBytes() == 6);
// Truncate
sv.Truncate(1);
REQUIRE(sv.SizeInBytes() == 3);
REQUIRE(sv.ValueAt(0) == 0xd4381);
sv.Clear();
REQUIRE(sv.Size() == 0);
sv.PushBack();
REQUIRE(sv.Size() == 1);
REQUIRE(sv.SizeInBytes() == 3);
sv.SetValueAt(0, 0x1fd4381);
REQUIRE(sv.SizeInBytes() == 4);
REQUIRE(sv.ValueAt(0) == 0x1fd4381);
}
}
TEST_CASE("UndoHistory") {
UndoHistory uh;
SECTION("Basics") {
REQUIRE(uh.IsSavePoint());
REQUIRE(uh.AfterSavePoint());
REQUIRE(!uh.BeforeSavePoint());
REQUIRE(!uh.BeforeReachableSavePoint());
REQUIRE(!uh.CanUndo());
REQUIRE(!uh.CanRedo());
bool startSequence = false;
const char *val = uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(startSequence);
REQUIRE(!uh.IsSavePoint());
REQUIRE(uh.AfterSavePoint());
REQUIRE(uh.CanUndo());
REQUIRE(!uh.CanRedo());
val = uh.AppendAction(ActionType::remove, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(startSequence);
// Undoing
{
const int steps = uh.StartUndo();
REQUIRE(steps == 1);
const Action action = uh.GetUndoStep();
REQUIRE(Equal(action, ActionType::remove, 0, "ab"));
uh.CompletedUndoStep();
}
{
const int steps = uh.StartUndo();
REQUIRE(steps == 1);
const Action action = uh.GetUndoStep();
REQUIRE(Equal(action, ActionType::insert, 0, "ab"));
uh.CompletedUndoStep();
}
REQUIRE(uh.IsSavePoint());
// Redoing
{
const int steps = uh.StartRedo();
REQUIRE(steps == 1);
const Action action = uh.GetRedoStep();
REQUIRE(Equal(action, ActionType::insert, 0, "ab"));
uh.CompletedRedoStep();
}
{
const int steps = uh.StartRedo();
REQUIRE(steps == 1);
const Action action = uh.GetRedoStep();
REQUIRE(Equal(action, ActionType::remove, 0, "ab"));
uh.CompletedRedoStep();
}
REQUIRE(!uh.IsSavePoint());
}
SECTION("EnsureTruncationAfterUndo") {
REQUIRE(uh.Actions() == 0);
bool startSequence = false;
uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(uh.Actions() == 1);
uh.AppendAction(ActionType::insert, 2, "cd", 2, startSequence, true);
REQUIRE(uh.Actions() == 2);
REQUIRE(uh.CanUndo());
REQUIRE(!uh.CanRedo());
// Undoing
const int steps = uh.StartUndo();
REQUIRE(steps == 2);
uh.GetUndoStep();
uh.CompletedUndoStep();
REQUIRE(uh.Actions() == 2); // Not truncated until forward action
uh.GetUndoStep();
uh.CompletedUndoStep();
REQUIRE(uh.Actions() == 2);
// Perform action which should truncate history
uh.AppendAction(ActionType::insert, 0, "12", 2, startSequence, true);
REQUIRE(uh.Actions() == 1);
}
SECTION("Coalesce") {
bool startSequence = false;
const char *val = uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(startSequence);
REQUIRE(!uh.IsSavePoint());
REQUIRE(uh.AfterSavePoint());
REQUIRE(uh.CanUndo());
REQUIRE(!uh.CanRedo());
val = uh.AppendAction(ActionType::insert, 2, "cd", 2, startSequence, true);
REQUIRE(memcmp(val, "cd", 2) == 0);
REQUIRE(!startSequence);
// Undoing
{
const int steps = uh.StartUndo();
REQUIRE(steps == 2);
const Action action2 = uh.GetUndoStep();
REQUIRE(Equal(action2, ActionType::insert, 2, "cd"));
uh.CompletedUndoStep();
const Action action1 = uh.GetUndoStep();
REQUIRE(Equal(action1, ActionType::insert, 0, "ab"));
uh.CompletedUndoStep();
}
REQUIRE(uh.IsSavePoint());
// Redoing
{
const int steps = uh.StartRedo();
REQUIRE(steps == 2);
const Action action1 = uh.GetRedoStep();
REQUIRE(Equal(action1, ActionType::insert, 0, "ab"));
uh.CompletedRedoStep();
const Action action2 = uh.GetRedoStep();
REQUIRE(Equal(action2, ActionType::insert, 2, "cd"));
uh.CompletedRedoStep();
}
REQUIRE(!uh.IsSavePoint());
}
SECTION("SimpleContainer") {
bool startSequence = false;
const char *val = uh.AppendAction(ActionType::container, 1000, nullptr, 0, startSequence, true);
REQUIRE(startSequence);
REQUIRE(!val);
val = uh.AppendAction(ActionType::container, 1001, nullptr, 0, startSequence, true);
REQUIRE(!startSequence);
REQUIRE(!val);
}
SECTION("CoalesceContainer") {
bool startSequence = false;
const char *val = uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(startSequence);
val = uh.AppendAction(ActionType::container, 1000, nullptr, 0, startSequence, true);
REQUIRE(!startSequence);
// container actions do not have text data, just the token store in position
REQUIRE(!val);
uh.AppendAction(ActionType::container, 1001, nullptr, 0, startSequence, true);
REQUIRE(!startSequence);
// This is a coalescible change since the container actions are skipped to determine compatibility
val = uh.AppendAction(ActionType::insert, 2, "cd", 2, startSequence, true);
REQUIRE(memcmp(val, "cd", 2) == 0);
REQUIRE(!startSequence);
// Break the sequence with a non-coalescible container action
uh.AppendAction(ActionType::container, 1002, nullptr, 0, startSequence, false);
REQUIRE(startSequence);
{
const int steps = uh.StartUndo();
REQUIRE(steps == 1);
const Action actionContainer = uh.GetUndoStep();
REQUIRE(EqualContainerAction(actionContainer, 1002));
REQUIRE(actionContainer.mayCoalesce == false);
uh.CompletedUndoStep();
}
{
const int steps = uh.StartUndo();
REQUIRE(steps == 4);
const Action actionInsert = uh.GetUndoStep();
REQUIRE(Equal(actionInsert, ActionType::insert, 2, "cd"));
uh.CompletedUndoStep();
{
const Action actionContainer = uh.GetUndoStep();
REQUIRE(EqualContainerAction(actionContainer, 1001));
uh.CompletedUndoStep();
}
{
const Action actionContainer = uh.GetUndoStep();
REQUIRE(EqualContainerAction(actionContainer, 1000));
uh.CompletedUndoStep();
}
{
const Action actionInsert1 = uh.GetUndoStep();
REQUIRE(Equal(actionInsert1, ActionType::insert, 0, "ab"));
uh.CompletedUndoStep();
}
}
// Reached beginning
REQUIRE(!uh.CanUndo());
}
SECTION("Grouping") {
uh.BeginUndoAction();
bool startSequence = false;
const char *val = uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(startSequence);
REQUIRE(!uh.IsSavePoint());
REQUIRE(uh.AfterSavePoint());
REQUIRE(uh.CanUndo());
REQUIRE(!uh.CanRedo());
val = uh.AppendAction(ActionType::remove, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(!startSequence);
val = uh.AppendAction(ActionType::insert, 0, "cde", 3, startSequence, true);
REQUIRE(memcmp(val, "cde", 3) == 0);
REQUIRE(!startSequence);
uh.EndUndoAction();
// Undoing
{
const int steps = uh.StartUndo();
REQUIRE(steps == 3);
const Action action3 = uh.GetUndoStep();
REQUIRE(Equal(action3, ActionType::insert, 0, "cde"));
uh.CompletedUndoStep();
const Action action2 = uh.GetUndoStep();
REQUIRE(Equal(action2, ActionType::remove, 0, "ab"));
uh.CompletedUndoStep();
const Action action1 = uh.GetUndoStep();
REQUIRE(Equal(action1, ActionType::insert, 0, "ab"));
uh.CompletedUndoStep();
}
REQUIRE(uh.IsSavePoint());
// Redoing
{
const int steps = uh.StartRedo();
REQUIRE(steps == 3);
const Action action1 = uh.GetRedoStep();
REQUIRE(Equal(action1, ActionType::insert, 0, "ab"));
uh.CompletedRedoStep();
const Action action2 = uh.GetRedoStep();
REQUIRE(Equal(action2, ActionType::remove, 0, "ab"));
uh.CompletedRedoStep();
const Action action3 = uh.GetRedoStep();
REQUIRE(Equal(action3, ActionType::insert, 0, "cde"));
uh.CompletedRedoStep();
}
REQUIRE(!uh.IsSavePoint());
}
SECTION("DeepGroup") {
uh.BeginUndoAction();
uh.BeginUndoAction();
bool startSequence = false;
const char *val = uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(startSequence);
val = uh.AppendAction(ActionType::container, 1000, nullptr, 0, startSequence, false);
REQUIRE(!val);
REQUIRE(!startSequence);
val = uh.AppendAction(ActionType::remove, 0, "ab", 2, startSequence, true);
REQUIRE(memcmp(val, "ab", 2) == 0);
REQUIRE(!startSequence);
val = uh.AppendAction(ActionType::insert, 0, "cde", 3, startSequence, true);
REQUIRE(memcmp(val, "cde", 3) == 0);
REQUIRE(!startSequence);
uh.EndUndoAction();
uh.EndUndoAction();
const int steps = uh.StartUndo();
REQUIRE(steps == 4);
}
SECTION("Tentative") {
REQUIRE(!uh.TentativeActive());
REQUIRE(uh.TentativeSteps() == -1);
uh.TentativeStart();
REQUIRE(uh.TentativeActive());
REQUIRE(uh.TentativeSteps() == 0);
bool startSequence = false;
uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(uh.TentativeActive());
REQUIRE(uh.TentativeSteps() == 1);
REQUIRE(uh.CanUndo());
uh.TentativeCommit();
REQUIRE(!uh.TentativeActive());
REQUIRE(uh.TentativeSteps() == -1);
REQUIRE(uh.CanUndo());
// TentativeUndo is the other important operation but it is performed by Document so add a local equivalent
uh.TentativeStart();
uh.AppendAction(ActionType::remove, 0, "ab", 2, startSequence, false);
uh.AppendAction(ActionType::insert, 0, "ab", 2, startSequence, true);
REQUIRE(uh.TentativeActive());
// The first TentativeCommit didn't seal off the first action so it is still undoable
REQUIRE(uh.TentativeSteps() == 2);
REQUIRE(uh.CanUndo());
TentativeUndo(uh);
REQUIRE(!uh.TentativeActive());
REQUIRE(uh.TentativeSteps() == -1);
REQUIRE(uh.CanUndo());
}
}
TEST_CASE("UndoActions") {
UndoActions ua;
SECTION("Basics") {
ua.PushBack();
REQUIRE(ua.SSize() == 1);
ua.Create(0, ActionType::insert, 0, 2, false);
REQUIRE(ua.AtStart(0));
REQUIRE(ua.LengthTo(0) == 0);
REQUIRE(ua.AtStart(1));
REQUIRE(ua.LengthTo(1) == 2);
ua.PushBack();
REQUIRE(ua.SSize() == 2);
ua.Create(0, ActionType::insert, 0, 2, false);
REQUIRE(ua.SSize() == 2);
ua.Truncate(1);
REQUIRE(ua.SSize() == 1);
ua.Clear();
REQUIRE(ua.SSize() == 0);
}
}
TEST_CASE("CharacterIndex") {
CellBuffer cb(true, false);
SECTION("Setup") {
REQUIRE(cb.LineCharacterIndex() == LineCharacterIndexType::None);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 0);
cb.SetUTF8Substance(true);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16);
REQUIRE(cb.LineCharacterIndex() == LineCharacterIndexType::Utf16);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 0);
cb.ReleaseLineCharacterIndex(LineCharacterIndexType::Utf16);
REQUIRE(cb.LineCharacterIndex() == LineCharacterIndexType::None);
}
SECTION("Insertion") {
cb.SetUTF8Substance(true);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32);
bool startSequence = false;
cb.InsertString(0, "a", 1, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 1);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 1);
constexpr std::string_view hwair = "\xF0\x90\x8D\x88";
cb.InsertString(0, hwair.data(), hwair.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 2);
}
SECTION("Deletion") {
cb.SetUTF8Substance(true);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32);
bool startSequence = false;
constexpr std::string_view hwair = "a\xF0\x90\x8D\x88z";
cb.InsertString(0, hwair.data(), hwair.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 4);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 3);
cb.DeleteChars(5, 1, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 2);
cb.DeleteChars(1, 4, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 1);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 1);
}
SECTION("Insert Complex") {
cb.SetUTF8Substance(true);
cb.SetLineEndTypes(LineEndType::Unicode);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32);
bool startSequence = false;
// 3 lines of text containing 8 bytes
constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz";
cb.InsertString(0, data.data(), data.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 5);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 6);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 4);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 5);
// Insert a new line at end -> "a\n\xF0\x90\x8D\x88\nz\n" 4 lines
// Last line empty
cb.InsertString(data.length(), "\n", 1, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 5);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 7);
REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf16) == 7);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 4);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 6);
REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf32) == 6);
// Insert a new line before end -> "a\n\xF0\x90\x8D\x88\nz\n\n" 5 lines
cb.InsertString(data.length(), "\n", 1, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 5);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 7);
REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf16) == 8);
REQUIRE(cb.IndexLineStart(5, LineCharacterIndexType::Utf16) == 8);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 4);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 6);
REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf32) == 7);
REQUIRE(cb.IndexLineStart(5, LineCharacterIndexType::Utf32) == 7);
// Insert a valid 3-byte UTF-8 character at start ->
// "\xE2\x82\xACa\n\xF0\x90\x8D\x88\nz\n\n" 5 lines
constexpr std::string_view euro = "\xE2\x82\xAC";
cb.InsertString(0, euro.data(), euro.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 6);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 8);
REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf16) == 9);
REQUIRE(cb.IndexLineStart(5, LineCharacterIndexType::Utf16) == 9);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 5);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 7);
REQUIRE(cb.IndexLineStart(4, LineCharacterIndexType::Utf32) == 8);
REQUIRE(cb.IndexLineStart(5, LineCharacterIndexType::Utf32) == 8);
// Insert a lone lead byte implying a 3 byte character at start of line 2 ->
// "\xE2\x82\xACa\n\EF\xF0\x90\x8D\x88\nz\n\n" 5 lines
// Should be treated as a single byte character
constexpr std::string_view lead = "\xEF";
cb.InsertString(5, lead.data(), lead.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 7);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 9);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 6);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 8);
// Insert an ASCII lead byte inside the 3-byte initial character ->
// "\xE2!\x82\xACa\n\EF\xF0\x90\x8D\x88\nz\n\n" 5 lines
// It should b treated as a single character and should cause the
// byte before and the 2 bytes after also be each treated as singles
// so 3 more characters on line 0.
constexpr std::string_view ascii = "!";
cb.InsertString(1, ascii.data(), ascii.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 6);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 10);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 6);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 9);
// Insert a NEL after the '!' to trigger the utf8 line end case ->
// "\xE2!\xC2\x85 \x82\xACa\n \EF\xF0\x90\x8D\x88\n z\n\n" 5 lines
constexpr std::string_view nel = "\xC2\x85";
cb.InsertString(2, nel.data(), nel.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 7);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 11);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 7);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 10);
}
SECTION("Delete Multiple lines") {
cb.SetUTF8Substance(true);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32);
bool startSequence = false;
// 3 lines of text containing 8 bytes
constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz\nc";
cb.InsertString(0, data.data(), data.length(), startSequence);
// Delete first 2 new lines -> "az\nc"
cb.DeleteChars(1, data.length() - 4, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 4);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 4);
}
SECTION("Delete Complex") {
cb.SetUTF8Substance(true);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32);
bool startSequence = false;
// 3 lines of text containing 8 bytes
constexpr std::string_view data = "a\n\xF0\x90\x8D\x88\nz";
cb.InsertString(0, data.data(), data.length(), startSequence);
// Delete lead byte from character on line 1 ->
// "a\n\x90\x8D\x88\nz"
// line 1 becomes 4 single byte characters
cb.DeleteChars(2, 1, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 6);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 7);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 6);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf32) == 7);
// Delete first new line ->
// "a\x90\x8D\x88\nz"
// Only 2 lines with line 0 containing 5 single byte characters
cb.DeleteChars(1, 1, startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 5);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 6);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 5);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 6);
// Restore lead byte from character on line 0 making a 4-byte character ->
// "a\xF0\x90\x8D\x88\nz"
constexpr std::string_view lead4 = "\xF0";
cb.InsertString(1, lead4.data(), lead4.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 4);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 5);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf32) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf32) == 3);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf32) == 4);
}
SECTION("Insert separates new line bytes") {
cb.SetUTF8Substance(true);
cb.AllocateLineCharacterIndex(LineCharacterIndexType::Utf16 | LineCharacterIndexType::Utf32);
bool startSequence = false;
// 2 lines of text containing 4 bytes
constexpr std::string_view data = "a\r\nb";
cb.InsertString(0, data.data(), data.length(), startSequence);
// 3 lines of text containing 5 bytes ->
// "a\r!\nb"
constexpr std::string_view ascii = "!";
cb.InsertString(2, ascii.data(), ascii.length(), startSequence);
REQUIRE(cb.IndexLineStart(0, LineCharacterIndexType::Utf16) == 0);
REQUIRE(cb.IndexLineStart(1, LineCharacterIndexType::Utf16) == 2);
REQUIRE(cb.IndexLineStart(2, LineCharacterIndexType::Utf16) == 4);
REQUIRE(cb.IndexLineStart(3, LineCharacterIndexType::Utf16) == 5);
}
}
TEST_CASE("ChangeHistory") {
ChangeHistory il;
struct Spanner {
Sci::Position position = 0;
Sci::Position length = 0;
};
SECTION("Start") {
REQUIRE(il.Length() == 0);
REQUIRE(il.DeletionCount(0,0) == 0);
REQUIRE(il.EditionAt(0) == 0);
REQUIRE(il.EditionEndRun(0) == 0);
REQUIRE(il.EditionDeletesAt(0) == 0);
REQUIRE(il.EditionNextDelete(0) == 1);
}
SECTION("Some Space") {
il.Insert(0, 10, false, true);
REQUIRE(il.Length() == 10);
REQUIRE(il.DeletionCount(0,10) == 0);
REQUIRE(il.EditionAt(0) == 0);
REQUIRE(il.EditionEndRun(0) == 10);
REQUIRE(il.EditionDeletesAt(0) == 0);
REQUIRE(il.EditionNextDelete(0) == 10);
REQUIRE(il.EditionDeletesAt(10) == 0);
REQUIRE(il.EditionNextDelete(10) == 11);
}
SECTION("An insert") {
il.Insert(0, 7, false, true);
il.SetSavePoint();
il.Insert(2, 3, true, true);
REQUIRE(il.Length() == 10);
REQUIRE(il.DeletionCount(0,10) == 0);
REQUIRE(il.EditionAt(0) == 0);
REQUIRE(il.EditionEndRun(0) == 2);
REQUIRE(il.EditionAt(2) == 2);
REQUIRE(il.EditionEndRun(2) == 5);
REQUIRE(il.EditionAt(5) == 0);
REQUIRE(il.EditionEndRun(5) == 10);
REQUIRE(il.EditionAt(10) == 0);
REQUIRE(il.EditionDeletesAt(0) == 0);
REQUIRE(il.EditionNextDelete(0) == 10);
REQUIRE(il.EditionDeletesAt(10) == 0);
}
SECTION("A delete") {
il.Insert(0, 10, false, true);
il.SetSavePoint();
il.DeleteRangeSavingHistory(2, 3, true, false);
REQUIRE(il.Length() == 7);
REQUIRE(il.DeletionCount(0,7) == 1);
REQUIRE(il.EditionAt(0) == 0);
REQUIRE(il.EditionEndRun(0) == 7);
REQUIRE(il.EditionAt(7) == 0);
REQUIRE(il.EditionDeletesAt(0) == 0);
const EditionSet one{ 2 };
REQUIRE(il.EditionNextDelete(0) == 2);
REQUIRE(il.EditionDeletesAt(2) == 2);
REQUIRE(il.EditionNextDelete(2) == 7);
REQUIRE(il.EditionDeletesAt(7) == 0);
}
SECTION("Insert, delete, and undo") {
il.Insert(0, 9, false, true);
il.SetSavePoint();
il.Insert(3, 1, true, true);
REQUIRE(il.EditionEndRun(0) == 3);
REQUIRE(il.EditionAt(3) == 2);
REQUIRE(il.EditionEndRun(3) == 4);
REQUIRE(il.EditionAt(4) == 0);
il.DeleteRangeSavingHistory(2, 3, true, false);
REQUIRE(il.Length() == 7);
REQUIRE(il.DeletionCount(0,7) == 1);
REQUIRE(il.EditionAt(0) == 0);
REQUIRE(il.EditionEndRun(0) == 7);
REQUIRE(il.EditionAt(7) == 0);
REQUIRE(il.EditionDeletesAt(0) == 0);
const EditionSet one{ 2 };
REQUIRE(il.EditionNextDelete(0) == 2);
REQUIRE(il.EditionDeletesAt(2) == 2);
REQUIRE(il.EditionNextDelete(2) == 7);
REQUIRE(il.EditionDeletesAt(7) == 0);
// Undo in detail (normally inside CellBuffer::PerformUndoStep)
il.UndoDeleteStep(2, 3, false);
REQUIRE(il.Length() == 10);
REQUIRE(il.DeletionCount(0, 10) == 0);
// The insertion has reappeared
REQUIRE(il.EditionEndRun(0) == 3);
REQUIRE(il.EditionAt(3) == 2);
REQUIRE(il.EditionEndRun(3) == 4);
REQUIRE(il.EditionAt(4) == 0);
}
SECTION("Deletes") {
il.Insert(0, 10, false, true);
il.SetSavePoint();
il.DeleteRangeSavingHistory(2, 3, true, false);
REQUIRE(il.Length() == 7);
REQUIRE(il.DeletionCount(0,7) == 1);
REQUIRE(il.EditionDeletesAt(0) == 0);
REQUIRE(il.EditionNextDelete(0) == 2);
REQUIRE(il.EditionDeletesAt(2) == 2);
REQUIRE(il.EditionNextDelete(2) == 7);
REQUIRE(il.EditionDeletesAt(7) == 0);
il.DeleteRangeSavingHistory(2, 1, true, false);
REQUIRE(il.Length() == 6);
REQUIRE(il.DeletionCount(0,6) == 2);
REQUIRE(il.EditionDeletesAt(0) == 0);
REQUIRE(il.EditionNextDelete(0) == 2);
REQUIRE(il.EditionDeletesAt(2) == 2);
REQUIRE(il.EditionNextDelete(2) == 6);
REQUIRE(il.EditionDeletesAt(6) == 0);
// Undo in detail (normally inside CellBuffer::PerformUndoStep)
il.UndoDeleteStep(2, 1, false);
REQUIRE(il.Length() == 7);
REQUIRE(il.DeletionCount(0, 7) == 1);
// Undo in detail (normally inside CellBuffer::PerformUndoStep)
il.UndoDeleteStep(2, 3, false);
REQUIRE(il.Length() == 10);
REQUIRE(il.DeletionCount(0, 10) == 0);
}
SECTION("Deletes 101") {
// Deletes that hit the start and end permanent positions
il.Insert(0, 3, false, true);
il.SetSavePoint();
REQUIRE(il.DeletionCount(0, 2) == 0);
il.DeleteRangeSavingHistory(1, 1, true, false);
REQUIRE(il.DeletionCount(0,2) == 1);
const EditionSet at1 = { {2, 1} };
REQUIRE(il.DeletionsAt(1) == at1);
il.DeleteRangeSavingHistory(1, 1, false, false);
REQUIRE(il.DeletionCount(0,1) == 2);
const EditionSet at2 = { {2, 1}, {3, 1} };
REQUIRE(il.DeletionsAt(1) == at2);
il.DeleteRangeSavingHistory(0, 1, false, false);
const EditionSet at3 = { {2, 1}, {3, 2} };
REQUIRE(il.DeletionsAt(0) == at3);
REQUIRE(il.DeletionCount(0,0) == 3);
// Undo them
il.UndoDeleteStep(0, 1, false);
REQUIRE(il.DeletionCount(0, 1) == 2);
REQUIRE(il.DeletionsAt(1) == at2);
il.UndoDeleteStep(1, 1, false);
REQUIRE(il.DeletionCount(0, 2) == 1);
REQUIRE(il.DeletionsAt(1) == at1);
il.UndoDeleteStep(1, 1, false);
REQUIRE(il.DeletionCount(0, 3) == 0);
}
SECTION("Deletes Stack") {
std::vector<Spanner> spans = {
{5, 1},
{4, 3},
{1, 1},
{1, 1},
{0, 1},
{0, 3},
};
// Deletes that hit the start and end permanent positions
il.Insert(0, 10, false, true);
REQUIRE(il.Length() == 10);
il.SetSavePoint();
REQUIRE(il.DeletionCount(0, 10) == 0);
for (size_t i = 0; i < std::size(spans); i++) {
il.DeleteRangeSavingHistory(spans[i].position, spans[i].length, false, false);
}
REQUIRE(il.Length() == 0);
for (size_t j = 0; j < std::size(spans); j++) {
const size_t i = std::size(spans) - j - 1;
il.UndoDeleteStep(spans[i].position, spans[i].length, false);
}
REQUIRE(il.DeletionCount(0, 10) == 0);
REQUIRE(il.Length() == 10);
}
SECTION("Delete Contiguous Backward") {
// Deletes that touch
constexpr Sci::Position length = 20;
constexpr Sci::Position rounds = 8;
il.Insert(0, length, false, true);
REQUIRE(il.Length() == length);
il.SetSavePoint();
for (Sci::Position i = 0; i < rounds; i++) {
il.DeleteRangeSavingHistory(9-i, 1, false, false);
}
constexpr Sci::Position lengthAfterDeletions = length - rounds;
REQUIRE(il.Length() == lengthAfterDeletions);
REQUIRE(il.DeletionCount(0, lengthAfterDeletions) == rounds);
for (Sci::Position j = 0; j < rounds; j++) {
il.UndoDeleteStep(2+j, 1, false);
}
// Restored to original
REQUIRE(il.DeletionCount(0, length) == 0);
REQUIRE(il.Length() == length);
}
SECTION("Delete Contiguous Forward") {
// Deletes that touch
constexpr size_t length = 20;
constexpr size_t rounds = 8;
il.Insert(0, length, false, true);
REQUIRE(il.Length() == length);
il.SetSavePoint();
for (size_t i = 0; i < rounds; i++) {
il.DeleteRangeSavingHistory(2,1, false, false);
}
constexpr size_t lengthAfterDeletions = length - rounds;
REQUIRE(il.Length() == lengthAfterDeletions);
REQUIRE(il.DeletionCount(0, lengthAfterDeletions) == rounds);
for (size_t j = 0; j < rounds; j++) {
il.UndoDeleteStep(2, 1, false);
}
// Restored to original
REQUIRE(il.Length() == length);
REQUIRE(il.DeletionCount(0, length) == 0);
}
}
struct InsertionResult {
Sci::Position position;
Sci::Position length;
int state;
bool operator==(const InsertionResult &other) const noexcept {
return position == other.position &&
length == other.length &&
state == other.state;
}
};
std::ostream &operator << (std::ostream &os, InsertionResult const &value) {
os << value.position << " " << value.length << " " << value.state;
return os;
}
using Insertions = std::vector<InsertionResult>;
std::ostream &operator << (std::ostream &os, Insertions const &value) {
os << "(";
for (const InsertionResult &el : value) {
os << "(" << el << ") ";
}
os << ")";
return os;
}
Insertions HistoryInsertions(const CellBuffer &cb) {
Insertions result;
Sci::Position startPos = 0;
while (startPos < cb.Length()) {
const Sci::Position endPos = cb.EditionEndRun(startPos);
const int ed = cb.EditionAt(startPos);
if (ed) {
result.push_back({ startPos, endPos - startPos, ed });
}
startPos = endPos;
}
return result;
}
struct DeletionResult {
Sci::Position position;
int state;
bool operator==(const DeletionResult &other) const noexcept {
return position == other.position &&
state == other.state;
}
};
std::ostream &operator << (std::ostream &os, DeletionResult const &value) {
os << value.position << " " << value.state;
return os;
}
using Deletions = std::vector<DeletionResult>;
std::ostream &operator << (std::ostream &os, Deletions const &value) {
os << "(";
for (const DeletionResult &el : value) {
os << "(" << el << ") ";
}
os << ")";
return os;
}
Deletions HistoryDeletions(const CellBuffer &cb) {
Deletions result;
Sci::Position positionDeletion = 0;
while (positionDeletion <= cb.Length()) {
const unsigned int editions = cb.EditionDeletesAt(positionDeletion);
if (editions & 1) {
result.push_back({ positionDeletion, 1 });
}
if (editions & 2) {
result.push_back({ positionDeletion, 2 });
}
if (editions & 4) {
result.push_back({ positionDeletion, 3 });
}
if (editions & 8) {
result.push_back({ positionDeletion, 4 });
}
positionDeletion = cb.EditionNextDelete(positionDeletion);
}
return result;
}
struct History {
Insertions insertions;
Deletions deletions;
bool operator==(const History &other) const {
return insertions == other.insertions &&
deletions == other.deletions;
}
};
std::ostream &operator << (std::ostream &os, History const &value) {
os << value.insertions << " " << value.deletions;
return os;
}
History HistoryOf(const CellBuffer &cb) {
return { HistoryInsertions(cb), HistoryDeletions(cb) };
}
void UndoBlock(CellBuffer &cb) {
const int steps = cb.StartUndo();
for (int step = 0; step < steps; step++) {
cb.PerformUndoStep();
}
}
void RedoBlock(CellBuffer &cb) {
const int steps = cb.StartRedo();
for (int step = 0; step < steps; step++) {
cb.PerformRedoStep();
}
}
TEST_CASE("CellBufferWithChangeHistory") {
SECTION("StraightUndoRedoSaveRevertRedo") {
CellBuffer cb(true, false);
cb.SetUndoCollection(false);
constexpr std::string_view sInsert = "abcdefghijklmnopqrstuvwxyz";
bool startSequence = false;
cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence);
cb.SetUndoCollection(true);
cb.SetSavePoint();
cb.ChangeHistorySet(true);
const History history0 { {}, {} };
REQUIRE(HistoryOf(cb) == history0);
// 1
cb.InsertString(4, "_", 1, startSequence);
const History history1{ {{4, 1, 3}}, {} };
REQUIRE(HistoryOf(cb) == history1);
// 2
cb.DeleteChars(2, 1, startSequence);
const History history2{ {{3, 1, 3}},
{{2, 3}} };
REQUIRE(HistoryOf(cb) == history2);
// 3
cb.InsertString(1, "[!]", 3, startSequence);
const History history3{ { {1, 3, 3}, {6, 1, 3} },
{ {5, 3} } };
REQUIRE(HistoryOf(cb) == history3);
// 4
cb.DeleteChars(2, 1, startSequence); // Inside an insertion
const History history4{ { {1, 2, 3}, {5, 1, 3} },
{ {2, 3}, {4, 3} }};
REQUIRE(HistoryOf(cb) == history4);
// 5 Delete all the insertions and deletions
cb.DeleteChars(1, 6, startSequence); // Inside an insertion
const History history5{ { },
{ {1, 3} } };
REQUIRE(HistoryOf(cb) == history5);
// Undo all
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == history4);
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == history3);
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == history2);
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == history1);
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == history0);
// Redo all
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history1);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history2);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history3);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history4);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history5);
cb.SetSavePoint();
const History history5s{ { },
{ {1, 2} } };
REQUIRE(HistoryOf(cb) == history5s);
// Change past save point
cb.InsertString(4, "123", 3, startSequence);
const History history6{ { {4, 3, 3} },
{ {1, 2} } };
REQUIRE(HistoryOf(cb) == history6);
// Undo to save point: same as 5 but with save state instead of unsaved
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == history5s);
// Reverting past save point, similar to 4 but with most saved and
// reverted delete at 1
UndoBlock(cb); // Reinsert most of original changes
const History history4s{ { {1, 2, 4}, {3, 2, 1}, {5, 1, 4}, {6, 1, 1} },
{ {2, 2}, {4, 2} } };
REQUIRE(HistoryOf(cb) == history4s);
UndoBlock(cb); // Reinsert "!",
const History history3s{ { {1, 3, 4}, {4, 2, 1}, {6, 1, 4}, {7, 1, 1} },
{ {5, 2} } };
REQUIRE(HistoryOf(cb) == history3s);
UndoBlock(cb); // Revert insertion of [!]
const History history2s{ { {1, 2, 1}, {3, 1, 4}, {4, 1, 1} },
{ {1, 1}, {2, 2} } };
REQUIRE(HistoryOf(cb) == history2s);
UndoBlock(cb); // Revert deletion, inserts at 2
const History history1s{ { {1, 3, 1}, {4, 1, 4}, {5, 1, 1} },
{ {1, 1} } };
REQUIRE(HistoryOf(cb) == history1s);
UndoBlock(cb); // Revert insertion of _ at 4, drops middle insertion run
// So merges down to 1 insertion
const History history0s{ { {1, 4, 1} },
{ {1, 1}, {4, 1} } };
REQUIRE(HistoryOf(cb) == history0s);
// At origin but with changes from disk
// Now redo the steps
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history1s);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history2s);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history3s);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history4s);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history5s);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == history6);
}
SECTION("Detached") {
CellBuffer cb(true, false);
cb.SetUndoCollection(false);
constexpr std::string_view sInsert = "abcdefghijklmnopqrstuvwxyz";
bool startSequence = false;
cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence);
cb.SetUndoCollection(true);
cb.SetSavePoint();
cb.ChangeHistorySet(true);
const History history0{ {}, {} };
REQUIRE(HistoryOf(cb) == history0);
// 1
cb.InsertString(4, "_", 1, startSequence);
const History history1{ {{4, 1, 3}}, {} };
REQUIRE(HistoryOf(cb) == history1);
// 2
cb.DeleteChars(2, 1, startSequence);
const History history2{ {{3, 1, 3}},
{{2, 3}} };
REQUIRE(HistoryOf(cb) == history2);
cb.SetSavePoint();
UndoBlock(cb);
const History history1s{ {{2, 1, 1}, {4, 1, 2}}, {} };
REQUIRE(HistoryOf(cb) == history1s);
cb.InsertString(6, "()", 2, startSequence);
const History detached2{ {{2, 1, 1}, {4, 1, 2}, {6, 2, 3}}, {} };
REQUIRE(HistoryOf(cb) == detached2);
cb.DeleteChars(9, 3, startSequence);
const History detached3{ {{2, 1, 1}, {4, 1, 2}, {6, 2, 3}}, {{9,3}} };
REQUIRE(HistoryOf(cb) == detached3);
UndoBlock(cb);
REQUIRE(HistoryOf(cb) == detached2);
UndoBlock(cb);
const History detached1{ {{2, 1, 1}, {4, 1, 2}}, {} };
REQUIRE(HistoryOf(cb) == detached1);
UndoBlock(cb);
const History detached0{ {{2, 1, 1}}, {{4,1}} };
REQUIRE(HistoryOf(cb) == detached0);
REQUIRE(!cb.CanUndo());
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == detached1);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == detached2);
RedoBlock(cb);
REQUIRE(HistoryOf(cb) == detached3);
}
}
namespace {
void PushUndoAction(CellBuffer &cb, int type, Sci::Position pos, std::string_view sv) {
cb.PushUndoActionType(type, pos);
cb.ChangeLastUndoActionText(sv.length(), sv.data());
}
}
TEST_CASE("CellBufferLoadUndoHistory") {
CellBuffer cb(false, false);
constexpr int remove = 1;
constexpr int insert = 0;
SECTION("Basics") {
cb.SetUndoCollection(false);
constexpr std::string_view sInsert = "abcdef";
bool startSequence = false;
cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence);
cb.SetUndoCollection(true);
cb.ChangeHistorySet(true);
// Create an undo history that matches the contents at current point 2
// So, 2 actions; current point; 2 actions
// a_cdef
PushUndoAction(cb, remove, 1, "_");
// acdef
PushUndoAction(cb, insert, 1, "b");
// abcdef -> current
PushUndoAction(cb, remove, 3, "d");
// abcef -> save
PushUndoAction(cb, insert, 3, "*");
// abc*ef
cb.SetUndoSavePoint(3);
cb.SetUndoDetach(-1);
cb.SetUndoTentative(-1);
cb.SetUndoCurrent(2);
// 2nd insertion is removed from change history as it isn't visible and isn't saved
// 2nd deletion is visible (as insertion) as it was saved but then reverted to original
// 1st insertion and 1st deletion are both visible as saved
const History hist{ {{1, 1, changeSaved}, {3, 1, changeRevertedOriginal}}, {{2, changeSaved}} };
REQUIRE(HistoryOf(cb) == hist);
}
SECTION("Detached") {
cb.SetUndoCollection(false);
constexpr std::string_view sInsert = "a-b=cdef";
bool startSequence = false;
cb.InsertString(0, sInsert.data(), sInsert.length(), startSequence);
cb.SetUndoCollection(true);
cb.ChangeHistorySet(true);
// Create an undo history that matches the contents at current point 2 which detached at 1
// So, insert saved; insert detached; current point
// abcdef
PushUndoAction(cb, insert, 1, "-");
// a-bcdef
PushUndoAction(cb, insert, 3, "=");
// a-b=cdef
cb.SetUndoSavePoint(-1);
cb.SetUndoDetach(1);
cb.SetUndoTentative(-1);
cb.SetUndoCurrent(2);
// This doesn't show elements due to undo.
// There was also a modified delete (reverting the insert) at 3 in the original but that is missing.
const History hist{ {{1, 1, changeSaved}, {3, 1, changeModified}}, {} };
REQUIRE(HistoryOf(cb) == hist);
}
}
namespace {
// Implement low quality reproducible pseudo-random numbers.
// Pseudo-random algorithm based on R. G. Dromey "How to Solve it by Computer" page 122.
class RandomSequence {
static constexpr int mult = 109;
static constexpr int incr = 853;
static constexpr int modulus = 4096;
int randomValue = 127;
public:
int Next() noexcept {
randomValue = (mult * randomValue + incr) % modulus;
return randomValue;
}
};
}
#if 1
TEST_CASE("CellBufferLong") {
// Call methods on CellBuffer pseudo-randomly trying to trigger assertion failures
CellBuffer cb(true, false);
SECTION("Random") {
RandomSequence rseq;
for (size_t i = 0; i < 20000; i++) {
const int r = rseq.Next() % 10;
if (r <= 2) { // 30%
// Insert text
const Sci::Position pos = rseq.Next() % (cb.Length() + 1);
const int len = rseq.Next() % 10 + 1;
std::string sInsert;
for (int j = 0; j < len; j++) {
sInsert.push_back(static_cast<char>('a' + j));
}
bool startSequence = false;
cb.InsertString(pos, sInsert.c_str(), len, startSequence);
} else if (r <= 5) { // 30%
// Delete Text
const Sci::Position pos = rseq.Next() % (cb.Length() + 1);
const int len = rseq.Next() % 10 + 1;
if (pos + len <= cb.Length()) {
bool startSequence = false;
cb.DeleteChars(pos, len, startSequence);
}
} else if (r <= 8) { // 30%
// Undo or redo
const bool undo = rseq.Next() % 2 == 1;
if (undo) {
UndoBlock(cb);
} else {
RedoBlock(cb);
}
} else { // 10%
// Save
cb.SetSavePoint();
}
}
}
}
#endif
|