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

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