Add SPI serialization/deserialization for the cameras

Change-Id: Ide6b13de583a65907ff2b927a2a6fd1fc507a5b0
diff --git a/aos/containers/sized_array.h b/aos/containers/sized_array.h
index 0b8b447..0c13ecc 100644
--- a/aos/containers/sized_array.h
+++ b/aos/containers/sized_array.h
@@ -35,6 +35,21 @@
   SizedArray &operator=(const SizedArray &) = default;
   SizedArray &operator=(SizedArray &&) = default;
 
+  bool operator==(const SizedArray &other) const {
+    if (other.size() != size()) {
+      return false;
+    }
+    for (size_t i = 0; i < size(); ++i) {
+      if (other[i] != (*this)[i]) {
+        return false;
+      }
+    }
+    return true;
+  }
+  bool operator!=(const SizedArray &other) const {
+    return !(*this == other);
+  }
+
   reference at(size_t i) {
     check_index(i);
     return array_.at(i);
diff --git a/aos/util/BUILD b/aos/util/BUILD
index 99ae87d..e712d54 100644
--- a/aos/util/BUILD
+++ b/aos/util/BUILD
@@ -1,5 +1,27 @@
 package(default_visibility = ["//visibility:public"])
 
+cc_library(
+    name = "bitpacking",
+    hdrs = [
+        "bitpacking.h",
+    ],
+    visibility = ["//visibility:public"],
+    deps = [
+        "//third_party/GSL",
+    ],
+)
+
+cc_test(
+    name = "bitpacking_test",
+    srcs = [
+        "bitpacking_test.cc",
+    ],
+    deps = [
+        ":bitpacking",
+        "//aos/testing:googletest",
+    ],
+)
+
 py_library(
     name = "py_trapezoid_profile",
     srcs = [
diff --git a/aos/util/bitpacking.h b/aos/util/bitpacking.h
new file mode 100644
index 0000000..df6ad16
--- /dev/null
+++ b/aos/util/bitpacking.h
@@ -0,0 +1,140 @@
+#ifndef AOS_UTIL_BITPACKING_H_
+#define AOS_UTIL_BITPACKING_H_
+
+#include <assert.h>
+
+#include <type_traits>
+
+#include "third_party/GSL/include/gsl/gsl"
+
+namespace aos {
+
+template <typename Integer>
+typename std::enable_if<std::is_unsigned<Integer>::value, Integer>::type
+MaskOnes(size_t bits) {
+  // Get these edge cases out of the way first, so we can subtract 1 from bits
+  // safely later without getting a negative number.
+  if (bits == 0) {
+    return 0;
+  }
+  if (bits == 1) {
+    return 1;
+  }
+  static constexpr Integer kOne = 1;
+  // Note that we shift at most by bits - 1. bits == sizeof(Integer) * 8 is
+  // valid, and shifting by the width of a type is undefined behavior, so we
+  // need to get a bit fancy to make it all work. Just ORing high_bit in
+  // explicitly at the end makes it work.
+  const Integer high_bit = kOne << (bits - 1);
+  return (high_bit - kOne) | high_bit;
+}
+
+template <typename Integer, size_t bits, size_t offset>
+typename std::enable_if<std::is_unsigned<Integer>::value &&
+                        sizeof(Integer) * 8 >= bits>::type
+PackBits(const Integer value, const gsl::span<char> destination) {
+  assert(static_cast<size_t>(destination.size()) * 8u >= bits + offset);
+  size_t bits_completed = 0;
+  while (bits_completed < bits) {
+    // Which logical bit (through all the bytes) we're writing at.
+    const size_t output_bit = offset + bits_completed;
+    // The lowest-numbered bit in the current byte we're writing to.
+    const size_t output_min_bit = output_bit % 8;
+    // The number of bits we're writing in this byte.
+    const size_t new_bits = std::min(8 - output_min_bit, bits - bits_completed);
+    // The highest-numbered bit in the current byte we're writing to.
+    const size_t output_max_bit = output_min_bit + new_bits;
+    // The new bits to set in the this byte.
+    const uint8_t new_byte_part =
+        (value >> bits_completed) & MaskOnes<Integer>(new_bits);
+    // A mask of bits to keep from the current value of this byte. Start with
+    // just the low ones.
+    uint8_t existing_mask = MaskOnes<uint8_t>(output_min_bit);
+    // And then add in the high bits to keep.
+    existing_mask |= MaskOnes<uint8_t>(std::max<int>(8 - output_max_bit, 0))
+                     << output_max_bit;
+    // The index of the byte we're writing to.
+    const size_t byte_index = output_bit / 8;
+    // The full new value of the current byte. Start with just the existing bits
+    // we're not touching.
+    uint8_t new_byte = destination[byte_index] & existing_mask;
+    // Add in the new part.
+    new_byte |= new_byte_part << output_min_bit;
+    destination[byte_index] = new_byte;
+    bits_completed += new_bits;
+  }
+  assert(bits_completed == bits);
+}
+
+template <typename Integer, size_t bits, size_t offset>
+typename std::enable_if<std::is_unsigned<Integer>::value &&
+                        sizeof(Integer) * 8 >= bits, Integer>::type
+UnpackBits(const gsl::span<const char> source) {
+  Integer result = 0;
+  assert(static_cast<size_t>(source.size()) * 8u >= bits + offset);
+  size_t bits_completed = 0;
+  while (bits_completed < bits) {
+    // Which logical bit (through all the bytes) we're reading at.
+    const size_t input_bit = offset + bits_completed;
+    // The lowest-numbered bit in the current byte we're reading from.
+    const size_t input_min_bit = input_bit % 8;
+    // The number of bits we're reading in this byte.
+    const size_t new_bits = std::min(8 - input_min_bit, bits - bits_completed);
+    // The index of the byte we're reading from.
+    const size_t byte_index = input_bit / 8;
+    // The part of the current byte we're actually reading.
+    const uint8_t new_byte_part =
+        (source[byte_index] >> input_min_bit) & MaskOnes<Integer>(new_bits);
+    result |= static_cast<Integer>(new_byte_part) << bits_completed;
+    bits_completed += new_bits;
+  }
+  assert(bits_completed == bits);
+  return result;
+}
+
+template <int bits>
+uint32_t FloatToIntLinear(float min, float max, float value) {
+  static_assert(bits <= 31, "Only support 32-bit outputs for now");
+  static_assert(bits >= 1, "Bits must be positive");
+  // Start such that value in [0, 1) maps to [0, 2**bits) in the final
+  // result.
+  float result = (value - min) / (max - min);
+  // Multiply so that value is in [0, 2**bits).
+  // Make sure we do the shifting in a 32-bit integer, despite C++'s weird
+  // integer promotions, which is safe because bits is at most 31.
+  result *= static_cast<uint32_t>(UINT32_C(1) << bits);
+  if (result <= 0.0f) {
+    return 0;
+  }
+  const float max_result = MaskOnes<uint32_t>(bits);
+  if (result >= max_result) {
+    return max_result;
+  }
+  return static_cast<uint32_t>(result);
+}
+
+template <int bits>
+float IntToFloatLinear(float min, float max, uint32_t value) {
+  static_assert(bits <= 31, "Only support 32-bit outputs for now");
+  static_assert(bits >= 1, "Bits must be positive");
+  const float max_value = MaskOnes<uint32_t>(bits);
+  if (value > max_value) {
+    value = max_value;
+  }
+  // Start such that result in [0, 2**bits) maps to [min, max) in the final
+  // result.
+  float result = value;
+  // Offset by half a bit so we return a value in the middle of each one.
+  // This causes us to return the middle floating point value which could be
+  // represented by a given integer value.
+  result += 0.5f;
+  // Multiply so that result is in [0, 1).
+  // Make sure we do the shifting in a 32-bit integer, despite C++'s weird
+  // integer promotions, which is safe because bits is at most 31.
+  result *= 1.0f / static_cast<uint32_t>(UINT32_C(1) << bits);
+  return min + result * (max - min);
+}
+
+}  // namespace aos
+
+#endif  // AOS_UTIL_BITPACKING_H_
diff --git a/aos/util/bitpacking_test.cc b/aos/util/bitpacking_test.cc
new file mode 100644
index 0000000..013e911
--- /dev/null
+++ b/aos/util/bitpacking_test.cc
@@ -0,0 +1,387 @@
+#include "aos/util/bitpacking.h"
+
+#include <stdint.h>
+
+#include "gtest/gtest.h"
+
+namespace aos {
+namespace testing {
+
+// Tests MaskOnes with small arguments.
+TEST(MaskOnesTest, Small) {
+  EXPECT_EQ(0u, MaskOnes<uint8_t>(0));
+  EXPECT_EQ(0u, MaskOnes<uint64_t>(0));
+  EXPECT_EQ(1u, MaskOnes<uint8_t>(1));
+  EXPECT_EQ(1u, MaskOnes<uint64_t>(1));
+}
+
+// Tests MaskOnes with large arguments.
+TEST(MaskOnesTest, Large) {
+  EXPECT_EQ(0xFFu, MaskOnes<uint8_t>(8));
+  EXPECT_EQ(0x7Fu, MaskOnes<uint8_t>(7));
+
+  EXPECT_EQ(0xFFu, MaskOnes<uint64_t>(8));
+  EXPECT_EQ(UINT64_C(0xFFFFFFFFFFFFFFFF), MaskOnes<uint64_t>(64));
+  EXPECT_EQ(UINT64_C(0x7FFFFFFFFFFFFFFF), MaskOnes<uint64_t>(63));
+}
+
+// Tests some simple non-edge-case use cases for PackBits.
+TEST(PackBitsTest, Basic) {
+  {
+    std::array<char, 3> buffer{};
+    PackBits<uint8_t, 8, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 3>{}), buffer);
+    PackBits<uint8_t, 8, 0>(9, buffer);
+    EXPECT_EQ((std::array<char, 3>{{9, 0, 0}}), buffer);
+    PackBits<uint8_t, 8, 8>(7, buffer);
+    EXPECT_EQ((std::array<char, 3>{{9, 7, 0}}), buffer);
+    PackBits<uint8_t, 8, 16>(1, buffer);
+    EXPECT_EQ((std::array<char, 3>{{9, 7, 1}}), buffer);
+  }
+  {
+    std::array<char, 3> buffer{};
+    PackBits<uint16_t, 16, 0>(0xdead, buffer);
+    EXPECT_EQ((std::array<char, 3>{
+                  {static_cast<char>(0xad), static_cast<char>(0xde), 0}}),
+              buffer);
+  }
+  {
+    std::array<char, 3> buffer{};
+    PackBits<uint8_t, 4, 0>(0xd7, buffer);
+    EXPECT_EQ((std::array<char, 3>{{0x7, 0, 0}}), buffer);
+  }
+  {
+    std::array<char, 3> buffer{};
+    PackBits<uint8_t, 4, 4>(0xd7, buffer);
+    EXPECT_EQ((std::array<char, 3>{{0x70, 0, 0}}), buffer);
+  }
+}
+
+// Verifies that PackBits puts bits in an order consistent with increasing
+// offsets.
+TEST(PackBitsTest, Consistency) {
+  {
+    std::array<char, 3> buffer1{};
+    PackBits<uint8_t, 8, 0>(0x80, buffer1);
+    std::array<char, 3> buffer2{};
+    PackBits<uint8_t, 1, 7>(0x1, buffer2);
+    EXPECT_EQ(buffer1, buffer2);
+  }
+  {
+    std::array<char, 1> buffer1{{static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 8, 0>(0x7F, buffer1);
+    std::array<char, 1> buffer2{{static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 1, 7>(0x0, buffer2);
+    EXPECT_EQ(buffer1, buffer2);
+  }
+  {
+    std::array<char, 1> buffer1{};
+    PackBits<uint8_t, 3, 5>(0x7, buffer1);
+    std::array<char, 1> buffer2{};
+    PackBits<uint8_t, 5, 3>(0x3C, buffer2);
+    EXPECT_EQ(buffer1, buffer2);
+  }
+  {
+    std::array<char, 1> buffer1{{static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 3, 5>(0x0, buffer1);
+    std::array<char, 1> buffer2{{static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 5, 3>(0x03, buffer2);
+    EXPECT_EQ(buffer1, buffer2);
+  }
+}
+
+// Tests some simple non-edge-case use cases for UnpackBits.
+TEST(UnpackBitsTest, Basic) {
+  {
+    std::array<char, 3> buffer{};
+    EXPECT_EQ(0u, (UnpackBits<uint8_t, 8, 0>(buffer)));
+    buffer = {{9, 0, 0}};
+    EXPECT_EQ(9u, (UnpackBits<uint8_t, 8, 0>(buffer)));
+    buffer = {{9, 7, 0}};
+    EXPECT_EQ(9u, (UnpackBits<uint8_t, 8, 0>(buffer)));
+    EXPECT_EQ(7u, (UnpackBits<uint8_t, 8, 8>(buffer)));
+    buffer = {{9, 7, 1}};
+    EXPECT_EQ(9u, (UnpackBits<uint8_t, 8, 0>(buffer)));
+    EXPECT_EQ(7u, (UnpackBits<uint8_t, 8, 8>(buffer)));
+    EXPECT_EQ(1u, (UnpackBits<uint8_t, 8, 16>(buffer)));
+  }
+  {
+    const std::array<char, 3> buffer = {
+        {static_cast<char>(0xad), static_cast<char>(0xde), 0}};
+    EXPECT_EQ(0xdead, (UnpackBits<uint16_t, 16, 0>(buffer)));
+  }
+  {
+    const std::array<char, 3> buffer = {{static_cast<char>(0xF7), 0, 0}};
+    EXPECT_EQ(7u, (UnpackBits<uint8_t, 4, 0>(buffer)));
+  }
+  {
+    const std::array<char, 3> buffer = {{static_cast<char>(0x7F), 0, 0}};
+    EXPECT_EQ(7u, (UnpackBits<uint8_t, 4, 4>(buffer)));
+  }
+}
+
+// Tests PackBits split across multiple bytes.
+TEST(PackBitsTest, AcrossBytes) {
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 7>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x80), static_cast<char>(0x7F)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 6>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xC0), static_cast<char>(0x3F)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 5>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xE0), static_cast<char>(0x1F)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 4>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xF0), static_cast<char>(0x0F)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 3>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xF8), static_cast<char>(0x07)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 2>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xFC), static_cast<char>(0x03)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{};
+    PackBits<uint8_t, 8, 1>(0xFF, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xFE), static_cast<char>(0x01)}}),
+              buffer);
+  }
+}
+
+// Tests UnpackBits split across multiple bytes.
+TEST(UnpackBitsTest, AcrossBytes) {
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0x80), static_cast<char>(0x7F)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 7>(buffer)));
+  }
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0xC0), static_cast<char>(0x3F)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 6>(buffer)));
+  }
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0xE0), static_cast<char>(0x1F)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 5>(buffer)));
+  }
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0xF0), static_cast<char>(0x0F)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 4>(buffer)));
+  }
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0xF8), static_cast<char>(0x07)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 3>(buffer)));
+  }
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0xFC), static_cast<char>(0x03)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 2>(buffer)));
+  }
+  {
+    const std::array<char, 2> buffer = {
+        {static_cast<char>(0xFE), static_cast<char>(0x01)}};
+    EXPECT_EQ(0xFFu, (UnpackBits<uint8_t, 8, 1>(buffer)));
+  }
+}
+
+// Verifies that PackBits avoids touching adjacent bits.
+TEST(PackBitsTest, AdjacentBits) {
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 1, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xFE), static_cast<char>(0xFF)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 7, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x80), static_cast<char>(0xFF)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint8_t, 8, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x00), static_cast<char>(0xFF)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint16_t, 9, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x00), static_cast<char>(0xFE)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint16_t, 14, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x00), static_cast<char>(0xC0)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint16_t, 15, 0>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x00), static_cast<char>(0x80)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint16_t, 15, 1>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0x01), static_cast<char>(0x00)}}),
+              buffer);
+  }
+  {
+    std::array<char, 2> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint16_t, 6, 8>(0, buffer);
+    EXPECT_EQ((std::array<char, 2>{
+                  {static_cast<char>(0xFF), static_cast<char>(0xC0)}}),
+              buffer);
+  }
+  {
+    std::array<char, 4> buffer{
+        {static_cast<char>(0xFF), static_cast<char>(0xFF),
+         static_cast<char>(0xFF), static_cast<char>(0xFF)}};
+    PackBits<uint16_t, 6, 24>(0, buffer);
+    EXPECT_EQ((std::array<char, 4>{
+                  {static_cast<char>(0xFF), static_cast<char>(0xFF),
+                   static_cast<char>(0xFF), static_cast<char>(0xC0)}}),
+              buffer);
+  }
+}
+
+// Tests FloatToIntLinear with values near or outside of its boundaries.
+TEST(FloatToIntLinearTest, OutOfBounds) {
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(0.0f, 1.0f, 0.0f)));
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(0.0f, 1.0f, -0.1f)));
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(0.0f, 1.0f, -1.0f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(0.0f, 1.0f, 1.0f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(0.0f, 1.0f, 1.1f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(0.0f, 1.0f, 2.0f)));
+
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(0.0f, 4.0f, 0.0f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(0.0f, 4.0f, 4.0f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(0.0f, 4.0f, 10.0f)));
+
+  EXPECT_EQ(0u, (FloatToIntLinear<3>(0.0f, 4.0f, 0.0f)));
+  EXPECT_EQ(0u, (FloatToIntLinear<3>(0.0f, 4.0f, -100.0f)));
+  EXPECT_EQ(7u, (FloatToIntLinear<3>(0.0f, 4.0f, 4.0f)));
+  EXPECT_EQ(7u, (FloatToIntLinear<3>(0.0f, 4.0f, 4.01f)));
+
+  EXPECT_EQ(0u, (FloatToIntLinear<3>(-3.0f, 5.0f, -3.0f)));
+  EXPECT_EQ(0u, (FloatToIntLinear<3>(-3.0f, 5.0f, -3.1f)));
+  EXPECT_EQ(7u, (FloatToIntLinear<3>(-3.0f, 5.0f, 5.0f)));
+  EXPECT_EQ(7u, (FloatToIntLinear<3>(-3.0f, 5.0f, 5.1f)));
+}
+
+// Tests that FloatToIntLinear rounds correctly at the boundaries between output
+// values.
+TEST(FloatToIntLinearTest, Rounding) {
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(0.0f, 1.0f, 0.49f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(0.0f, 1.0f, 0.51f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(-1.0f, 0.0f, -0.49f)));
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(-1.0f, 0.0f, -0.51f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<1>(-1.0f, 1.0f, 0.01f)));
+  EXPECT_EQ(0u, (FloatToIntLinear<1>(-1.0f, 1.0f, -0.01f)));
+
+  EXPECT_EQ(0u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.124f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.126f)));
+  EXPECT_EQ(1u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.249f)));
+  EXPECT_EQ(2u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.251f)));
+  EXPECT_EQ(2u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.374f)));
+  EXPECT_EQ(3u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.376f)));
+  EXPECT_EQ(3u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.499f)));
+  EXPECT_EQ(4u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.501f)));
+  EXPECT_EQ(4u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.624f)));
+  EXPECT_EQ(5u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.626f)));
+  EXPECT_EQ(5u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.749f)));
+  EXPECT_EQ(6u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.751f)));
+  EXPECT_EQ(6u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.874f)));
+  EXPECT_EQ(7u, (FloatToIntLinear<3>(0.0f, 1.0f, 0.876f)));
+}
+
+// Tests IntToFloatLinear with values near or outside of its boundaries.
+TEST(IntToFloatLinearTest, OutOfBounds) {
+  EXPECT_EQ(0.25f, (IntToFloatLinear<1>(0.0f, 1.0f, 0)));
+  EXPECT_EQ(0.75f, (IntToFloatLinear<1>(0.0f, 1.0f, 1)));
+  EXPECT_EQ(0.75f, (IntToFloatLinear<1>(0.0f, 1.0f, 2)));
+  EXPECT_EQ(0.75f, (IntToFloatLinear<1>(0.0f, 1.0f, 3)));
+
+  EXPECT_EQ(1.0f, (IntToFloatLinear<1>(0.0f, 4.0f, 0)));
+  EXPECT_EQ(3.0f, (IntToFloatLinear<1>(0.0f, 4.0f, 1)));
+
+  EXPECT_EQ(0.0625f, (IntToFloatLinear<3>(0.0f, 1.0f, 0)));
+  EXPECT_EQ(0.9375f, (IntToFloatLinear<3>(0.0f, 1.0f, 7)));
+  EXPECT_EQ(0.9375f, (IntToFloatLinear<3>(0.0f, 1.0f, 8)));
+}
+
+// Tests IntToFloatLinear with some specific values which are easy to calculate
+// by hand.
+TEST(IntToFloatLinearTest, Values) {
+  EXPECT_EQ(0.125f, (IntToFloatLinear<2>(0.0f, 1.0f, 0)));
+  EXPECT_EQ(0.375f, (IntToFloatLinear<2>(0.0f, 1.0f, 1)));
+  EXPECT_EQ(0.625f, (IntToFloatLinear<2>(0.0f, 1.0f, 2)));
+  EXPECT_EQ(0.875f, (IntToFloatLinear<2>(0.0f, 1.0f, 3)));
+
+  EXPECT_EQ(0.0625f, (IntToFloatLinear<3>(0.0f, 1.0f, 0)));
+  EXPECT_EQ(0.1875f, (IntToFloatLinear<3>(0.0f, 1.0f, 1)));
+  EXPECT_EQ(0.3125f, (IntToFloatLinear<3>(0.0f, 1.0f, 2)));
+  EXPECT_EQ(0.4375f, (IntToFloatLinear<3>(0.0f, 1.0f, 3)));
+  EXPECT_EQ(0.5625f, (IntToFloatLinear<3>(0.0f, 1.0f, 4)));
+  EXPECT_EQ(0.6875f, (IntToFloatLinear<3>(0.0f, 1.0f, 5)));
+  EXPECT_EQ(0.8125f, (IntToFloatLinear<3>(0.0f, 1.0f, 6)));
+  EXPECT_EQ(0.9375f, (IntToFloatLinear<3>(0.0f, 1.0f, 7)));
+
+  EXPECT_EQ(-0.875f, (IntToFloatLinear<2>(-1.0f, 0.0f, 0)));
+  EXPECT_EQ(-0.625f, (IntToFloatLinear<2>(-1.0f, 0.0f, 1)));
+  EXPECT_EQ(-0.375f, (IntToFloatLinear<2>(-1.0f, 0.0f, 2)));
+  EXPECT_EQ(-0.125f, (IntToFloatLinear<2>(-1.0f, 0.0f, 3)));
+
+  EXPECT_EQ(-0.75f, (IntToFloatLinear<2>(-1.0f, 1.0f, 0)));
+  EXPECT_EQ(-0.25f, (IntToFloatLinear<2>(-1.0f, 1.0f, 1)));
+  EXPECT_EQ(0.25f, (IntToFloatLinear<2>(-1.0f, 1.0f, 2)));
+  EXPECT_EQ(0.75f, (IntToFloatLinear<2>(-1.0f, 1.0f, 3)));
+}
+
+}  // namespace testing
+}  // namespace aos