Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
SWEN90006-A1-2018
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jingjiahui Lu
SWEN90006-A1-2018
Commits
7d089253
Commit
7d089253
authored
Sep 16, 2018
by
Jingjiahui Lu
Browse files
Options
Downloads
Patches
Plain Diff
Update PartitioningTests.java
parent
a28e304f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/swen90006/machine/PartitioningTests.java
+195
-44
195 additions, 44 deletions
test/swen90006/machine/PartitioningTests.java
with
195 additions
and
44 deletions
test/swen90006/machine/PartitioningTests.java
+
195
−
44
View file @
7d089253
...
...
@@ -8,6 +8,8 @@ import java.nio.file.Files;
import
java.nio.file.FileSystems
;
import
org.junit.*
;
import
org.junit.rules.ExpectedException
;
import
static
org
.
junit
.
Assert
.*;
public
class
PartitioningTests
...
...
@@ -25,66 +27,215 @@ public class PartitioningTests
}
//Any method annotation with "@Test" is executed as a test.
@Test
public
void
aTest
()
@Test
public
void
EC1
()
{
String
a
=
"RET R2;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
0
);
}
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC2
()
throws
Throwable
{
//the assertEquals method used to check whether two values are
//equal, using the equals method
final
int
expected
=
2
;
final
int
actual
=
1
+
1
;
assertEquals
(
ex
p
ecte
d
,
actual
);
String
a
=
"RET R33;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
)
;
Machine
machine
=
new
Machine
()
;
assertEquals
(
"Invalid"
,
machine
.
exec
u
te
(
list
),
InvalidInstructionException
.
class
);
}
@Test
public
void
anotherTest
()
@Test
(
expected
=
NoReturnValueException
.
class
)
public
void
EC3
()
{
List
<
String
>
list
=
new
ArrayList
<
String
>()
;
list
.
add
(
"a"
);
list
.
add
(
"b"
);
String
a
=
"MOV R0 1;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>(
);
list
.
add
(
a
);
//the assertTrue method is used to check whether something holds.
assert
True
(
list
.
contains
(
"a"
)
);
Machine
m
=
new
Machine
();
assert
Equals
(
"Invalid"
,
m
.
execute
(
list
),
NoReturnValueException
.
class
);
}
//Test test opens a file and executes the machine
@Test
public
void
aFileOpenTest
()
@Test
public
void
EC4
()
{
final
List
<
String
>
lines
=
readInstructions
(
"examples/array.s"
);
String
a
=
"MOV R2 1;"
;
String
b
=
"MOV R1 2;"
;
String
c
=
"ADD R3 R1 R2;"
;
String
d
=
"RET R3;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
li
ne
s
),
45
);
assertEquals
(
m
.
execute
(
lis
t
),
3
);
}
//To test an exception, specify the expected exception after the @Test
@Test
(
expected
=
java
.
io
.
IOException
.
class
)
public
void
anExceptionTest
()
throws
Throwable
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC5
()
{
String
a
=
"MOV R0 1;"
;
String
b
=
"MOV R1 2;"
;
String
c
=
"ADD R35 R1 R2;"
;
String
d
=
"RET R35;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
Machine
m
=
new
Machine
();
assertEquals
(
"Invalid"
,
m
.
execute
(
list
),
InvalidInstructionException
.
class
);
}
@Test
public
void
EC6
()
{
String
a
=
"MOV R1 6;"
;
String
b
=
"MOV R2 2;"
;
String
c
=
"MOV R3 6;"
;
String
d
=
"DIV R3 R1 R2;"
;
String
e
=
"RET R3;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
list
.
add
(
e
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
3
);
}
@Test
public
void
EC7
()
{
String
a
=
"MOV R1 6;"
;
String
b
=
"MOV R2 0;"
;
String
d
=
"DIV R3 R1 R2;"
;
String
e
=
"RET R3;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
d
);
list
.
add
(
e
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
0
);
}
@Test
public
void
EC8
()
{
String
a
=
"MOV R3 65535;"
;
String
b
=
"RET R3;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
65535
);
}
/*
* [EC10]: {list|length>1/\INSTUCTION in [MOV,JZ]/\regsi in [R0,R1,...,R31]/\vali not in[-65536,65535]} invalid
MOV R3 65536; RET R3;
*/
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC9
()
{
String
a
=
"MOV R3 65536"
;
String
b
=
"RET R3;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
Machine
m
=
new
Machine
();
assertEquals
(
"Invalid"
,
m
.
execute
(
list
),
InvalidInstructionException
.
class
);
}
@Test
(
expected
=
NoReturnValueException
.
class
)
public
void
EC10
()
{
throw
new
java
.
io
.
IOException
();
String
b
=
"JMP 65535;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
b
);
Machine
m
=
new
Machine
();
assertEquals
(
"Invalid"
,
m
.
execute
(
list
),
NoReturnValueException
.
class
);
}
//This test should fail.
//To provide additional feedback when a test fails, an error message
//can be included
@Test
public
void
aFailedTest
()
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC11
()
{
//include a message for better feedback
final
int
expected
=
2
;
final
int
actual
=
1
+
2
;
assertEquals
(
"Some failure message"
,
expected
,
actual
);
String
b
=
"JMP -65536;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
b
);
Machine
m
=
new
Machine
();
assertEquals
(
"Invalid"
,
m
.
execute
(
list
),
InvalidInstructionException
.
class
);
}
//Read in a file containing a program and convert into a list of
//string instructions
private
List
<
String
>
readInstructions
(
String
file
)
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC12
()
{
String
a
=
"MOV R1 0;"
;
String
b
=
"MOV R2 7;"
;
String
c
=
"LDR R2 R1 65536;"
;
String
d
=
"RET R2;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
Machine
m
=
new
Machine
();
assertEquals
(
"Invalid"
,
m
.
execute
(
list
),
InvalidInstructionException
.
class
);
}
@Test
public
void
EC13
()
{
Charset
charset
=
Charset
.
forName
(
"UTF-8"
);
List
<
String
>
lines
=
null
;
try
{
lines
=
Files
.
readAllLines
(
FileSystems
.
getDefault
().
getPath
(
file
),
charset
);
String
a
=
"MOV R1 2;"
;
String
b
=
"MOV R2 7;"
;
String
c
=
"LDR R2 R1 65534;"
;
String
d
=
"RET R2;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
7
);
}
catch
(
Exception
e
){
System
.
err
.
println
(
"Invalid input file! (stacktrace follows)"
);
e
.
printStackTrace
(
System
.
err
);
System
.
exit
(
1
);
@Test
public
void
EC14
()
{
String
a
=
"MOV R1 55;"
;
String
b
=
"MOV R2 7;"
;
String
c
=
"LDR R2 R1 -44;"
;
String
d
=
"RET R2;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
0
);
}
return
lines
;
@Test
public
void
EC15
()
{
String
a
=
"MOV R1 -1;"
;
String
b
=
"MOV R2 7;"
;
String
c
=
"LDR R2 R1 0;"
;
String
d
=
"RET R2;"
;
final
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
a
);
list
.
add
(
b
);
list
.
add
(
c
);
list
.
add
(
d
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
list
),
7
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment