Blame test/test-fs-fd-hash.c

Packit Service e08953
/* Copyright libuv project contributors. All rights reserved.
Packit Service e08953
 *
Packit Service e08953
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit Service e08953
 * of this software and associated documentation files (the "Software"), to
Packit Service e08953
 * deal in the Software without restriction, including without limitation the
Packit Service e08953
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit Service e08953
 * sell copies of the Software, and to permit persons to whom the Software is
Packit Service e08953
 * furnished to do so, subject to the following conditions:
Packit Service e08953
 *
Packit Service e08953
 * The above copyright notice and this permission notice shall be included in
Packit Service e08953
 * all copies or substantial portions of the Software.
Packit Service e08953
 *
Packit Service e08953
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service e08953
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service e08953
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service e08953
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit Service e08953
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit Service e08953
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit Service e08953
 * IN THE SOFTWARE.
Packit Service e08953
 */
Packit Service e08953
Packit Service e08953
#if defined(_WIN32) && !defined(USING_UV_SHARED)
Packit Service e08953
Packit Service e08953
#include "uv.h"
Packit Service e08953
#include "task.h"
Packit Service e08953
Packit Service e08953
#include "../src/win/fs-fd-hash-inl.h"
Packit Service e08953
Packit Service e08953
Packit Service e08953
#define HASH_MAX 1000000000
Packit Service e08953
#define HASH_INC (1000 * UV__FD_HASH_SIZE + 2)
Packit Service e08953
#define BUCKET_MAX (UV__FD_HASH_SIZE * UV__FD_HASH_GROUP_SIZE * 10)
Packit Service e08953
#define BUCKET_INC UV__FD_HASH_SIZE
Packit Service e08953
#define FD_DIFF 9
Packit Service e08953
Packit Service e08953
Packit Service e08953
void assert_nonexistent(int fd) {
Packit Service e08953
  struct uv__fd_info_s info = { 0 };
Packit Service e08953
  ASSERT(!uv__fd_hash_get(fd, &info));
Packit Service e08953
  ASSERT(!uv__fd_hash_remove(fd, &info));
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
void assert_existent(int fd) {
Packit Service e08953
  struct uv__fd_info_s info = { 0 };
Packit Service e08953
  ASSERT(uv__fd_hash_get(fd, &info));
Packit Service e08953
  ASSERT(info.flags == fd + FD_DIFF);
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
void assert_insertion(int fd) {
Packit Service e08953
  struct uv__fd_info_s info = { 0 };
Packit Service e08953
  assert_nonexistent(fd);
Packit Service e08953
  info.flags = fd + FD_DIFF;
Packit Service e08953
  uv__fd_hash_add(fd, &info;;
Packit Service e08953
  assert_existent(fd);
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
void assert_removal(int fd) {
Packit Service e08953
  struct uv__fd_info_s info = { 0 };
Packit Service e08953
  assert_existent(fd);
Packit Service e08953
  uv__fd_hash_remove(fd, &info;;
Packit Service e08953
  ASSERT(info.flags == fd + FD_DIFF);
Packit Service e08953
  assert_nonexistent(fd);
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
Packit Service e08953
/* Run a function for a set of values up to a very high number */
Packit Service e08953
#define RUN_HASH(function)                                                   \
Packit Service e08953
  do {                                                                       \
Packit Service e08953
    for (fd = 0; fd < HASH_MAX; fd += HASH_INC) {                            \
Packit Service e08953
      function(fd);                                                          \
Packit Service e08953
    }                                                                        \
Packit Service e08953
  } while (0)
Packit Service e08953
Packit Service e08953
/* Run a function for a set of values that will cause many collisions */
Packit Service e08953
#define RUN_COLLISIONS(function)                                             \
Packit Service e08953
  do {                                                                       \
Packit Service e08953
    for (fd = 1; fd < BUCKET_MAX; fd += BUCKET_INC) {                        \
Packit Service e08953
      function(fd);                                                          \
Packit Service e08953
    }                                                                        \
Packit Service e08953
  } while (0)
Packit Service e08953
Packit Service e08953
Packit Service e08953
TEST_IMPL(fs_fd_hash) {
Packit Service e08953
  int fd;
Packit Service e08953
Packit Service e08953
  uv__fd_hash_init();
Packit Service e08953
Packit Service e08953
  /* Empty table */
Packit Service e08953
  RUN_HASH(assert_nonexistent);
Packit Service e08953
  RUN_COLLISIONS(assert_nonexistent);
Packit Service e08953
Packit Service e08953
  /* Fill up */
Packit Service e08953
  RUN_HASH(assert_insertion);
Packit Service e08953
  RUN_COLLISIONS(assert_insertion);
Packit Service e08953
Packit Service e08953
  /* Full */
Packit Service e08953
  RUN_HASH(assert_existent);
Packit Service e08953
  RUN_COLLISIONS(assert_existent);
Packit Service e08953
Packit Service e08953
  /* Update */
Packit Service e08953
  {
Packit Service e08953
    struct uv__fd_info_s info = { 0 };
Packit Service e08953
    info.flags = FD_DIFF + FD_DIFF;
Packit Service e08953
    uv__fd_hash_add(0, &info;;
Packit Service e08953
  }
Packit Service e08953
  {
Packit Service e08953
    struct uv__fd_info_s info = { 0 };
Packit Service e08953
    ASSERT(uv__fd_hash_get(0, &info));
Packit Service e08953
    ASSERT(info.flags == FD_DIFF + FD_DIFF);
Packit Service e08953
  }
Packit Service e08953
  {
Packit Service e08953
    /* Leave as it was, will be again tested below */
Packit Service e08953
    struct uv__fd_info_s info = { 0 };
Packit Service e08953
    info.flags = FD_DIFF;
Packit Service e08953
    uv__fd_hash_add(0, &info;;
Packit Service e08953
  }
Packit Service e08953
Packit Service e08953
  /* Remove all */
Packit Service e08953
  RUN_HASH(assert_removal);
Packit Service e08953
  RUN_COLLISIONS(assert_removal);
Packit Service e08953
Packit Service e08953
  /* Empty table */
Packit Service e08953
  RUN_HASH(assert_nonexistent);
Packit Service e08953
  RUN_COLLISIONS(assert_nonexistent);
Packit Service e08953
  
Packit Service e08953
  return 0;
Packit Service e08953
}
Packit Service e08953
Packit Service e08953
#else
Packit Service e08953
Packit Service e08953
typedef int file_has_no_tests;  /* ISO C forbids an empty translation unit. */
Packit Service e08953
Packit Service e08953
#endif  /* ifndef _WIN32 */