TEST_F(UnitsFixture,QuantityConverter_OSQuantityVector) { // basic conversion OSQuantityVector testVec(createIPEnergy(),2u,100.0); Quantity testQ(100.0,createIPEnergy()); OSQuantityVector resultVec = convert(testVec,UnitSystem(UnitSystem::Wh)); OptionalQuantity resultQ = convert(testQ,UnitSystem(UnitSystem::Wh)); ASSERT_EQ(2u,resultVec.size()); ASSERT_TRUE(resultQ); EXPECT_EQ(resultQ.get(),resultVec.getQuantity(0)); EXPECT_EQ(resultQ.get(),resultVec.getQuantity(1)); EXPECT_EQ(resultQ->system(),resultVec.system()); resultVec = convert(testVec,resultQ->units()); resultQ = convert(testQ,resultQ->units()); ASSERT_EQ(2u,resultVec.size()); ASSERT_TRUE(resultQ); EXPECT_EQ(resultQ.get(),resultVec.getQuantity(0)); EXPECT_EQ(resultQ.get(),resultVec.getQuantity(1)); EXPECT_EQ(resultQ->system(),resultVec.system()); // temperature conversion testVec = OSQuantityVector(createCelsiusTemperature(),2u,20.0); testQ = Quantity(20.0,createCelsiusTemperature()); resultVec = convert(testVec,UnitSystem(UnitSystem::Fahrenheit)); resultQ = convert(testQ,UnitSystem(UnitSystem::Fahrenheit)); ASSERT_EQ(2u,resultVec.size()); ASSERT_TRUE(resultQ); EXPECT_EQ(resultQ.get().units(),resultVec.units()); // not sure why these aren't quite getting the precision we would like EXPECT_NEAR(resultQ.get().value(),resultVec.getQuantity(0).value(),1.0E-12); EXPECT_NEAR(resultQ.get().value(),resultVec.getQuantity(1).value(),1.0E-12); EXPECT_EQ(resultQ->system(),resultVec.system()); EXPECT_TRUE(resultVec.isAbsolute()); EXPECT_TRUE(resultQ->isAbsolute()); testVec.setAsRelative(); testQ.setAsRelative(); resultVec = convert(testVec,UnitSystem(UnitSystem::Fahrenheit)); resultQ = convert(testQ,UnitSystem(UnitSystem::Fahrenheit)); ASSERT_EQ(2u,resultVec.size()); ASSERT_TRUE(resultQ); EXPECT_EQ(resultQ.get().units(),resultVec.units()); // not sure why these aren't quite getting the precision we would like EXPECT_NEAR(resultQ.get().value(),resultVec.getQuantity(0).value(),1.0E-12); EXPECT_NEAR(resultQ.get().value(),resultVec.getQuantity(1).value(),1.0E-12); EXPECT_EQ(resultQ->system(),resultVec.system()); EXPECT_TRUE(resultVec.isRelative()); EXPECT_TRUE(resultQ->isRelative()); }
OSQuantityVector convert(const OSQuantityVector& original, const Unit& targetUnits) { OSQuantityVector result; Quantity testQuantity(0.0,original.units()); OptionalQuantity offset = convert(testQuantity,targetUnits); if (!offset) { return result; } testQuantity.setValue(1.0); OptionalQuantity factorPlusOffset = convert(testQuantity,targetUnits); OS_ASSERT(factorPlusOffset); OS_ASSERT(offset->units() == factorPlusOffset->units()); result = OSQuantityVector(offset->units(),original.values()); result = result * (factorPlusOffset->value() - offset->value()) + offset.get(); return result; }