Created
August 7, 2017 15:12
-
-
Save EricGrange/8e625d5d734c897f254fb0968c64dd77 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| procedure TForm93.BBMonkeyClick(Sender: TObject); | |
| const | |
| cCommands : array [0..61] of Integer = ( | |
| ecChar, ecString, ecChar, ecString, ecChar, ecString, ecChar, ecString, | |
| //ecChar, ecChar, ecChar, ecChar, ecChar, ecChar, | |
| ecTab, ecTab, ecShiftTab, | |
| ecLineBreak, ecLineBreak, ecLineBreak, ecInsertLine, | |
| ecDeleteChar, ecBackspace, ecDeleteLine, //ecDeleteWord, | |
| ecDeleteBeginningOfLine, //ecDeleteEndOfLine, | |
| ecLeft, ecRight, ecUp, ecDown, ecPageUp, ecPageDown, | |
| ecSelectionLeft, ecSelectionRight, ecSelectionUp, ecSelectionDown, | |
| ecLineStart, ecLineEnd, ecWordLeft, ecWordRight, | |
| ecSelectionLineStart, ecSelectionLineEnd, ecSelectionWordLeft, ecSelectionWordRight, | |
| ecEditorTop, ecEditorBottom, | |
| ecSelectionEditorTop, ecSelectionEditorBottom, | |
| ecPageUp, ecPageDown, | |
| ecSelectionPageUp, ecSelectionPageDown, | |
| ecSelectionLeft, ecSelectionRight, ecSelectionUp, ecSelectionDown, | |
| // ecInsertMode, ecOverwriteMode, | |
| ecLineComment, ecBlockComment, | |
| ecBlockIndent, ecBlockUnindent, | |
| ecCopy, ecCut, ecPaste, | |
| ecUndo, ecRedo, | |
| ecMoveLineUp, ecMoveLineDown, ecMoveCharLeft, ecMoveCharRight | |
| ); | |
| cNBActions = 3; | |
| function MonkeyRun(seed : Integer; skip, nbActions : Integer) : String; | |
| var | |
| s : TStringStream; | |
| j, k, cmd : Integer; | |
| u : Boolean; | |
| ident, finalState : String; | |
| sample : String; | |
| begin | |
| RandSeed := seed; | |
| case Random(10) of | |
| 0..2 : sample := ',,'; | |
| 3..6 : sample := '1,'#9'22,333, 4444,55555'; | |
| else | |
| sample := '1,2,3,4,5' | |
| end; | |
| Editor.Clear; | |
| s := TStringStream.Create(StrReplaceAll(',', #13#10, sample)+#13#10); | |
| Editor.LoadFromStream(s); | |
| s.Free; | |
| Editor.InsertMode := True; | |
| TClipboardHack(Clipboard).HardOpen; | |
| try | |
| Clipboard.AsText := 'b'; | |
| finally | |
| TClipboardHack(Clipboard).HardClose; | |
| end; | |
| RandSeed := seed; | |
| u := False; | |
| for j := 1 to skip+nbActions do begin | |
| cmd := cCommands[Random(Length(cCommands))]; | |
| if j <= skip then continue; | |
| case cmd of | |
| ecCut, ecCopy, ecPaste : begin | |
| TClipboardHack(Clipboard).HardOpen; | |
| try | |
| Editor.ExecuteCommand(cmd, 'a', nil); | |
| finally | |
| TClipboardHack(Clipboard).HardClose; | |
| end; | |
| end; | |
| ecString : | |
| for k := 1 to Random(10)+1 do | |
| Editor.ExecuteCommand(ecChar, 'c', nil); | |
| ecUndo, ecRedo : begin | |
| u := True; | |
| Editor.ExecuteCommand(ecUndo, #0, nil); | |
| end; | |
| else | |
| Editor.ExecuteCommand(cmd, 'a', nil); | |
| end; | |
| end; | |
| finalState := Editor.Lines.CommaText; | |
| for j := 1 to nbActions do | |
| Editor.ExecuteCommand(ecUndo, #0, nil); | |
| if StrRemoveChar('"', Editor.Lines.CommaText) <> sample then begin | |
| RandSeed := seed; | |
| Result := 'Failed for RandSeed = ' + IntToStr(seed); | |
| for j:=1 to skip+nbActions do begin | |
| cmd := cCommands[Random(Length(cCommands))]; | |
| if j<=skip then continue; | |
| EditorCommandToIdent(cmd, ident); | |
| Result := Result + ', '+ ident; | |
| end; | |
| Exit; | |
| end; | |
| if not u then begin | |
| for j := 1 to nbActions do | |
| Editor.ExecuteCommand(ecRedo, #0, nil); | |
| if Editor.Lines.CommaText <> finalState then begin | |
| RandSeed := seed; | |
| Result := 'Failed Redo for RandSeed = ' + IntToStr(seed); | |
| for j:=1 to nbActions do begin | |
| cmd := cCommands[Random(Length(cCommands))]; | |
| EditorCommandToIdent(cmd, ident); | |
| Result := Result + ', '+ ident; | |
| end; | |
| Exit; | |
| end else Result := ''; | |
| for j := 1 to nbActions do | |
| Editor.ExecuteCommand(ecUndo, #0, nil); | |
| end else Result := ''; | |
| end; | |
| var | |
| i, len, skip : Integer; | |
| bestLen, bestSkip : Integer; | |
| run : String; | |
| begin | |
| for i:=1 to 100000 do begin | |
| Caption := IntToStr(i); | |
| if (i and 127)=0 then Application.ProcessMessages; | |
| run := MonkeyRun(i, 0, cNBActions); | |
| if run <> '' then begin | |
| {bestLen := cNBActions; | |
| bestSkip := 0; | |
| for len := cNBActions-1 downto 1 do begin | |
| Caption := 'Narrowing failure at RandSeed = ' + IntToStr(i) | |
| + ', best length = ' + IntToStr(bestLen); | |
| for skip := 0 to cNBActions-len do begin | |
| if MonkeyRun(i, skip, len) <> '' then begin | |
| bestLen := len; | |
| bestSkip := skip; | |
| break; | |
| end; | |
| end; | |
| end; | |
| run := MonkeyRun(i, bestSkip, bestLen); // } | |
| BBMonkey.Caption := Editor.Lines.CommaText; | |
| Clipboard.AsText := run; | |
| Caption := run; | |
| break; | |
| end; | |
| end; | |
| end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment