From 4ae30d08d2df7fdb8b236b6c650a1a29a84ee562 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Fri, 20 Feb 2009 17:55:55 +0000 Subject: Move units to new module printing. Add printing_unittests on Windows too. Make test_shell and chrome_dll dependent on printing. Review URL: http://codereview.chromium.org/21475 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10105 0039d316-1c4b-4281-b951-d872f2087c98 --- printing/DEPS | 3 + printing/printing.vcproj | 147 +++++++++++++++++++++++++++++++++ printing/printing.vsprops | 8 ++ printing/printing_unittests.vcproj | 165 +++++++++++++++++++++++++++++++++++++ printing/units.cc | 42 ++++++++++ printing/units.h | 27 ++++++ printing/units_unittest.cc | 54 ++++++++++++ 7 files changed, 446 insertions(+) create mode 100644 printing/DEPS create mode 100644 printing/printing.vcproj create mode 100644 printing/printing.vsprops create mode 100644 printing/printing_unittests.vcproj create mode 100644 printing/units.cc create mode 100644 printing/units.h create mode 100644 printing/units_unittest.cc (limited to 'printing') diff --git a/printing/DEPS b/printing/DEPS new file mode 100644 index 0000000..5cd0867 --- /dev/null +++ b/printing/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+base", +] diff --git a/printing/printing.vcproj b/printing/printing.vcproj new file mode 100644 index 0000000..bfc7f95 --- /dev/null +++ b/printing/printing.vcproj @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/printing/printing.vsprops b/printing/printing.vsprops new file mode 100644 index 0000000..5be0f76 --- /dev/null +++ b/printing/printing.vsprops @@ -0,0 +1,8 @@ + + + diff --git a/printing/printing_unittests.vcproj b/printing/printing_unittests.vcproj new file mode 100644 index 0000000..1683999 --- /dev/null +++ b/printing/printing_unittests.vcproj @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/printing/units.cc b/printing/units.cc new file mode 100644 index 0000000..5f543fe --- /dev/null +++ b/printing/units.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/units.h" + +#include "base/logging.h" + +namespace printing { + +int ConvertUnit(int value, int old_unit, int new_unit) { + DCHECK_GT(new_unit, 0); + DCHECK_GT(old_unit, 0); + // With integer arithmetic, to divide a value with correct rounding, you need + // to add half of the divisor value to the dividend value. You need to do the + // reverse with negative number. + if (value >= 0) { + return ((value * new_unit) + (old_unit / 2)) / old_unit; + } else { + return ((value * new_unit) - (old_unit / 2)) / old_unit; + } +} + +double ConvertUnitDouble(double value, double old_unit, double new_unit) { + DCHECK_GT(new_unit, 0); + DCHECK_GT(old_unit, 0); + return value * new_unit / old_unit; +} + +int ConvertMilliInchToHundredThousanthMeter(int milli_inch) { + // 1" == 25.4 mm + // 1" == 25400 um + // 0.001" == 25.4 um + // 0.001" == 2.54 cmm + return ConvertUnit(milli_inch, 100, 254); +} + +int ConvertHundredThousanthMeterToMilliInch(int cmm) { + return ConvertUnit(cmm, 254, 100); +} + +} // namespace printing diff --git a/printing/units.h b/printing/units.h new file mode 100644 index 0000000..d23e268 --- /dev/null +++ b/printing/units.h @@ -0,0 +1,27 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PRINTING_UNITS_H_ +#define PRINTING_UNITS_H_ + +namespace printing { + +// Length of a thousanth of inches in 0.01mm unit. +const int kHundrethsMMPerInch = 2540; + +// Converts from one unit system to another using integer arithmetics. +int ConvertUnit(int value, int old_unit, int new_unit); + +// Converts from one unit system to another using doubles. +double ConvertUnitDouble(double value, double old_unit, double new_unit); + +// Converts from 0.001 inch unit to 0.00001 meter. +int ConvertMilliInchToHundredThousanthMeter(int milli_inch); + +// Converts from 0.00001 meter unit to 0.001 inch. +int ConvertHundredThousanthMeterToMilliInch(int cmm); + +} // namespace printing + +#endif // PRINTING_UNITS_H_ diff --git a/printing/units_unittest.cc b/printing/units_unittest.cc new file mode 100644 index 0000000..e042cf3 --- /dev/null +++ b/printing/units_unittest.cc @@ -0,0 +1,54 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/units.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/gtest/include/gtest/gtest-spi.h" + +using namespace printing; + +TEST(UnitsTest, Convertions) { + EXPECT_EQ(100, ConvertUnit(100, 100, 100)); + EXPECT_EQ(-100, ConvertUnit(-100, 100, 100)); + EXPECT_EQ(0, ConvertUnit(0, 100, 100)); + EXPECT_EQ(99, ConvertUnit(99, 100, 100)); + EXPECT_EQ(101, ConvertUnit(101, 100, 100)); + EXPECT_EQ(99900, ConvertUnit(999, 10, 1000)); + EXPECT_EQ(100100, ConvertUnit(1001, 10, 1000)); + + // Rounding. + EXPECT_EQ(10, ConvertUnit(999, 1000, 10)); + EXPECT_EQ(10, ConvertUnit(950, 1000, 10)); + EXPECT_EQ(9, ConvertUnit(949, 1000, 10)); + EXPECT_EQ(10, ConvertUnit(1001, 1000, 10)); + EXPECT_EQ(10, ConvertUnit(1049, 1000, 10)); + EXPECT_EQ(11, ConvertUnit(1050, 1000, 10)); + EXPECT_EQ(-10, ConvertUnit(-999, 1000, 10)); + EXPECT_EQ(-10, ConvertUnit(-950, 1000, 10)); + EXPECT_EQ(-9, ConvertUnit(-949, 1000, 10)); + EXPECT_EQ(-10, ConvertUnit(-1001, 1000, 10)); + EXPECT_EQ(-10, ConvertUnit(-1049, 1000, 10)); + EXPECT_EQ(-11, ConvertUnit(-1050, 1000, 10)); + + EXPECT_EQ(0, ConvertUnit(2, 1000000000, 1)); + EXPECT_EQ(2000000000, ConvertUnit(2, 1, 1000000000)); + EXPECT_EQ(4000000000U, + static_cast(ConvertUnit(2, 1, 2000000000))); + + EXPECT_EQ(100, ConvertUnitDouble(100, 100, 100)); + EXPECT_EQ(-100, ConvertUnitDouble(-100, 100, 100)); + EXPECT_EQ(0, ConvertUnitDouble(0, 100, 100)); + EXPECT_EQ(0.000002, ConvertUnitDouble(2, 1000, 0.001)); + EXPECT_EQ(2000000, ConvertUnitDouble(2, 0.001, 1000)); + + EXPECT_EQ(kHundrethsMMPerInch, ConvertMilliInchToHundredThousanthMeter(1000)); + EXPECT_EQ(-kHundrethsMMPerInch, + ConvertMilliInchToHundredThousanthMeter(-1000)); + EXPECT_EQ(0, ConvertMilliInchToHundredThousanthMeter(0)); + EXPECT_EQ(1000, ConvertHundredThousanthMeterToMilliInch(kHundrethsMMPerInch)); + EXPECT_EQ(-1000, + ConvertHundredThousanthMeterToMilliInch(-kHundrethsMMPerInch)); + EXPECT_EQ(0, ConvertHundredThousanthMeterToMilliInch(0)); +} + -- cgit v1.1