Skip to content

Commit 22173e9

Browse files
committed
L6: Z2, Z3
1 parent 444c36a commit 22173e9

21 files changed

+798
-0
lines changed

OopL6/OopL6.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj
55
EndProject
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zad1", "Zad1\Zad1.csproj", "{2C410DF6-FF0C-45BB-9EE7-AB049F201F67}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zad2", "Zad2\Zad2.csproj", "{9898B644-E7A5-4061-811C-8EBC60CDF510}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zad3", "Zad3\Zad3.csproj", "{77BA0795-94A2-40AF-B7DF-58BB031B6B82}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -19,6 +23,14 @@ Global
1923
{2C410DF6-FF0C-45BB-9EE7-AB049F201F67}.Debug|Any CPU.Build.0 = Debug|Any CPU
2024
{2C410DF6-FF0C-45BB-9EE7-AB049F201F67}.Release|Any CPU.ActiveCfg = Release|Any CPU
2125
{2C410DF6-FF0C-45BB-9EE7-AB049F201F67}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{9898B644-E7A5-4061-811C-8EBC60CDF510}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{9898B644-E7A5-4061-811C-8EBC60CDF510}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{9898B644-E7A5-4061-811C-8EBC60CDF510}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{9898B644-E7A5-4061-811C-8EBC60CDF510}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{77BA0795-94A2-40AF-B7DF-58BB031B6B82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{77BA0795-94A2-40AF-B7DF-58BB031B6B82}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{77BA0795-94A2-40AF-B7DF-58BB031B6B82}.Release|Any CPU.ActiveCfg = Release|Any CPU
33+
{77BA0795-94A2-40AF-B7DF-58BB031B6B82}.Release|Any CPU.Build.0 = Release|Any CPU
2234
EndGlobalSection
2335
GlobalSection(SolutionProperties) = preSolution
2436
HideSolutionNode = FALSE

OopL6/Tests/Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
</Otherwise>
5151
</Choose>
5252
<ItemGroup>
53+
<Compile Include="Zad3Test.cs" />
54+
<Compile Include="Zad2Test.cs" />
5355
<Compile Include="Zad1Test.cs" />
5456
<Compile Include="Properties\AssemblyInfo.cs" />
5557
</ItemGroup>
@@ -58,6 +60,14 @@
5860
<Project>{2C410DF6-FF0C-45BB-9EE7-AB049F201F67}</Project>
5961
<Name>Zad1</Name>
6062
</ProjectReference>
63+
<ProjectReference Include="..\Zad2\Zad2.csproj">
64+
<Project>{9898b644-e7a5-4061-811c-8ebc60cdf510}</Project>
65+
<Name>Zad2</Name>
66+
</ProjectReference>
67+
<ProjectReference Include="..\Zad3\Zad3.csproj">
68+
<Project>{77BA0795-94A2-40AF-B7DF-58BB031B6B82}</Project>
69+
<Name>Zad3</Name>
70+
</ProjectReference>
6171
</ItemGroup>
6272
<Choose>
6373
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">

OopL6/Tests/Zad2Test.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Zad2;
4+
5+
namespace Tests
6+
{
7+
[TestClass]
8+
public class Zad2Test
9+
{
10+
const string TRUE_TOKEN = "true";
11+
const string FALSE_TOKEN = "false";
12+
13+
[TestMethod]
14+
public void TestUnaryVar()
15+
{
16+
const string VAR_NAME = "x";
17+
18+
var ctx = new Context();
19+
ctx.SetValue(VAR_NAME, false);
20+
21+
var unary = new UnaryExpression(VAR_NAME);
22+
Assert.AreEqual(true, unary.Interpret(ctx));
23+
}
24+
25+
[TestMethod]
26+
public void TestConst()
27+
{
28+
var ctx = new Context();
29+
30+
var constExp = new ConstExpression(TRUE_TOKEN);
31+
Assert.AreEqual(true, constExp.Interpret(ctx));
32+
33+
constExp = new ConstExpression(FALSE_TOKEN);
34+
Assert.AreEqual(false, constExp.Interpret(ctx));
35+
}
36+
37+
[TestMethod]
38+
public void TestBinaryAndVar()
39+
{
40+
const string VAR_X = "x";
41+
const string VAR_Y = "y";
42+
const string VAR_Z = "z";
43+
44+
var ctx = new Context();
45+
ctx.SetValue(VAR_X, false);
46+
ctx.SetValue(VAR_Y, true);
47+
ctx.SetValue(VAR_Z, true);
48+
49+
var binary = new BinaryExpression(VAR_X, BinaryOp.And, VAR_Y);
50+
Assert.AreEqual(false, binary.Interpret(ctx));
51+
52+
binary = new BinaryExpression(VAR_Y, BinaryOp.And, VAR_Z);
53+
Assert.AreEqual(true, binary.Interpret(ctx));
54+
}
55+
56+
[TestMethod]
57+
public void TestBinaryOrVar()
58+
{
59+
const string VAR_X = "x";
60+
const string VAR_Y = "y";
61+
const string VAR_Z = "z";
62+
63+
var ctx = new Context();
64+
ctx.SetValue(VAR_X, false);
65+
ctx.SetValue(VAR_Y, true);
66+
ctx.SetValue(VAR_Z, false);
67+
68+
var binary = new BinaryExpression(VAR_X, BinaryOp.Or, VAR_Y);
69+
Assert.AreEqual(true, binary.Interpret(ctx));
70+
71+
binary = new BinaryExpression(VAR_X, BinaryOp.Or, VAR_Z);
72+
Assert.AreEqual(false, binary.Interpret(ctx));
73+
}
74+
75+
[TestMethod]
76+
public void TestComplexExpression()
77+
{
78+
const string VAR_X = "x";
79+
const string VAR_Y = "y";
80+
const string VAR_Z = "z";
81+
82+
var ctx = new Context();
83+
ctx.SetValue(VAR_X, true);
84+
ctx.SetValue(VAR_Y, true);
85+
ctx.SetValue(VAR_Z, false);
86+
87+
var constExp = new ConstExpression(TRUE_TOKEN);
88+
var unaryExp = new UnaryExpression(constExp);
89+
Assert.AreEqual(false, unaryExp.Interpret(ctx));
90+
91+
var binaryExp =
92+
new BinaryExpression(
93+
new BinaryExpression(VAR_X,
94+
BinaryOp.And,
95+
unaryExp),
96+
BinaryOp.Or,
97+
new BinaryExpression(new UnaryExpression(VAR_Y),
98+
BinaryOp.And,
99+
VAR_Z));
100+
101+
Assert.AreEqual(false, binaryExp.Interpret(ctx));
102+
}
103+
104+
[TestMethod]
105+
[ExpectedException(typeof(InvalidOperationException))]
106+
public void TestVarNotFoundExc()
107+
{
108+
const string VAR_NAME = "x";
109+
110+
var ctx = new Context();
111+
var unary = new UnaryExpression(VAR_NAME);
112+
unary.Interpret(ctx);
113+
}
114+
}
115+
}

OopL6/Tests/Zad3Test.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Zad3;
4+
5+
namespace Tests
6+
{
7+
[TestClass]
8+
public class Zad3Test
9+
{
10+
[TestMethod]
11+
public void TestHeightVisitor()
12+
{
13+
var tree = new Tree();
14+
tree.Add(10);
15+
tree.Add(1);
16+
tree.Add(20);
17+
tree.Add(23);
18+
tree.Add(21);
19+
20+
var visitor = new HeightVisitor();
21+
tree.Accept(visitor);
22+
23+
Assert.AreEqual(3, visitor.TreeHeight);
24+
}
25+
}
26+
}

OopL6/Zad2/AbstractExpression.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Zad2
2+
{
3+
public abstract class AbstractExpression
4+
{
5+
#region Public methods
6+
7+
public abstract bool Interpret(Context context);
8+
9+
#endregion
10+
}
11+
}

OopL6/Zad2/BinaryExpression.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
3+
namespace Zad2
4+
{
5+
public class BinaryExpression : AbstractExpression
6+
{
7+
#region Enums
8+
9+
private enum Scenario
10+
{
11+
VarVar,
12+
ExpVar,
13+
VarExp,
14+
ExpExp
15+
}
16+
17+
#endregion
18+
#region Private fields
19+
20+
private readonly BinaryOp? _op;
21+
private readonly Scenario? _scenario;
22+
23+
private readonly AbstractExpression _xExp = new NullExpression();
24+
private readonly string _xVarName;
25+
private readonly AbstractExpression _yExp = new NullExpression();
26+
private readonly string _yVarName;
27+
28+
#endregion
29+
#region Ctors
30+
31+
public BinaryExpression(string xVarName, BinaryOp op, string yVarName)
32+
{
33+
if (string.IsNullOrEmpty(xVarName) || string.IsNullOrEmpty(yVarName))
34+
{
35+
throw new ArgumentNullException();
36+
}
37+
38+
_xVarName = xVarName;
39+
_yVarName = yVarName;
40+
41+
_op = op;
42+
_scenario = Scenario.VarVar;
43+
}
44+
45+
public BinaryExpression(string xVarName, BinaryOp op, AbstractExpression exp)
46+
{
47+
if (string.IsNullOrEmpty(xVarName) || exp == null)
48+
{
49+
throw new ArgumentNullException();
50+
}
51+
52+
_xVarName = xVarName;
53+
_yExp = exp;
54+
55+
_op = op;
56+
_scenario = Scenario.VarExp;
57+
}
58+
59+
public BinaryExpression(AbstractExpression exp, BinaryOp op, string yVarName)
60+
{
61+
_xExp = exp;
62+
_yVarName = yVarName;
63+
64+
_op = op;
65+
_scenario = Scenario.ExpVar;
66+
}
67+
68+
public BinaryExpression(AbstractExpression xExp, BinaryOp op, AbstractExpression yExp)
69+
{
70+
_xExp = xExp;
71+
_yExp = xExp;
72+
73+
_op = op;
74+
_scenario = Scenario.ExpExp;
75+
}
76+
77+
#endregion
78+
#region Overrides
79+
80+
public override bool Interpret(Context context)
81+
{
82+
bool xVar;
83+
bool yVar;
84+
85+
switch (_scenario)
86+
{
87+
case Scenario.VarVar:
88+
xVar = context.GetValue(_xVarName);
89+
yVar = context.GetValue(_yVarName);
90+
break;
91+
case Scenario.VarExp:
92+
xVar = context.GetValue(_xVarName);
93+
yVar = _yExp.Interpret(context);
94+
break;
95+
case Scenario.ExpVar:
96+
xVar = _xExp.Interpret(context);
97+
yVar = context.GetValue(_yVarName);
98+
break;
99+
case Scenario.ExpExp:
100+
xVar = _xExp.Interpret(context);
101+
yVar = _yExp.Interpret(context);
102+
break;
103+
default:
104+
throw new InvalidOperationException();
105+
}
106+
107+
switch (_op)
108+
{
109+
case BinaryOp.And:
110+
return xVar && yVar;
111+
case BinaryOp.Or:
112+
return xVar || yVar;
113+
default:
114+
throw new InvalidOperationException();
115+
}
116+
}
117+
118+
#endregion
119+
}
120+
}

OopL6/Zad2/BinaryOp.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Zad2
2+
{
3+
public enum BinaryOp
4+
{
5+
And,
6+
Or
7+
}
8+
}

OopL6/Zad2/ConstExpression.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace Zad2
4+
{
5+
public class ConstExpression : AbstractExpression
6+
{
7+
#region Private fields
8+
9+
private bool? _value;
10+
11+
#endregion
12+
#region Ctors
13+
14+
public ConstExpression(string token)
15+
{
16+
bool result;
17+
if (bool.TryParse(token, out result))
18+
{
19+
_value = result;
20+
return;
21+
}
22+
23+
throw new ArgumentException();
24+
}
25+
26+
#endregion
27+
#region Overrides
28+
29+
public override bool Interpret(Context context)
30+
{
31+
if (_value.HasValue)
32+
{
33+
return (bool) _value;
34+
}
35+
36+
throw new InvalidOperationException();
37+
}
38+
39+
#endregion
40+
}
41+
}

0 commit comments

Comments
 (0)