Commit b8734a6c authored by Marcus Greiff's avatar Marcus Greiff

Refactored tests to allow running on arbitrary operating systems with the...

Refactored tests to allow running on arbitrary operating systems with the RUN_TESTS flag set to true
parent d98af659
......@@ -46,3 +46,21 @@ const gpio_channels =[
"gpio69"
"gpio7"
]
# These pins are exported with the Device Tree Overlay cape-universaln (default)
const validPins = Dict(
"P9.22" => ("PWM0A", "pwmchip0", "0"),
"P9.21" => ("PWM0B", "pwmchip0", "1"),
"P9.14" => ("PWM1A", "pwmchip2", "0"),
"P9.16" => ("PWM1B", "pwmchip2", "1"),
"P8.19" => ("PWM2A", "pwmchip4", "0"),
"P8.13" => ("PWM2B", "pwmchip4", "1"),
)
# These pins are exported with the Device Tree Overlay cape-universala
# "P8.36" => ("PWM1A", "pwmchip1", "0"),
# "P9.29" => ("PWM0B", "pwmchip0", "1"),
# "P8.46" => ("PWM2B", "pwmchip2", "1")
# "P8.45" => ("PWM2A", "pwmchip2", "0"),
# "P8.34" => ("PWM1B", "pwmchip1", "1"),
# "P9.31" => ("PWM0A", "pwmchip0", "0"),
......@@ -5,11 +5,12 @@ AM3359 chip, see p.182 in
www.ti.com/product/AM3359/technicaldocuments
"""
type PWM <: IO_Object
i::Int32
pin::String
chip::String
filestreams::Array{IOStream,1} #1 = enable, 2 = period, 3 = duty_cycle, 4 = polarity
filestreams::Array{IOStream,1}
function PWM(i::Int32)
pins = collect(keys(validPins))
(i < 1 || i > length(pins)) && error("Invalid PWM index: $i")
......@@ -35,24 +36,6 @@ type PWM <: IO_Object
end
end
# These pins are exported with the Device Tree Overlay cape-universaln (default)
validPins = Dict(
"P9.22" => ("PWM0A", "pwmchip0", "0"),
"P9.21" => ("PWM0B", "pwmchip0", "1"),
"P9.14" => ("PWM1A", "pwmchip2", "0"),
"P9.16" => ("PWM1B", "pwmchip2", "1"),
"P8.19" => ("PWM2A", "pwmchip4", "0"),
"P8.13" => ("PWM2B", "pwmchip4", "1"),
)
# These pins are exported with the Device Tree Overlay cape-universala
# "P8.36" => ("PWM1A", "pwmchip1", "0"),
# "P9.29" => ("PWM0B", "pwmchip0", "1"),
# "P8.46" => ("PWM2B", "pwmchip2", "1")
# "P8.45" => ("PWM2A", "pwmchip2", "0"),
# "P8.34" => ("PWM1B", "pwmchip1", "1"),
# "P9.31" => ("PWM0A", "pwmchip0", "0"),
"""
write!(pwm::PWM, args::Tuple{Int32,String}, debug::Bool=false)
Writes an entry to an operation on the PWM, of the form args = (operation, entry).
......@@ -98,3 +81,16 @@ function teardown!(pwm::PWM, debug::Bool=false)
write(filename, export_number)
return
end
"""
to_string(pwm::PWM,, debug::Bool=false)
Generates a string representation of the GPIO device.
"""
function to_string(pwm::PWM, debug::Bool=false)
debug && return
message = "\nID: $(pwm.i)\n\nAvailable filestreams:\n"
for ii = 1:length(pwm.filestreams)
message = string(message, " index=$(ii) - name=$(pwm.filestreams[ii].name) - write/read=$(iswritable(pwm.filestreams[ii]))/$(isreadable(pwm.filestreams[ii]))\n")
end
return message
end
......@@ -46,21 +46,27 @@ end
Closes all open filestreams for the SysLED 'led'.
"""
function teardown(led::SysLED, debug::Bool=false)
debug && return
close(led.filestream)
debug && return
close(led.filestream)
if isdefined(:RUNNING_TESTS)
# Remove the dummy file system for testing
try
#println("$(led.basedir)/beaglebone:green:usr$(led.i-1)")
rm("$(led.basedir)/beaglebone:green:usr$(led.i-1)"; recursive=true)
catch
error("Could not remove the requested LED testfiles for channel beaglebone:green:usr$(led.i-1).")
end
if isdefined(:RUNNING_TESTS)
# Remove the dummy file system for testing
try
#println("$(led.basedir)/beaglebone:green:usr$(led.i-1)")
rm("$(led.basedir)/beaglebone:green:usr$(led.i-1)"; recursive=true)
catch
error("Could not remove the requested LED testfiles for channel beaglebone:green:usr$(led.i-1).")
end
end
end
function export_led(i::Int32)
"""
export_led(i::Int32, debug::Bool=false)
Exports a dummy filesystem for testing the LED implementation
"""
function export_led(i::Int32, debug::Bool=false)
debug && return
if isdefined(:RUNNING_TESTS)
# Export a dummy file system for testing
basedir = "$(pwd())/testfilesystem/leds"
......@@ -80,3 +86,14 @@ function export_led(i::Int32)
end
return basedir
end
"""
to_string(led::SysLED, debug::Bool=false)
Generates a string representation of the GPIO device.
"""
function to_string(led::SysLED, debug::Bool=false)
debug && return
message = "\nID: $(led.i)\n\nAvailable filestream:\n"
message = string(message, " name=$(led.filestream.name) - write/read=$(iswritable(led.filestream))/$(isreadable(led.filestream))\n")
return message
end
......@@ -7,40 +7,40 @@ using Base.Test
@testset "Inialization/Termination" begin
# Initialize three devices
initdev("gpio", 1)
initdev("gpio", Int32(1))
@test sum(listdev()) == 1
initdev("gpio", 3)
initdev("gpio", Int32(3))
@test sum(listdev()) == 2
initdev("gpio", 5)
initdev("gpio", Int32(5))
@test sum(listdev()) == 3
#printdev("gpio", 3)
# Attempt to initialize a device which has already been initialized
@test_throws ErrorException initdev("gpio", 1)
@test_throws ErrorException initdev("gpio", 3)
@test_throws ErrorException initdev("gpio", 5)
@test_throws ErrorException initdev("gpio", Int32(1))
@test_throws ErrorException initdev("gpio", Int32(3))
@test_throws ErrorException initdev("gpio", Int32(5))
# Attempt to initialize a device with a very high index (no matching channel)
@test_throws ErrorException initdev("gpio", 1000)
@test_throws ErrorException initdev("gpio", Int32(1000))
# Attempt to remove devices which have not been initialized
@test_throws ErrorException closedev("gpio", 2)
@test_throws ErrorException closedev("gpio", 4)
@test_throws ErrorException closedev("gpio", 6)
@test_throws ErrorException closedev("gpio", Int32(2))
@test_throws ErrorException closedev("gpio", Int32(4))
@test_throws ErrorException closedev("gpio", Int32(6))
#printdev("gpio", 3)
# Remove devices from TOC
closedev("gpio", 1)
closedev("gpio", Int32(1))
@test sum(listdev()) == 2
closedev("gpio", 3)
closedev("gpio", Int32(3))
@test sum(listdev()) == 1
closedev("gpio", 5)
closedev("gpio", Int32(5))
@test sum(listdev()) == 0
end
......@@ -48,45 +48,45 @@ using Base.Test
@testset "Read/Write" begin
# Fixture
device = initdev("gpio", 1)
device = initdev("gpio", Int32(1))
# Test that an exception is thrown when an invalid operation is given
# supported operations are 1,2,3
@test_throws ErrorException write!(device, (0, "something"))
@test_throws ErrorException write!(device, (4, "something"))
@test_throws ErrorException write!(device, (Int32(0), "something"))
@test_throws ErrorException write!(device, (Int32(4), "something"))
# Test that exceptions are thrown for each individual operation
@test_throws ErrorException write!(device, (1, "bad_entry"))
@test_throws ErrorException write!(device, (2, "bad_entry"))
@test_throws ErrorException write!(device, (3, "bad_entry"))
@test_throws ErrorException write!(device, (Int32(1), "bad_entry"))
@test_throws ErrorException write!(device, (Int32(2), "bad_entry"))
@test_throws ErrorException write!(device, (Int32(3), "bad_entry"))
# Test operation 1
write!(device, (1, "1"))
@test read(device, 1) == "1"
write!(device, (1, "0"))
@test read(device, 1) == "0"
write!(device, (1, "1"))
@test read(device, 1) == "1"
write!(device, (1, "0"))
@test read(device, 1) == "0"
write!(device, (2, "in"))
@test read(device, 2) == "in"
write!(device, (2, "out"))
@test read(device, 2) == "out"
write!(device, (2, "in"))
@test read(device, 2) == "in"
write!(device, (2, "out"))
@test read(device, 2) == "out"
write!(device, (Int32(1), "1"))
@test read(device, Int32(1)) == "1"
write!(device, (Int32(1), "0"))
@test read(device, Int32(1)) == "0"
write!(device, (Int32(1), "1"))
@test read(device, Int32(1)) == "1"
write!(device, (Int32(1), "0"))
@test read(device, Int32(1)) == "0"
write!(device, (Int32(2), "in"))
@test read(device, Int32(2)) == "in"
write!(device, (Int32(2), "out"))
@test read(device, Int32(2)) == "out"
write!(device, (Int32(2), "in"))
@test read(device, Int32(2)) == "in"
write!(device, (Int32(2), "out"))
@test read(device, Int32(2)) == "out"
# Test operation 3
@test_throws ErrorException write!(device, (3, "none"))
@test_throws ErrorException write!(device, (3, "rising"))
@test_throws ErrorException write!(device, (3, "falling"))
@test_throws ErrorException write!(device, (3, "both"))
@test_throws ErrorException write!(device, (Int32(3), "none"))
@test_throws ErrorException write!(device, (Int32(3), "rising"))
@test_throws ErrorException write!(device, (Int32(3), "falling"))
@test_throws ErrorException write!(device, (Int32(3), "both"))
# Close Gpio
closedev("gpio", 1)
closedev("gpio", Int32(1))
end
@testset "All channels" begin
......@@ -96,9 +96,9 @@ using Base.Test
# Configure the GPIO for output usage
devices = []
for ii = 1:length(gpio_channels)
device = initdev("gpio", ii)
device = initdev("gpio", Int32(ii))
# Operation 2 -> in/out, set out
write!(device, (2, "out"))
write!(device, (Int32(2), "out"))
# Append to list
append!(devices, [device])
end
......@@ -107,15 +107,15 @@ using Base.Test
for i = 1:10
for ii = 1:length(gpio_channels)
state = "$(i%2)"
write!(devices[ii], (1, state))
@test read(devices[ii], 1) == state
write!(devices[ii], (Int32(1), state))
@test read(devices[ii], Int32(1)) == state
end
sleep(0.10)
end
# Closes all devices
for ii = 1:length(gpio_channels)
closedev("gpio", ii)
closedev("gpio", Int32(ii))
end
end
end
......@@ -7,49 +7,48 @@ using Base.Test
@testset "Inialization/Termination" begin
# Initialize three devices
initdev("sysled", 1)
initdev("sysled", Int32(1))
@test sum(listdev()) == 1
initdev("sysled", 3)
initdev("sysled", Int32(3))
@test sum(listdev()) == 2
# Attempt to initialize a device which has already been initialized
@test_throws ErrorException initdev("sysled", 1)
@test_throws ErrorException initdev("sysled", 3)
@test_throws ErrorException initdev("sysled", Int32(1))
@test_throws ErrorException initdev("sysled", Int32(3))
# Attempt to initialize a device with a very high index (no matching channel)
@test_throws ErrorException initdev("sysled", 1000)
@test_throws ErrorException initdev("sysled", Int32(1000))
# Attempt to remove devices which have not been initialized
@test_throws ErrorException closedev("sysled", 2)
@test_throws ErrorException closedev("sysled", 4)
@test_throws ErrorException closedev("sysled", Int32(2))
@test_throws ErrorException closedev("sysled", Int32(4))
# Remove devices from TOC
closedev("sysled", 1)
closedev("sysled", Int32(1))
@test sum(listdev()) == 1
closedev("sysled", 3)
closedev("sysled", Int32(3))
@test sum(listdev()) == 0
end
@testset "Error Handling" begin
device = initdev("sysled", 1)
device = initdev("sysled", Int32(1))
# Test that an exception is thrown when a faulty ID is given
@test_throws ErrorException write!(device, "bad_entry")
# Close device
closedev("sysled", 1)
closedev("sysled", Int32(1))
end
@testset "IO Communication" begin
# Instanciate all possible leds and perform 10 read/write commands
println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
device1 = initdev("sysled", 1)
device2 = initdev("sysled", 2)
device3 = initdev("sysled", 3)
device4 = initdev("sysled", 4)
device1 = initdev("sysled", Int32(1))
device2 = initdev("sysled", Int32(2))
device3 = initdev("sysled", Int32(3))
device4 = initdev("sysled", Int32(4))
for i = 1:10
stateA = "$(i%2)"
......@@ -65,10 +64,9 @@ using Base.Test
sleep(0.1)
end
println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
closedev("sysled", 1)
closedev("sysled", 2)
closedev("sysled", 3)
closedev("sysled", 4)
closedev("sysled", Int32(1))
closedev("sysled", Int32(2))
closedev("sysled", Int32(3))
closedev("sysled", Int32(4))
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment