Skip to content

Commit

Permalink
Первые тесты симулятора воплащены!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirpitchnsk committed May 11, 2023
1 parent 982c0f5 commit c0a3a2d
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 12 deletions.
7 changes: 6 additions & 1 deletion LogicSimulator/Models/Project.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Avalonia.Controls;
using LogicSimulator.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -113,5 +112,11 @@ public void SaveAs(Window mw) {
Save();
parent.AppendProject(this);
}

/*
* Для тестирования
*/

public void SetDir(string path) => FileDir = path;
}
}
6 changes: 6 additions & 0 deletions LogicSimulator/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ protected static Project? CurrentProj {
map.current_scheme = value.GetFirstScheme();
}
}

/*
* Для тестирования
*/

public static Project? TopSecretGetProj() => current_proj;
}
}
11 changes: 9 additions & 2 deletions LogicSimulator/Views/Shapes/LightBulb.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ public partial class LightBulb: GateBase, IGate, INotifyPropertyChanged {
readonly SolidColorBrush ColorA = new(Color.Parse("#00ff00")); // On
readonly SolidColorBrush ColorB = new(Color.Parse("#1c1c1c")); // Off
public void Brain(ref bool[] ins, ref bool[] outs) {
var value = ins[0];
var value = state = ins[0];
Dispatcher.UIThread.InvokeAsync(() => {
border.Background = value ? ColorA : ColorB;
});

}

/*
* Для тестирования
*/

bool state;

public bool GetState() => state;
}
}
9 changes: 9 additions & 0 deletions LogicSimulator/Views/Shapes/Switch.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,14 @@ public override void ExtraImport(string key, object extra) {
my_state = @state;
if (my_state) border.Background = new SolidColorBrush(Color.Parse("#7d1414"));
}

/*
* Для тестирования
*/

public void SetState(bool state) {
my_state = state;
border.Background = new SolidColorBrush(Color.Parse(state ? "#7d1414" : "#d32f2e"));
}
}
}
62 changes: 55 additions & 7 deletions UITestsLogicSimulator/AllTestsInOnePlace.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.VisualTree;
using LogicSimulator.Models;
using LogicSimulator.ViewModels;
using LogicSimulator.Views;
using LogicSimulator.Views.Shapes;
using System.Text;
using Button = Avalonia.Controls.Button;

namespace UITestsLogicSimulator {
Expand Down Expand Up @@ -81,6 +81,12 @@ private string Export() {
private void Ticks(int count) {
while (count-- > 0) map.sim.TopSecretPublicTickMethod();
}
private static void SaveProject() { // Чтобы только посмотреть, что всё соединилось как надо
var proj = ViewModelBase.TopSecretGetProj() ?? throw new Exception("А где проект? :/");
proj.SetDir("../../..");
proj.FileName = "tested";
proj.Save();
}



Expand All @@ -91,24 +97,66 @@ public void GeneralTest() {
SelectGate(0); // AND-gate
Task.Delay(1).GetAwaiter().GetResult();

IGate? gate = Click(canv, 100, 100);
IGate? gate = Click(canv, 200, 200);
Assert.NotNull(gate);
var data = Export();
Assert.Equal("{\"name\": \"Newy\", \"created\": 123, \"modified\": 456, \"items\": [{\"id\": 0, \"pos\": \"$p$100,100\", \"size\": \"$s$90,90\", \"base_size\": 25}], \"joins\": [], \"states\": \"00\"}", data);
Assert.Equal("{\"name\": \"Newy\", \"created\": 123, \"modified\": 456, \"items\": [{\"id\": 0, \"pos\": \"$p$200,200\", \"size\": \"$s$90,90\", \"base_size\": 25}], \"joins\": [], \"states\": \"00\"}", data);

SelectGate(1); // OR-gate
SelectGate(3); // XOR-gate
Task.Delay(1).GetAwaiter().GetResult();

IGate? gate2 = Click(canv, 150, 150);
IGate? gate2 = Click(canv, 300, 300);
Assert.NotNull(gate2);

Move(gate.SecretGetPin(2), gate2.SecretGetPin(0));
Move(gate.SecretGetPin(2), gate2.SecretGetPin(0)); // Соединяем gate и gate2

data = Export();
Assert.Equal("{\"name\": \"Newy\", \"created\": 123, \"modified\": 456, \"items\": [{\"id\": 0, \"pos\": \"$p$100,100\", \"size\": \"$s$90,90\", \"base_size\": 25}, {\"id\": 1, \"pos\": \"$p$150,150\", \"size\": \"$s$90,90\", \"base_size\": 25}], \"joins\": [[0, 2, \"Out\", 1, 0, \"In\"]], \"states\": \"000\"}", data);
Assert.Equal("{\"name\": \"Newy\", \"created\": 123, \"modified\": 456, \"items\": [{\"id\": 0, \"pos\": \"$p$200,200\", \"size\": \"$s$90,90\", \"base_size\": 25}, {\"id\": 3, \"pos\": \"$p$300,300\", \"size\": \"$s$90,90\", \"base_size\": 25}], \"joins\": [[0, 2, \"Out\", 1, 0, \"In\"]], \"states\": \"000\"}", data);

SelectGate(5); // Switch-gate
Task.Delay(1).GetAwaiter().GetResult();

IGate? button = Click(canv, 100, 150);
IGate? button2 = Click(canv, 100, 250);
IGate? button3 = Click(canv, 100, 350);
Assert.NotNull(button);
Assert.NotNull(button2);
Assert.NotNull(button3);

Move(button.SecretGetPin(0), gate.SecretGetPin(0));
Move(button2.SecretGetPin(0), gate.SecretGetPin(1));
Move(button3.SecretGetPin(0), gate2.SecretGetPin(1));

SelectGate(7); // LightBulb-gate
Task.Delay(1).GetAwaiter().GetResult();

IGate? ball = Click(canv, 400, 300);
Assert.NotNull(ball);

Move(gate2.SecretGetPin(2), ball.SecretGetPin(0));

var input = (Switch) button;
var input2 = (Switch) button2;
var input3 = (Switch) button3;
var output = (LightBulb) ball;

StringBuilder sb = new();
for (int i = 0; i < 8; i++) {
input.SetState((i & 4) > 0);
input2.SetState((i & 2) > 0);
input3.SetState((i & 1) > 0);
if (i > 0) sb.Append('|');
for (int tick = 0; tick < 5; tick++) {
Ticks(1);
sb.Append(output.GetState() ? '1' : '0');
}
}
Assert.Equal("00000|00111|11000|00111|11000|00111|11011|11000", sb.ToString());


Log("Export: " + Export());
Log("ОК!");
// SaveProject();
}
}
}
11 changes: 10 additions & 1 deletion UITestsLogicSimulator/TestLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
Tapped: True | 1
Tapped: True | 1
Moved: False | 6
Export: {"name": "Newy", "created": 123, "modified": 456, "items": [{"id": 0, "pos": "$p$100,100", "size": "$s$90,90", "base_size": 25}, {"id": 1, "pos": "$p$150,150", "size": "$s$90,90", "base_size": 25}], "joins": [[0, 2, "Out", 1, 0, "In"]], "states": "000"}
Tapped: True | 1
Tapped: True | 1
Tapped: True | 1
Moved: False | 6
Moved: False | 6
Moved: False | 6
Tapped: True | 1
Moved: False | 6
Sim: 00000|00111|11000|00111|11000|00111|11011|11000
Export: {"name": "Newy", "created": 123, "modified": 456, "items": [{"id": 0, "pos": "$p$200,200", "size": "$s$90,90", "base_size": 25}, {"id": 3, "pos": "$p$300,300", "size": "$s$90,90", "base_size": 25}, {"id": 5, "pos": "$p$100,150", "size": "$s$75,75", "base_size": 25, "state": true}, {"id": 5, "pos": "$p$100,250", "size": "$s$75,75", "base_size": 25, "state": true}, {"id": 5, "pos": "$p$100,350", "size": "$s$75,75", "base_size": 25, "state": true}, {"id": 7, "pos": "$p$400,300", "size": "$s$75,75", "base_size": 25}], "joins": [[0, 2, "Out", 1, 0, "In"], [1, 2, "Out", 5, 0, "In"], [2, 0, "Out", 0, 0, "In"], [3, 0, "Out", 0, 1, "In"], [4, 0, "Out", 1, 1, "In"]], "states": "010111"}
ОК!
28 changes: 28 additions & 0 deletions UITestsLogicSimulator/tested
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<Dict name="Новый проект" created="$1683838478" modified="$1683838479">
<schemes>
<List>
<Dict name="Newy" created="$123" modified="$1683838479" states="010111">
<items>
<List>
<Dict id="$0" pos="$p$200,200" size="$s$90,90" base_size="$25"/>
<Dict id="$3" pos="$p$300,300" size="$s$90,90" base_size="$25"/>
<Dict id="$5" pos="$p$100,150" size="$s$75,75" base_size="$25" state="_BOOL_yeah"/>
<Dict id="$5" pos="$p$100,250" size="$s$75,75" base_size="$25" state="_BOOL_yeah"/>
<Dict id="$5" pos="$p$100,350" size="$s$75,75" base_size="$25" state="_BOOL_yeah"/>
<Dict id="$7" pos="$p$400,300" size="$s$75,75" base_size="$25"/>
</List>
</items>
<joins>
<List>
<List _0='$0' _1='$2' _2='Out' _3='$1' _4='$0' _5='In'/>
<List _0='$1' _1='$2' _2='Out' _3='$5' _4='$0' _5='In'/>
<List _0='$2' _1='$0' _2='Out' _3='$0' _4='$0' _5='In'/>
<List _0='$3' _1='$0' _2='Out' _3='$0' _4='$1' _5='In'/>
<List _0='$4' _1='$0' _2='Out' _3='$1' _4='$1' _5='In'/>
</List>
</joins>
</Dict>
</List>
</schemes>
</Dict>
2 changes: 1 addition & 1 deletion build.num
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1075
1078

0 comments on commit c0a3a2d

Please sign in to comment.