astro/sundial/sundial_test.go
starainrt 3ffdbe0034
feat: 扩展天文计算能力
- 新增日食、月食、本地可见性、中心线、半影区域、SVG 图示与沙罗周期信息
- 新增行星冲合、留、方照、物理星历、视直径、相位、亮肢角、轨道节点等计算
- 新增木星伽利略卫星位置、现象与接触事件计算
- 新增恒星星表、星座判定、自行修正与观测辅助能力
- 新增 coord、formula、orbit、sundial、lite/sun、lite/moon 等扩展包
- 完善农历年号、月相英文别名、视差角、大气质量、折射、日晷与双星计算
- 增加 NASA、JPL Horizons、IMCCE 等回归测试数据与基线测试
- 重构基础算法文件组织,补充大量公开 API 注释和语义回归测试
- 更新中文和英文 README,补充示例、精度说明、SVG 配图
2026-05-01 22:38:44 +08:00

130 lines
4.1 KiB
Go

package sundial
import (
"math"
"testing"
"time"
"b612.me/astro/sun"
)
func TestTrueSolarTimeMatchesSunPackage(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
got := TrueSolarTime(date, lon)
want := sun.ApparentSolarTime(date, lon)
if !got.Equal(want) {
t.Fatalf("true solar time mismatch: got %s want %s", got, want)
}
}
func TestMeanSolarTimeMatchesSyntheticLongitudeZone(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
got := MeanSolarTime(date, lon)
want := date.In(time.FixedZone("LTZ", int(lon*3600.0/15.0)))
if math.Abs(got.Sub(want).Seconds()) > 1 {
t.Fatalf("mean solar time mismatch: got %s want %s", got, want)
}
}
func TestEquationTimeMatchesTrueMinusMeanSolarTime(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
apparent := clockHours(TrueSolarTime(date, lon))
mean := clockHours(MeanSolarTime(date, lon))
diff := apparent - mean
want := sun.EquationTime(date)
if math.Abs(diff-want) > 1e-9 {
t.Fatalf("equation-time mismatch: got %.12f want %.12f", diff, want)
}
}
func TestHourAngleMatchesTrueSolarClockFace(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
hourAngle := HourAngle(date, lon)
trueSolar := TrueSolarTime(date, lon)
trueHours := float64(trueSolar.Hour()) +
float64(trueSolar.Minute())/60 +
float64(trueSolar.Second())/3600 +
float64(trueSolar.Nanosecond())/3.6e12
want := normalizeSigned180((trueHours - 12) * 15)
if math.Abs(hourAngle-want) > 0.02 {
t.Fatalf("hour angle mismatch: got %.6f want %.6f", hourAngle, want)
}
}
func TestMeanSolarHourAngleMatchesInstantChain(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
meanDate := MeanSolarTime(date, lon)
meanSolarHours := clockHours(meanDate)
got := MeanSolarHourAngle(meanDate, meanSolarHours)
want := HourAngle(meanDate, longitudeFromTimeZone(meanDate))
if math.Abs(got-want) > 1e-9 {
t.Fatalf("mean-solar hour angle mismatch: got %.12f want %.12f", got, want)
}
}
func TestZoneTimeHourAngleRebuildsTargetClockInstant(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
zoneTimeHours := 9.5
got := ZoneTimeHourAngle(date, lon, zoneTimeHours)
want := HourAngle(dateWithClockHours(date, zoneTimeHours), lon)
if math.Abs(got-want) > 1e-7 {
t.Fatalf("zone-time hour angle mismatch: got %.12f want %.12f", got, want)
}
}
func TestHorizontalHourLineAngleKnownValues(t *testing.T) {
if math.Abs(HorizontalHourLineAngle(0, -75)) > 1e-12 {
t.Fatalf("equatorial horizontal sundial should keep all hour lines on the noon line")
}
got := HorizontalHourLineAngle(45, 45)
want := math.Atan(math.Sin(45*math.Pi/180)*math.Tan(45*math.Pi/180)) * 180 / math.Pi
if math.Abs(got-want) > 1e-12 {
t.Fatalf("known-value mismatch: got %.12f want %.12f", got, want)
}
if math.Abs(HorizontalHourLineAngle(45, -45)+got) > 1e-12 {
t.Fatalf("morning/afternoon symmetry mismatch")
}
}
func TestHorizontalHourLineAngleAtMatchesHourAngleChain(t *testing.T) {
date := time.Date(2026, 6, 21, 15, 30, 45, 123000000, time.FixedZone("CST", 8*3600))
lon := 116.3913
lat := 39.9042
got := HorizontalHourLineAngleAt(date, lon, lat)
want := HorizontalHourLineAngle(lat, HourAngle(date, lon))
if math.Abs(got-want) > 1e-12 {
t.Fatalf("hour-line chain mismatch: got %.12f want %.12f", got, want)
}
}
func TestInvalidHourLineInputReturnsNaN(t *testing.T) {
if !math.IsNaN(HorizontalHourLineAngle(math.NaN(), 30)) {
t.Fatalf("NaN latitude should produce NaN result")
}
if !math.IsNaN(HorizontalHourLineAngle(30, math.Inf(1))) {
t.Fatalf("Inf hour angle should produce NaN result")
}
if !math.IsNaN(MeanSolarHourAngle(time.Now(), math.NaN())) {
t.Fatalf("NaN mean-solar time should produce NaN result")
}
if !math.IsNaN(ZoneTimeHourAngle(time.Now(), math.Inf(1), 12)) {
t.Fatalf("Inf longitude should produce NaN result")
}
}