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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Zhuolun Lang
SWEN90006-A1-2018
Commits
2e9729f7
Commit
2e9729f7
authored
6 years ago
by
Zhuolun Lang
Browse files
Options
Downloads
Patches
Plain Diff
Update PartitioningTests.java
parent
a671ccc6
No related branches found
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
+126
-77
126 additions, 77 deletions
test/swen90006/machine/PartitioningTests.java
with
126 additions
and
77 deletions
test/swen90006/machine/PartitioningTests.java
+
126
−
77
View file @
2e9729f7
...
...
@@ -8,83 +8,132 @@ import java.nio.file.Files;
import
java.nio.file.FileSystems
;
import
org.junit.*
;
import
com.sun.xml.internal.ws.util.StringUtils
;
import
static
org
.
junit
.
Assert
.*;
public
class
PartitioningTests
{
//Any method annotated with "@Before" will be executed before each test,
//allowing the tester to set up some shared resources.
@Before
public
void
setUp
()
{
public
class
PartitioningTests
{
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC1
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"add R-1 R0"
);
machine
.
execute
(
instruction
);
}
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC2
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"sub R32 R0"
);
machine
.
execute
(
instruction
);
}
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC3
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"mov R0 -65536"
);
machine
.
execute
(
instruction
);
}
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC4
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"ldr R0 R1 65536"
);
machine
.
execute
(
instruction
);
}
//Any method annotated with "@After" will be executed after each test,
//allowing the tester to release any shared resources used in the setup.
@After
public
void
tearDown
()
{
@Test
public
void
EC5
()
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"ret R0"
);
assertEquals
(
machine
.
execute
(
instruction
),
0
);
}
//Any method annotation with "@Test" is executed as a test.
@Test
public
void
aTest
()
{
//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
(
expected
,
actual
);
@Test
(
expected
=
NoReturnValueException
.
class
)
public
void
EC6
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"jmp -1"
);
machine
.
execute
(
instruction
);
}
@Test
public
void
anotherTest
()
{
List
<
String
>
list
=
new
ArrayList
<
String
>();
list
.
add
(
"a"
);
list
.
add
(
"b"
);
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC7
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"div R-1 R0 R1"
);
instruction
.
add
(
"ret R0"
);
machine
.
execute
(
instruction
);
}
//the assertTrue method is used to check whether something holds.
assertTrue
(
list
.
contains
(
"a"
));
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC8
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"str R32 1 R0"
);
instruction
.
add
(
"ret R0"
);
machine
.
execute
(
instruction
);
}
//Test test opens a file and executes the machine
@Test
public
void
aFileOpenTest
()
{
final
List
<
String
>
lines
=
readInstructions
(
"examples/array.s"
);
Machine
m
=
new
Machine
();
assertEquals
(
m
.
execute
(
lines
),
45
);
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC9
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"jz R0 -65536"
);
instruction
.
add
(
"ret R0"
);
machine
.
execute
(
instruction
);
}
//To test an exception, specify the expected exception after the @Test
@Test
(
expected
=
java
.
io
.
IOException
.
class
)
public
void
anExceptionTest
()
throws
Throwable
{
throw
new
java
.
io
.
IOException
();
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC10
()
throws
Throwable
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"mov R0 65536"
);
instruction
.
add
(
"jz R0 1"
);
instruction
.
add
(
"ret R0"
);
machine
.
execute
(
instruction
);
}
//This test should fail.
//To provide additional feedback when a test fails, an error message
//can be included
@Test
public
void
aFailedTest
()
{
//include a message for better feedback
final
int
expected
=
2
;
final
int
actual
=
1
+
2
;
assertEquals
(
"Some failure message"
,
ex
p
ecte
d
,
actual
);
@Test
public
void
EC11
()
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>
()
;
instruction
.
add
(
"mov R0 -65535"
);
instruction
.
add
(
"mov R31 65535"
);
instruction
.
add
(
"mul R1 R0 R31"
)
;
instruction
.
add
(
"ret R0"
)
;
assertEquals
(
machine
.
exec
u
te
(
instruction
),
-
65535
);
}
//Read in a file containing a program and convert into a list of
//string instructions
private
List
<
String
>
readInstructions
(
String
file
)
{
Charset
charset
=
Charset
.
forName
(
"UTF-8
"
);
List
<
String
>
lines
=
null
;
try
{
lines
=
Files
.
readAllLines
(
FileSystems
.
getDefault
().
getPath
(
file
),
charset
);
@Test
(
expected
=
NoReturnValueException
.
class
)
public
void
EC12
()
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
"mov R0 -65535
"
);
instruction
.
add
(
"mov R31 65535"
)
;
instruction
.
add
(
"jmp 2"
);
machine
.
execute
(
instruction
);
}
catch
(
Exception
e
){
System
.
err
.
println
(
"Invalid input file! (stacktrace follows)"
);
e
.
printStackTrace
(
System
.
err
);
System
.
exit
(
1
);
@Test
(
expected
=
InvalidInstructionException
.
class
)
public
void
EC13
()
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
instruction
.
add
(
""
);
instruction
.
add
(
"abc"
);
machine
.
execute
(
instruction
);
}
return
lines
;
@Test
(
expected
=
NoReturnValueException
.
class
)
public
void
EC14
()
{
Machine
machine
=
new
Machine
();
List
<
String
>
instruction
=
new
ArrayList
<>();
machine
.
execute
(
instruction
);
}
}
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