Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #1

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 59 additions & 65 deletions clouds/clouds_test.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,70 @@
package clouds

import "testing"
import (
"testing"

type testpair struct {
input string
expected Cloud
expectedHeightInM int
}

var tests = []testpair{

// Type CloudType
// height int
// HeightNotDefined bool
// Cumulonimbus bool
// ToweringCumulus bool
// CBNotDefined bool

{"FEW005", Cloud{FEW, 5, false, false, false, false}, 150},
{"FEW010CB", Cloud{FEW, 10, false, true, false, false}, 300},
{"SCT018", Cloud{SCT, 18, false, false, false, false}, 550},
{"BKN025///", Cloud{BKN, 25, false, false, false, true}, 760},
{"OVC///", Cloud{OVC, 0, true, false, false, false}, 0},
{"///015", Cloud{NotDefined, 15, false, false, false, false}, 460},
{"//////", Cloud{NotDefined, 0, true, false, false, false}, 0},
{"//////CB", Cloud{NotDefined, 0, true, true, false, false}, 0},
{"BKN020TCU", Cloud{BKN, 20, false, false, true, false}, 610},
{"NSC", Cloud{NSC, 0, false, false, false, false}, 0},
{"RESHRA", Cloud{NotDefined, 0, false, false, false, false}, 0},
}
. "github.com/smartystreets/goconvey/convey"
. "github.com/urkk/metar/conversion"
)

func TestParseCloud(t *testing.T) {
arr := &Clouds{}
for _, pair := range tests {
v, ok := ParseCloud(pair.input)
if ok && v != pair.expected {
t.Error(
"For", pair.input,
"expected", pair.expected,
"got", v,
)
}
if ok && !arr.AppendCloud(pair.input) {
t.Error("For", pair.input, "error append cloud")
} else if !ok && arr.AppendCloud(pair.input) {
t.Error("For", pair.input, "error append cloud")
}
}
}

func TestHeightFt(t *testing.T) {
for _, pair := range tests {
v, ok := ParseCloud(pair.input)
if ok && v.HeightFt() != pair.expected.height*100 {
t.Error(
"For", pair.input,
"expected", pair.expected.height*100,
"got", v.HeightFt(),
)
}
type testpair struct {
input string
expected Cloud
}
}

func TestHeightM(t *testing.T) {
for _, pair := range tests {
v, ok := ParseCloud(pair.input)
if ok && v.HeightM() != pair.expectedHeightInM {
t.Error(
"For", pair.input,
"expected", pair.expectedHeightInM,
"got", v.HeightM(),
)
}
var tests = []testpair{

// Type CloudType
// height int
// HeightNotDefined bool
// Cumulonimbus bool
// ToweringCumulus bool
// CBNotDefined bool

{"FEW005", Cloud{FEW, 5, false, false, false, false}},
{"FEW010CB", Cloud{FEW, 10, false, true, false, false}},
{"SCT018", Cloud{SCT, 18, false, false, false, false}},
{"BKN025///", Cloud{BKN, 25, false, false, false, true}},
{"OVC///", Cloud{OVC, 0, true, false, false, false}},
{"///015", Cloud{NotDefined, 15, false, false, false, false}},
{"//////", Cloud{NotDefined, 0, true, false, false, false}},
{"//////CB", Cloud{NotDefined, 0, true, true, false, false}},
{"BKN020TCU", Cloud{BKN, 20, false, false, true, false}},
{"NSC", Cloud{NSC, 0, false, false, false, false}},
{"RESHRA", Cloud{"", 0, false, false, false, false}},
}
Convey("Cloud layer parsing tests", t, func() {
Convey("cloud must parsed correctly", func() {
for _, pair := range tests {
cloud, _ := ParseCloud(pair.input)
So(cloud, ShouldResemble, pair.expected)

}
})

Convey("height in feet must calculated correctly", func() {
for _, pair := range tests {
cloud, _ := ParseCloud(pair.input)
So(cloud.HeightFt(), ShouldEqual, pair.expected.height*100)
}
})

Convey("height in meters must calculated correctly", func() {
for _, pair := range tests {
cloud, _ := ParseCloud(pair.input)
So(cloud.HeightM(), ShouldEqual, FtToM(pair.expected.height*100))
}
})

Convey("correct cloud must can be appended", func() {
for _, pair := range tests {
_, ok := ParseCloud(pair.input)
So(arr.AppendCloud(pair.input), ShouldEqual, ok)
}
})

})
}
4 changes: 2 additions & 2 deletions conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func MpsToKts(m float64) float64 {
}

// SMileToM - converts statute miles to meters
func SMileToM(sm int) int {
return int(math.Round(float64(sm) * 1609.344))
func SMileToM(sm float64) int {
return int(math.Round(sm * 1609.344))
}

// FtToM - converts feet to meters (rounded to 10 meters)
Expand Down
158 changes: 88 additions & 70 deletions conversion/conversion_test.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,115 @@
package conversion

import "testing"
import (
"math"
"testing"

type testpairint struct {
input int
expected int
}

var testsHPaToMmHg = []testpairint{
{1020, 765}, //765.0650305276
{1015, 761}, //761.3147117505
{1011, 758}, //758.3144567288
}
. "github.com/smartystreets/goconvey/convey"
)

func TestHPaToMmHg(t *testing.T) {
for _, pair := range testsHPaToMmHg {
v := HPaToMmHg(pair.input)
if v != pair.expected {
t.Error(
"For", pair.input,
"expected", pair.expected,
"got", v,
)
Convey("converts hectopascal to mm of mercury", t, func() {
for input := 985; input < 1027; input++ {
So(HPaToMmHg(input), ShouldAlmostEqual, float64(input)*0.75006375541921, 1)
}
}
})
}

var testsMmHgToHPa = []testpairint{
{768, 1024}, //1023.91296
{764, 1019}, //1018.58008
{758, 1011}, //1010.58076
func TestMmHgToHPa(t *testing.T) {
Convey("converts mm of mercury to hectopascal", t, func() {
for input := 740; input < 780; input++ {
So(MmHgToHPa(input), ShouldAlmostEqual, float64(input)*1.333223684, 1)
}
})
}

func TestMmHgToHPa(t *testing.T) {
for _, pair := range testsMmHgToHPa {
v := MmHgToHPa(pair.input)
if v != pair.expected {
t.Error(
"For", pair.input,
"expected", pair.expected,
"got", v,
)
func TestDirectionToCardinalDirection(t *testing.T) {

type testpairDirection struct {
input int
expected string
}

var testsDirection = []testpairDirection{
{360, "N"},
{30, "NE"},
{275, "W"},
{0, "N"},
}

Convey("converts direction in degrees to points of the compass", t, func() {
for _, pair := range testsDirection {
So(DirectionToCardinalDirection(pair.input), ShouldEqual, pair.expected)
}
})
}

func TestCalcRelativeHumidity(t *testing.T) {
type testRelativeHumidity struct {
temp, dewpoint, rh int
}
var testsRelativeHumidity = []testRelativeHumidity{
{20, 7, 43},
{31, 7, 22},
{25, 25, 100},
{25, 19, 69},
{-7, -13, 62},
{0, -9, 51},
}
Convey("calculates the relative humidity of the dew point and temperature", t, func() {
for _, data := range testsRelativeHumidity {
So(CalcRelativeHumidity(data.temp, data.dewpoint), ShouldEqual, data.rh)
}
})
}

type testpairDirection struct {
input int
expected string
func TestKphToMps(t *testing.T) {
Convey("converts kilometres per hour to meters per second", t, func() {
for input := .0; input < 200; input++ {
So(KphToMps(input), ShouldAlmostEqual, input/3.6, .00001)
}
})
}

var testsDirection = []testpairDirection{
{360, "N"},
{30, "NE"},
{275, "W"},
{0, "N"},
func TestKtsToMps(t *testing.T) {
Convey("converts knots to meters per second", t, func() {
for input := .0; input < 50; input++ {
So(KtsToMps(input), ShouldAlmostEqual, input/1.94384, .00001)
}
})
}

func TestDirectionToCardinalDirection(t *testing.T) {
for _, pair := range testsDirection {
v := DirectionToCardinalDirection(pair.input)
if v != pair.expected {
t.Error(
"For", pair.input,
"expected", pair.expected,
"got", v,
)
func TestMpsToKts(t *testing.T) {
Convey("converts meters per second to knots", t, func() {
for input := .0; input < 50; input++ {
So(MpsToKts(input), ShouldAlmostEqual, input*1.94384, .00001)
}
}
})
}

type testRelativeHumidity struct {
temp, dewpoint, rh int
func TestSMileToM(t *testing.T) {
Convey("converts statute miles to meters", t, func() {
for input := .0; input < 6.25; {
So(SMileToM(input), ShouldAlmostEqual, input*1609.344, 1)
input += .25
}
})
}

var testsRelativeHumidity = []testRelativeHumidity{
{20, 7, 43},
{31, 7, 22},
{25, 25, 100},
{25, 19, 69},
{-7, -13, 62},
{0, -9, 51},
func TestFtToM(t *testing.T) {
Convey("converts feet to meters (rounded to 10 meters)", t, func() {
for input := 300; input < 3000; {
So(FtToM(input), ShouldAlmostEqual, int(math.Round(float64(input)*0.3048/10)*10), 10)
input += 300
}
})
}

func TestCalcRelativeHumidity(t *testing.T) {
for _, data := range testsRelativeHumidity {
rh := CalcRelativeHumidity(data.temp, data.dewpoint)
if rh != data.rh {
t.Error(
"For t", data.temp, " dew point ", data.dewpoint,
"expected", data.rh,
"got", rh,
)
func TestInHgTohPa(t *testing.T) {
Convey("converts inch of mercury to hectopascal", t, func() {
for input := 29.0; input < 30.3; {
So(InHgTohPa(input), ShouldAlmostEqual, input*33.86389, 1)
input += .1
}
}
})
}
Loading