refactor: floats, tests
This commit is contained in:
@@ -4,11 +4,126 @@ import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/iliadenisov/galaxy/internal/controller"
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
g "github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// var (
|
||||
// Cruiser = game.ShipType{
|
||||
// Name: "Cruiser",
|
||||
// Drive: 15,
|
||||
// Armament: 1,
|
||||
// Weapons: 15,
|
||||
// Shields: 15,
|
||||
// Cargo: 0,
|
||||
// }
|
||||
// )
|
||||
|
||||
func TestBlockUpgradeCost(t *testing.T) {
|
||||
assert.Equal(t, 00.0, controller.BlockUpgradeCost(1, 1.0, 1.0))
|
||||
assert.Equal(t, 25.0, controller.BlockUpgradeCost(5, 1.0, 2.0))
|
||||
assert.Equal(t, 50.0, controller.BlockUpgradeCost(10, 1.0, 2.0))
|
||||
}
|
||||
|
||||
func TestGroupUpgradeCost(t *testing.T) {
|
||||
sg := &g.ShipGroup{
|
||||
Tech: map[g.Tech]g.Float{
|
||||
g.TechDrive: 1.0,
|
||||
g.TechWeapons: 1.0,
|
||||
g.TechShields: 1.0,
|
||||
g.TechCargo: 1.0,
|
||||
},
|
||||
Number: 1,
|
||||
}
|
||||
assert.Equal(t, 225.0, controller.GroupUpgradeCost(sg, Cruiser, 2.0, 2.0, 2.0, 2.0).UpgradeCost(1))
|
||||
}
|
||||
|
||||
func TestUpgradeMaxShips(t *testing.T) {
|
||||
sg := &g.ShipGroup{
|
||||
Tech: map[g.Tech]g.Float{
|
||||
g.TechDrive: 1.0,
|
||||
g.TechWeapons: 1.0,
|
||||
g.TechShields: 1.0,
|
||||
g.TechCargo: 1.0,
|
||||
},
|
||||
Number: 10,
|
||||
}
|
||||
uc := controller.GroupUpgradeCost(sg, Cruiser, 2.0, 2.0, 2.0, 2.0)
|
||||
assert.Equal(t, uint(4), uc.UpgradeMaxShips(1000))
|
||||
}
|
||||
|
||||
func TestCurrentUpgradingLevel(t *testing.T) {
|
||||
sg := &g.ShipGroup{
|
||||
StateUpgrade: nil,
|
||||
}
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechDrive))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechWeapons))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechShields))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechCargo))
|
||||
|
||||
sg.StateUpgrade = &g.InUpgrade{
|
||||
UpgradeTech: []g.UpgradePreference{
|
||||
{Tech: g.TechDrive, Level: 1.5, Cost: 100.1},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, 1.5, controller.CurrentUpgradingLevel(sg, g.TechDrive))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechWeapons))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechShields))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechCargo))
|
||||
|
||||
sg.StateUpgrade.UpgradeTech = append(sg.StateUpgrade.UpgradeTech, g.UpgradePreference{Tech: g.TechCargo, Level: 2.2, Cost: 200.2})
|
||||
assert.Equal(t, 1.5, controller.CurrentUpgradingLevel(sg, g.TechDrive))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechWeapons))
|
||||
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechShields))
|
||||
assert.Equal(t, 2.2, controller.CurrentUpgradingLevel(sg, g.TechCargo))
|
||||
}
|
||||
|
||||
func TestFutureUpgradeLevel(t *testing.T) {
|
||||
assert.Equal(t, 0.0, controller.FutureUpgradeLevel(2.0, 2.0, 2.0))
|
||||
assert.Equal(t, 0.0, controller.FutureUpgradeLevel(2.0, 2.0, 3.0))
|
||||
assert.Equal(t, 1.5, controller.FutureUpgradeLevel(1.5, 2.0, 3.0))
|
||||
assert.Equal(t, 2.0, controller.FutureUpgradeLevel(2.5, 1.0, 2.0))
|
||||
assert.Equal(t, 2.5, controller.FutureUpgradeLevel(2.5, 1.0, 0.0))
|
||||
}
|
||||
|
||||
func TestUpgradeGroupPreference(t *testing.T) {
|
||||
sg := g.ShipGroup{
|
||||
Number: 4,
|
||||
Tech: g.TechSet{
|
||||
g.TechDrive: 1.0,
|
||||
g.TechWeapons: 1.0,
|
||||
g.TechShields: 1.0,
|
||||
g.TechCargo: 1.0,
|
||||
},
|
||||
}
|
||||
assert.Nil(t, sg.StateUpgrade)
|
||||
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechDrive, 0)
|
||||
assert.Nil(t, sg.StateUpgrade)
|
||||
|
||||
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechDrive, 2.0)
|
||||
assert.NotNil(t, sg.StateUpgrade)
|
||||
assert.Equal(t, 300., sg.StateUpgrade.TechCost(g.TechDrive))
|
||||
assert.Equal(t, 300., sg.StateUpgrade.Cost())
|
||||
|
||||
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechWeapons, 2.0)
|
||||
assert.NotNil(t, sg.StateUpgrade)
|
||||
assert.Equal(t, 300., sg.StateUpgrade.TechCost(g.TechWeapons))
|
||||
assert.Equal(t, 600., sg.StateUpgrade.Cost())
|
||||
|
||||
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechShields, 2.0)
|
||||
assert.NotNil(t, sg.StateUpgrade)
|
||||
assert.Equal(t, 300., sg.StateUpgrade.TechCost(g.TechShields))
|
||||
assert.Equal(t, 900., sg.StateUpgrade.Cost())
|
||||
|
||||
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechCargo, 2.0)
|
||||
assert.NotNil(t, sg.StateUpgrade)
|
||||
assert.Equal(t, 0., sg.StateUpgrade.TechCost(g.TechCargo))
|
||||
assert.Equal(t, 900., sg.StateUpgrade.Cost())
|
||||
}
|
||||
|
||||
func TestUpgradeGroup(t *testing.T) {
|
||||
c, g := newCache()
|
||||
// group #1 - in_orbit, free to upgrade
|
||||
@@ -63,5 +178,5 @@ func TestUpgradeGroup(t *testing.T) {
|
||||
|
||||
assert.ErrorContains(t,
|
||||
g.UpgradeGroup(Race_0.Name, 4, "DRIVE", 1, 1.3),
|
||||
e.GenericErrorText(e.ErrInputUpgradeGroupBreakNotAllowed))
|
||||
e.GenericErrorText(e.ErrShipsBusy))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user