astro/lite/sun/sun_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

82 lines
2.6 KiB
Go

package sun
import (
"math"
"testing"
"time"
fullsun "b612.me/astro/sun"
)
func TestLiteSunPositionAgainstFullPrecision(t *testing.T) {
samples := []time.Time{
time.Date(2026, 1, 5, 12, 0, 0, 0, time.UTC),
time.Date(2026, 3, 20, 6, 0, 0, 0, time.UTC),
time.Date(2026, 6, 21, 0, 0, 0, 0, time.UTC),
time.Date(2026, 9, 22, 18, 0, 0, 0, time.UTC),
time.Date(2026, 12, 21, 12, 0, 0, 0, time.UTC),
}
for _, sample := range samples {
if got, want := ApparentLo(sample), fullsun.ApparentLo(sample); math.Abs(angleDiff(got, want)) > 0.02 {
t.Fatalf("ApparentLo(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), got, want)
}
gotRA, gotDec := ApparentRaDec(sample)
wantRA, wantDec := fullsun.ApparentRaDec(sample)
if math.Abs(angleDiff(gotRA, wantRA)) > 0.03 {
t.Fatalf("ApparentRa(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), gotRA, wantRA)
}
if math.Abs(gotDec-wantDec) > 0.03 {
t.Fatalf("ApparentDec(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), gotDec, wantDec)
}
}
}
func TestLiteSunRiseSetAgainstFullPrecision(t *testing.T) {
cases := []struct {
name string
date time.Time
lon float64
lat float64
}{
{"Shanghai", time.Date(2026, 1, 1, 0, 0, 0, 0, time.FixedZone("CST", 8*3600)), 121.4737, 31.2304},
{"London", time.Date(2026, 3, 20, 0, 0, 0, 0, time.UTC), -0.1278, 51.5074},
{"NewYork", time.Date(2026, 6, 21, 0, 0, 0, 0, time.FixedZone("EST", -5*3600)), -74.0060, 40.7128},
{"Sydney", time.Date(2026, 9, 23, 0, 0, 0, 0, time.FixedZone("AEST", 10*3600)), 151.2093, -33.8688},
}
for _, tc := range cases {
gotRise, gotErr := RiseTime(tc.date, tc.lon, tc.lat, 0, true)
wantRise, wantErr := fullsun.RiseTime(tc.date, tc.lon, tc.lat, 0, true)
if gotErr != nil || wantErr != nil {
t.Fatalf("%s rise unexpected error: got=%v want=%v", tc.name, gotErr, wantErr)
}
assertTimeWithinMinutes(t, tc.name+" rise", gotRise, wantRise, 2.0)
gotSet, gotSetErr := SetTime(tc.date, tc.lon, tc.lat, 0, true)
wantSet, wantSetErr := fullsun.SetTime(tc.date, tc.lon, tc.lat, 0, true)
if gotSetErr != nil || wantSetErr != nil {
t.Fatalf("%s set unexpected error: got=%v want=%v", tc.name, gotSetErr, wantSetErr)
}
assertTimeWithinMinutes(t, tc.name+" set", gotSet, wantSet, 2.0)
}
}
func angleDiff(a, b float64) float64 {
diff := math.Mod(a-b, 360)
if diff > 180 {
diff -= 360
}
if diff < -180 {
diff += 360
}
return diff
}
func assertTimeWithinMinutes(t *testing.T, name string, got, want time.Time, limitMinutes float64) {
t.Helper()
if math.Abs(got.Sub(want).Minutes()) > limitMinutes {
t.Fatalf("%s = %s, want %s", name, got, want)
}
}