File: stdbit_test.c

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (61 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download | duplicates (4)
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
//===-- Unittests for stdbit ----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

/*
 * The intent of this test is validate that:
 * 1. We provide the definition of the various type generic macros of stdbit.h
 * (the macros are transitively included from stdbit-macros.h by stdbit.h).
 * 2. It dispatches to the correct underlying function.
 * Because unit tests build without public packaging, the object files produced
 * do not contain non-namespaced symbols.
 */

/*
 * Declare these BEFORE including stdbit-macros.h so that this test may still be
 * run even if a given target doesn't yet have these individual entrypoints
 * enabled.
 */
#include "stdbit_stub.h"

#include "include/llvm-libc-macros/stdbit-macros.h"

#include <assert.h>

#define CHECK_FUNCTION(FUNC_NAME, VAL)                                         \
  do {                                                                         \
    assert(FUNC_NAME((unsigned char)0U) == VAL##AU);                           \
    assert(FUNC_NAME((unsigned short)0U) == VAL##BU);                          \
    assert(FUNC_NAME(0U) == VAL##CU);                                          \
    assert(FUNC_NAME(0UL) == VAL##DU);                                         \
    assert(FUNC_NAME(0ULL) == VAL##EU);                                        \
  } while (0)

int main(void) {
  CHECK_FUNCTION(stdc_leading_zeros, 0xA);
  CHECK_FUNCTION(stdc_leading_ones, 0xB);
  CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
  CHECK_FUNCTION(stdc_trailing_ones, 0xD);
  CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
  CHECK_FUNCTION(stdc_first_leading_one, 0xF);
  CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
  CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
  CHECK_FUNCTION(stdc_count_zeros, 0x2);
  CHECK_FUNCTION(stdc_count_ones, 0x3);

  assert(!stdc_has_single_bit((unsigned char)1U));
  assert(!stdc_has_single_bit((unsigned short)1U));
  assert(!stdc_has_single_bit(1U));
  assert(!stdc_has_single_bit(1UL));
  assert(!stdc_has_single_bit(1ULL));

  CHECK_FUNCTION(stdc_bit_width, 0x4);
  CHECK_FUNCTION(stdc_bit_floor, 0x5);
  CHECK_FUNCTION(stdc_bit_ceil, 0x6);

  return 0;
}