1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75

base / sys_byteorder_unittest.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/sys_byteorder.h"

#include <stdint.h>

#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const uint16_t k16BitTestData = 0xaabb;
const uint16_t k16BitSwappedTestData = 0xbbaa;
const uint32_t k32BitTestData = 0xaabbccdd;
const uint32_t k32BitSwappedTestData = 0xddccbbaa;
const uint64_t k64BitTestData = 0xaabbccdd44332211;
const uint64_t k64BitSwappedTestData = 0x11223344ddccbbaa;

}  // namespace

TEST(ByteOrderTest, NetToHost16) {
  uint16_t h = base::NetToHost16(k16BitTestData);
#if defined(ARCH_CPU_LITTLE_ENDIAN)
  EXPECT_EQ(k16BitSwappedTestData, h);
#else
  EXPECT_EQ(k16BitTestData, h);
#endif
}

TEST(ByteOrderTest, NetToHost32) {
  uint32_t h = base::NetToHost32(k32BitTestData);
#if defined(ARCH_CPU_LITTLE_ENDIAN)
  EXPECT_EQ(k32BitSwappedTestData, h);
#else
  EXPECT_EQ(k32BitTestData, h);
#endif
}

TEST(ByteOrderTest, NetToHost64) {
  uint64_t h = base::NetToHost64(k64BitTestData);
#if defined(ARCH_CPU_LITTLE_ENDIAN)
  EXPECT_EQ(k64BitSwappedTestData, h);
#else
  EXPECT_EQ(k64BitTestData, h);
#endif
}

TEST(ByteOrderTest, HostToNet16) {
  uint16_t n = base::HostToNet16(k16BitTestData);
#if defined(ARCH_CPU_LITTLE_ENDIAN)
  EXPECT_EQ(k16BitSwappedTestData, n);
#else
  EXPECT_EQ(k16BitTestData, n);
#endif
}

TEST(ByteOrderTest, HostToNet32) {
  uint32_t n = base::HostToNet32(k32BitTestData);
#if defined(ARCH_CPU_LITTLE_ENDIAN)
  EXPECT_EQ(k32BitSwappedTestData, n);
#else
  EXPECT_EQ(k32BitTestData, n);
#endif
}

TEST(ByteOrderTest, HostToNet64) {
  uint64_t n = base::HostToNet64(k64BitTestData);
#if defined(ARCH_CPU_LITTLE_ENDIAN)
  EXPECT_EQ(k64BitSwappedTestData, n);
#else
  EXPECT_EQ(k64BitTestData, n);
#endif
}