cacheline_unittest.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <gtest/gtest.h>
  21. #include "butil/time.h"
  22. #include "butil/macros.h"
  23. namespace {
  24. class CachelineTest : public testing::Test {
  25. };
  26. struct BAIDU_CACHELINE_ALIGNMENT Bar {
  27. int y;
  28. };
  29. struct Foo {
  30. char dummy1[0];
  31. int z;
  32. int BAIDU_CACHELINE_ALIGNMENT x[0];
  33. int y;
  34. int m;
  35. Bar bar;
  36. };
  37. TEST_F(CachelineTest, cacheline_alignment) {
  38. ASSERT_EQ(64u, offsetof(Foo, x));
  39. ASSERT_EQ(64u, offsetof(Foo, y));
  40. ASSERT_EQ(68u, offsetof(Foo, m));
  41. ASSERT_EQ(128u, offsetof(Foo, bar));
  42. ASSERT_EQ(64u, sizeof(Bar));
  43. ASSERT_EQ(192u, sizeof(Foo));
  44. }
  45. }