Blame opae-libs/tests/xfpga/test_wsid_list_c.cpp

Packit 534379
// Copyright(c) 2017-2018, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
Packit 534379
#ifdef __cplusplus
Packit 534379
Packit 534379
extern "C" {
Packit 534379
#endif
Packit 534379
#include <opae/utils.h>
Packit 534379
#include "wsid_list_int.h"
Packit 534379
Packit 534379
#ifdef __cplusplus
Packit 534379
}
Packit 534379
#endif
Packit 534379
#include <random>
Packit 534379
#include <chrono>
Packit 534379
#include <thread>
Packit 534379
#include "gtest/gtest.h"
Packit 534379
Packit 534379
#ifndef BUILD_ASE
Packit 534379
 /*
Packit 534379
 * On hardware, the mmio map is a hash table.
Packit 534379
 */
Packit 534379
static bool mmio_map_is_empty(struct wsid_tracker *root) {
Packit 534379
  if (!root || (root->n_hash_buckets == 0))
Packit 534379
    { return true; }
Packit 534379
  else{
Packit 534379
    uint64_t i;
Packit 534379
    for (i = 0; i < root->n_hash_buckets; ++i) {
Packit 534379
      if (root->table[i])
Packit 534379
        { return false; }
Packit 534379
    }
Packit 534379
  }
Packit 534379
   return true;
Packit 534379
}
Packit 534379
#else
Packit 534379
 /*
Packit 534379
 * In ASE, the mmio map is a list.
Packit 534379
 */
Packit 534379
static bool mmio_map_is_empty(struct wsid_map *root) {
Packit 534379
  return !root;
Packit 534379
}
Packit 534379
#endif
Packit 534379
Packit 534379
// define some operators to alter index consistently
Packit 534379
constexpr uint64_t index_to_wsid(uint64_t i) { return i * 6; }
Packit 534379
constexpr uint64_t index_to_addr(uint64_t i) { return i * 5; }
Packit 534379
constexpr uint64_t index_to_phys(uint64_t i) { return i * 4; }
Packit 534379
constexpr uint64_t index_to_len(uint64_t i) { return i * 3; }
Packit 534379
constexpr uint64_t index_to_offset(uint64_t i) { return i * 2; }
Packit 534379
constexpr uint64_t index_to_index(uint64_t i) { return i * 1; }
Packit 534379
constexpr uint64_t index_to_flags(uint64_t i) { return i * i; }
Packit 534379
Packit 534379
static uint64_t stress_count = 0;
Packit 534379
Packit 534379
void cleanup_cb(wsid_map *ws) { (void) ws; stress_count--; }
Packit 534379
   
Packit 534379
class wsid_list_f : public ::testing::Test {
Packit 534379
 protected:
Packit 534379
  wsid_list_f() 
Packit 534379
       : wsid_root_(nullptr) {}
Packit 534379
Packit 534379
  virtual void SetUp() override {
Packit 534379
    wsid_root_ = wsid_tracker_init(1000);
Packit 534379
    count_ = 100;
Packit 534379
    distribution_ = std::uniform_int_distribution<int>(0, count_);
Packit 534379
    uint64_t i;
Packit 534379
    for (i = 0; i < count_; ++i) {
Packit 534379
      EXPECT_TRUE(wsid_add(wsid_root_, index_to_wsid(i), index_to_addr(i),
Packit 534379
                           index_to_phys(i), index_to_len(i),
Packit 534379
                           index_to_offset(i), index_to_index(i),
Packit 534379
                           index_to_flags(i)));
Packit 534379
    }
Packit 534379
  }
Packit 534379
Packit 534379
  virtual void TearDown() override {
Packit 534379
      auto cleanup = [](struct wsid_map *w) -> void {
Packit 534379
           EXPECT_EQ(w->wsid, index_to_wsid(w->index));};
Packit 534379
 
Packit 534379
      bool empty = mmio_map_is_empty(wsid_root_);
Packit 534379
      if ( !empty ) {
Packit 534379
        wsid_tracker_cleanup(wsid_root_, cleanup);
Packit 534379
        wsid_root_ = nullptr;
Packit 534379
      }
Packit 534379
  }
Packit 534379
Packit 534379
  struct wsid_tracker *wsid_root_;
Packit 534379
  uint64_t count_;
Packit 534379
  std::default_random_engine generator_;
Packit 534379
  std::uniform_int_distribution<int> distribution_;
Packit 534379
};
Packit 534379
Packit 534379
/*
Packit 534379
 * @test    wsid_init_neg
Packit 534379
 *
Packit 534379
 * @details When wsid_tracker_init()'s n_hash_buckets parameter
Packit 534379
 *          is greater then the max, the function returns NULL.
Packit 534379
 */
Packit 534379
TEST_F(wsid_list_f, wsid_init_neg) {
Packit 534379
  EXPECT_EQ(wsid_tracker_init(123456789), nullptr);
Packit 534379
}
Packit 534379
Packit 534379
TEST_F(wsid_list_f, wsid_add) {
Packit 534379
  // the setup adds, now we just confirm that it added the right data
Packit 534379
  wsid_map *it = nullptr;
Packit 534379
  int i = count_;
Packit 534379
  while (i-- >= 0) {
Packit 534379
    it = wsid_find_by_index(wsid_root_, i);
Packit 534379
    if (it) {
Packit 534379
      EXPECT_EQ(it->wsid, index_to_wsid(i));
Packit 534379
      EXPECT_EQ(it->addr, index_to_addr(i));
Packit 534379
      EXPECT_EQ(it->phys, index_to_phys(i));
Packit 534379
      EXPECT_EQ(it->len, index_to_len(i));
Packit 534379
      EXPECT_EQ(it->offset, index_to_offset(i));
Packit 534379
      EXPECT_EQ(it->index, index_to_index(i));
Packit 534379
      ASSERT_EQ(it->flags, index_to_flags(i));
Packit 534379
      it = nullptr;
Packit 534379
    }
Packit 534379
  }
Packit 534379
  it = nullptr;
Packit 534379
}
Packit 534379
Packit 534379
TEST_F(wsid_list_f, wsid_del) {
Packit 534379
  uint32_t wsid = index_to_wsid(distribution_(generator_));
Packit 534379
  EXPECT_TRUE(wsid_del(wsid_root_, wsid));
Packit 534379
  wsid_map *it = wsid_find(wsid_root_, wsid);
Packit 534379
  // now look for the wsid in the list
Packit 534379
  while (it != nullptr) {
Packit 534379
    if (it->wsid == wsid) {
Packit 534379
      break;
Packit 534379
    }
Packit 534379
    it = it->next;
Packit 534379
  }
Packit 534379
  // it is null when we've looked at whole list without finding wsid
Packit 534379
  EXPECT_EQ(it, nullptr);
Packit 534379
  // it isn't there so we shouldn't be able to delete it again
Packit 534379
  EXPECT_FALSE(wsid_del(wsid_root_, wsid));
Packit 534379
}
Packit 534379
Packit 534379
TEST_F(wsid_list_f, wsid_find) {
Packit 534379
  uint32_t index = distribution_(generator_);
Packit 534379
  wsid_map *ws = wsid_find_by_index(wsid_root_, index);
Packit 534379
  ASSERT_NE(ws, nullptr);
Packit 534379
  EXPECT_EQ(ws->wsid, index_to_wsid(index));
Packit 534379
}
Packit 534379
Packit 534379
TEST_F(wsid_list_f, wsid_find_by_index) {
Packit 534379
  uint64_t index = distribution_(generator_);
Packit 534379
  wsid_map *ws = wsid_find(wsid_root_, index_to_wsid(index));
Packit 534379
  ASSERT_NE(ws, nullptr);
Packit 534379
  EXPECT_EQ(ws->index, index);
Packit 534379
}
Packit 534379
Packit 534379
TEST_F(wsid_list_f, stress) {
Packit 534379
  uint64_t count = count_;
Packit 534379
  // FIXME: wsid_add can result in process being killed (out of memory) if it's
Packit 534379
  // called too many times.
Packit 534379
  uint64_t count_max = 1024;
Packit 534379
  for (count = count_; count < count_max; ++count) {
Packit 534379
    EXPECT_TRUE(wsid_add(wsid_root_, index_to_wsid(count),
Packit 534379
                         index_to_addr(count), index_to_phys(count),
Packit 534379
                         index_to_len(count), index_to_offset(count),
Packit 534379
                         index_to_index(count), index_to_flags(count)));
Packit 534379
  }
Packit 534379
  stress_count = count;
Packit 534379
  wsid_tracker_cleanup(wsid_root_, cleanup_cb);
Packit 534379
  EXPECT_EQ(stress_count, 0);
Packit 534379
  wsid_root_ = nullptr;
Packit 534379
}