package loc import ( "strings" "testing" ) func TestParseSizePrecision(t *testing.T) { n, ok := parseSizePrecision("", 0x98) if n != 0x98 || !ok { t.Error("") } n, ok = parseSizePrecision("-1", 0x98) if ok { t.Error("") } n, ok = parseSizePrecision("ddd", 0x98) if ok { t.Error("") } n, ok = parseSizePrecision("100000000", 0x98) if ok { t.Error("") } n, ok = parseSizePrecision("0", 0x98) if !ok || n != 0x00 { t.Error("0") } n, ok = parseSizePrecision("0.1", 0x98) if !ok || n != 0x11 { t.Error("0.1") } n, ok = parseSizePrecision("900", 0x98) if !ok || n != 0x94 { t.Error("900") } n, ok = parseSizePrecision("900.1", 0x98) if !ok || n != 0x94 { t.Error("900") } n, ok = parseSizePrecision("12345.8", 0x98) if !ok || n != 0x16 { t.Error("12345.8") } n, ok = parseSizePrecision("90000000", 0x98) if !ok || n != 0x99 { t.Error("9000000") } } func TestParseLatLong(t *testing.T) { _, ok := parseLatLong("", "", "", 90) if ok { t.Error("") } _, ok = parseLatLong("a", "b", "c", 90) if ok { t.Error("") } _, ok = parseLatLong("50", "1.2", "0", 90) if ok { t.Error("") } l, ok := parseLatLong("90", "", "", 90) if !ok || l != 90*60*60*1000 { t.Error("90") } l, ok = parseLatLong("90", "", "", 89) if ok { t.Error("90 > 89") } l, ok = parseLatLong("90", "1", "0", 90) if ok { t.Error("90 > 90 1 0") } l, ok = parseLatLong("90", "0", "0", 90) if !ok || l != 90*60*60*1000 { t.Error("90 0 0") } l, ok = parseLatLong("0", "0", "0", 90) if !ok || l != 0 { t.Error("0 0 0") } l, ok = parseLatLong("89", "2", "3", 90) if !ok || l != ((89*60+2)*60+3)*1000 { t.Error("89 2 3") } l, ok = parseLatLong("54", "4", "3.8", 90) if !ok || l != ((54*60+4)*60+3.8)*1000 { t.Error("54 4 3.8") } } func TestParseString(t *testing.T) { l := parseLOCString("") if l != nil { t.Error("") } l = parseLOCString("random stuff") if l != nil { t.Error("") } l = parseLOCString("0 N") if l != nil { t.Error("") } l = parseLOCString("0 N 0 E 0") if l == nil { t.Error("") } if l.Latitude != 1<<31 || l.Longitude != 1<<31 || l.Altitude != 10000000 { t.Error("0 N 0 E 0") } if l.Size != 0x12 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("89 N 23 E 0") if l == nil { t.Error("") } if l.Latitude != 1<<31+89*60*60*1000 || l.Longitude != 1<<31+23*60*60*1000 || l.Altitude != 10000000 { t.Error("89 N 23 E 0") } if l.Size != 0x12 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("89 S 23 W 0") if l == nil { t.Error("") } if l.Latitude != 1<<31-89*60*60*1000 || l.Longitude != 1<<31-23*60*60*1000 || l.Altitude != 10000000 { t.Error("89 N 23 E 0") } if l.Size != 0x12 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("89 2 1.4 N 23 6 9.8 E 0") if l == nil { t.Error("") } if l.Latitude != 1<<31+((89*60+2)*60+1.4)*1000 || l.Longitude != 1<<31+((23*60+6)*60+9.8)*1000 || l.Altitude != 10000000 { t.Error("89 2 1.4 N 23 6 9.8 E 0") } if l.Size != 0x12 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("59 N 10 E 15.0 30.0 2000.0 5.0") if l == nil { t.Error("") } if l.Latitude != 1<<31+59*60*60*1000 || l.Longitude != 1<<31+10*60*60*1000 || l.Altitude != 10001500 { t.Error("59 N 10 E 15.0 30.0 2000.0 5.0") } if l.Size != 0x33 || l.HorizPre != 0x25 || l.VertPre != 0x52 { t.Error("") } l = parseLOCString("42 21 54 N 71 06 18 W -24m 30m") if l == nil { t.Error("") } if l.Latitude != 1<<31+((42*60+21)*60+54)*1000 || l.Longitude != 1<<31-((71*60+6)*60+18)*1000 || l.Altitude != 10000000-24*100 { t.Error("42 21 54 N 71 06 18 W -24m 30m") } if l.Size != 0x33 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("42 21 43.952 N 71 5 6.344 W -24m 1m 200m") if l == nil { t.Error("") } if l.Latitude != 1<<31+((42*60+21)*60+43.952)*1000 || l.Longitude != 1<<31-((71*60+5)*60+6.344)*1000 || l.Altitude != 10000000-24*100 { t.Errorf("42 21 43.952 N 71 5 6.344 W -24m 1m 200m") } if l.Size != 0x12 || l.HorizPre != 0x24 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("52 14 05 N 00 08 50 E 10m") if l == nil { t.Error("") } if l.Latitude != 1<<31+((52*60+14)*60+5)*1000 || l.Longitude != 1<<31+((0*60+8)*60+50)*1000 || l.Altitude != 10000000+10*100 { t.Errorf("52 14 05 N 00 08 50 E 10m") } if l.Size != 0x12 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("2 7 19 S 116 2 25 E 10m") if l == nil { t.Error("") } if l.Latitude != 1<<31-((2*60+7)*60+19)*1000 || l.Longitude != 1<<31+((116*60+2)*60+25)*1000 || l.Altitude != 10000000+10*100 { t.Errorf("2 7 19 S 116 2 25 E 10m") } if l.Size != 0x12 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } l = parseLOCString("42 21 28.764 N 71 00 51.617 W -44m 2000m") if l == nil { t.Error("") } if l.Latitude != 1<<31+((42*60+21)*60+28.764)*1000 || l.Longitude != 1<<31-((71*60+0)*60+51.617)*1000 || l.Altitude != 10000000-44*100 { t.Errorf("42 21 28.764 N 71 00 51.617 W -44m 2000m") } if l.Size != 0x25 || l.HorizPre != 0x16 || l.VertPre != 0x13 { t.Error("") } } func TestStringLOC(t *testing.T) { l := parseLOCString("59 N 10 E 15.0 30.0 2000.0 5.0") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "59 00 0.000 N 10 00 0.000 E 15m 30m 2000m 5m") { t.Error("") } l = parseLOCString("42 21 54 N 71 06 18 W -24m 30m") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "42 21 54.000 N 71 06 18.000 W -24m 30m 10000m 10m") { t.Error("") } l = parseLOCString("42 21 43.952 N 71 5 6.344 W -24m 1m 200m") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "42 21 43.952 N 71 05 6.344 W -24m 1m 200m 10m") { t.Error("") } l = parseLOCString("52 14 05 N 00 08 50 E 10m") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "52 14 5.000 N 00 08 50.000 E 10m 1m 10000m 10m") { t.Error("") } l = parseLOCString("2 7 19 S 116 2 25 E 10m") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "02 07 19.000 S 116 02 25.000 E 10m 1m 10000m 10m") { t.Error("") } l = parseLOCString("42 21 28.764 N 71 00 51.617 W -44m 2000m") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "42 21 28.764 N 71 00 51.617 W -44m 2000m 10000m 10m") { t.Error("") } l = parseLOCString("42 21 28.764 N 71 00 51.617 W -44.55m 2000m") if l == nil { t.Error("") } if !strings.HasSuffix(l.String(), "42 21 28.764 N 71 00 51.617 W -44.55m 2000m 10000m 10m") { t.Error(l.String()) } }