1 /*
2  * PROJECT:         ReactOS kernel-mode tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for ntintsafe.h functions
5  * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <rtltests.h>
9 #define ENABLE_INTSAFE_SIGNED_FUNCTIONS
10 #include <ntintsafe.h>
11 
12 START_TEST(RtlIntSafe)
13 {
14     NTSTATUS Status;
15     INT8 Int8Result;
16     UINT8 UInt8Result;
17     INT IntResult;
18     UINT UIntResult;
19     USHORT UShortResult;
20     SHORT ShortResult;
21     LONGLONG LongLongResult;
22 
23 #define TEST_CONVERSION(FromName, FromType, ToName, ToType, Print, Value, Expected, ExpectedStatus) \
24     do                                                                                              \
25     {                                                                                               \
26         ToName ## Result = (ToType)0xfedcba9876543210;                                              \
27         Status = Rtl ## FromName ## To ## ToName(Value,                                             \
28                                                  &ToName ## Result);                                \
29         ok_eq_hex(Status, ExpectedStatus);                                                          \
30         ok_eq_ ## Print(ToName ## Result, Expected);                                                \
31     } while (0)
32 
33     TEST_CONVERSION(UInt8, UINT8, Int8,   INT8,   int,  0,                  0,              STATUS_SUCCESS);
34     TEST_CONVERSION(UInt8, UINT8, Int8,   INT8,   int,  5,                  5,              STATUS_SUCCESS);
35     TEST_CONVERSION(UInt8, UINT8, Int8,   INT8,   int,  INT8_MAX,           INT8_MAX,       STATUS_SUCCESS);
36     TEST_CONVERSION(UInt8, UINT8, Int8,   INT8,   int,  INT8_MAX + 1,       (INT8)-1,       STATUS_INTEGER_OVERFLOW);
37     TEST_CONVERSION(UInt8, UINT8, Int8,   INT8,   int,  (UINT8)-1,          (INT8)-1,       STATUS_INTEGER_OVERFLOW);
38 
39     TEST_CONVERSION(ULong, ULONG, UShort, USHORT, uint, 0,                  0,              STATUS_SUCCESS);
40     TEST_CONVERSION(ULong, ULONG, UShort, USHORT, uint, 5,                  5,              STATUS_SUCCESS);
41     TEST_CONVERSION(ULong, ULONG, UShort, USHORT, uint, USHORT_MAX,         USHORT_MAX,     STATUS_SUCCESS);
42     TEST_CONVERSION(ULong, ULONG, UShort, USHORT, uint, USHORT_MAX + 1,     (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
43     TEST_CONVERSION(ULong, ULONG, UShort, USHORT, uint, (ULONG)-1,          (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
44 
45     TEST_CONVERSION(ULong, ULONG, Int,    INT,    int,  0,                  0,              STATUS_SUCCESS);
46     TEST_CONVERSION(ULong, ULONG, Int,    INT,    int,  5,                  5,              STATUS_SUCCESS);
47     TEST_CONVERSION(ULong, ULONG, Int,    INT,    int,  INT_MAX,            INT_MAX,        STATUS_SUCCESS);
48     TEST_CONVERSION(ULong, ULONG, Int,    INT,    int,  (ULONG)INT_MAX + 1, (INT)-1,        STATUS_INTEGER_OVERFLOW);
49     TEST_CONVERSION(ULong, ULONG, Int,    INT,    int,  (ULONG)-1,          (INT)-1,        STATUS_INTEGER_OVERFLOW);
50 
51     TEST_CONVERSION(ULong, ULONG, UInt,   UINT,   uint, 0,                  0,              STATUS_SUCCESS);
52     TEST_CONVERSION(ULong, ULONG, UInt,   UINT,   uint, 5,                  5,              STATUS_SUCCESS);
53     TEST_CONVERSION(ULong, ULONG, UInt,   UINT,   uint, UINT_MAX,           UINT_MAX,       STATUS_SUCCESS);
54     TEST_CONVERSION(ULong, ULONG, UInt,   UINT,   uint, (ULONG)-1,          (UINT)-1,       STATUS_SUCCESS);
55 
56     TEST_CONVERSION(Int8,  INT8,  UInt8,  UINT8,  uint, 0,                  0,              STATUS_SUCCESS);
57     TEST_CONVERSION(Int8,  INT8,  UInt8,  UINT8,  uint, 5,                  5,              STATUS_SUCCESS);
58     TEST_CONVERSION(Int8,  INT8,  UInt8,  UINT8,  uint, INT8_MAX,           INT8_MAX,       STATUS_SUCCESS);
59     TEST_CONVERSION(Int8,  INT8,  UInt8,  UINT8,  uint, -1,                 (UINT8)-1,      STATUS_INTEGER_OVERFLOW);
60     TEST_CONVERSION(Int8,  INT8,  UInt8,  UINT8,  uint, INT8_MIN,           (UINT8)-1,      STATUS_INTEGER_OVERFLOW);
61 
62     TEST_CONVERSION(Int8,  INT8,  UShort, USHORT, uint, 0,                  0,              STATUS_SUCCESS);
63     TEST_CONVERSION(Int8,  INT8,  UShort, USHORT, uint, 5,                  5,              STATUS_SUCCESS);
64     TEST_CONVERSION(Int8,  INT8,  UShort, USHORT, uint, INT8_MAX,           INT8_MAX,       STATUS_SUCCESS);
65     TEST_CONVERSION(Int8,  INT8,  UShort, USHORT, uint, -1,                 (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
66     TEST_CONVERSION(Int8,  INT8,  UShort, USHORT, uint, INT8_MIN,           (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
67 
68     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, 0,                  0,              STATUS_SUCCESS);
69     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, 5,                  5,              STATUS_SUCCESS);
70     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, USHORT_MAX,         USHORT_MAX,     STATUS_SUCCESS);
71     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, USHORT_MAX + 1,     (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
72     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, LONG_MAX,           (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
73     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, -1,                 (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
74     TEST_CONVERSION(Long,  LONG,  UShort, USHORT, uint, LONG_MIN,           (USHORT)-1,     STATUS_INTEGER_OVERFLOW);
75 
76     TEST_CONVERSION(Long,  LONG,  UInt,   UINT,   uint, 0,                  0,              STATUS_SUCCESS);
77     TEST_CONVERSION(Long,  LONG,  UInt,   UINT,   uint, 5,                  5,              STATUS_SUCCESS);
78     TEST_CONVERSION(Long,  LONG,  UInt,   UINT,   uint, INT_MAX,            INT_MAX,       STATUS_SUCCESS);
79     TEST_CONVERSION(Long,  LONG,  UInt,   UINT,   uint, -1,                 (UINT)-1,       STATUS_INTEGER_OVERFLOW);
80     TEST_CONVERSION(Long,  LONG,  UInt,   UINT,   uint, LONG_MIN,           (UINT)-1,       STATUS_INTEGER_OVERFLOW);
81 
82     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  0,                  0,              STATUS_SUCCESS);
83     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  5,                  5,              STATUS_SUCCESS);
84     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  INT8_MAX,           INT8_MAX,       STATUS_SUCCESS);
85     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  INT8_MAX + 1,       (INT8)-1,       STATUS_INTEGER_OVERFLOW);
86     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  INT_MAX,            (INT8)-1,       STATUS_INTEGER_OVERFLOW);
87     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  INT8_MIN,           INT8_MIN,       STATUS_SUCCESS);
88     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  INT8_MIN - 1,       (INT8)-1,       STATUS_INTEGER_OVERFLOW);
89     TEST_CONVERSION(Int,   INT,   Int8,   INT8,   int,  INT_MIN,            (INT8)-1,       STATUS_INTEGER_OVERFLOW);
90 
91     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  0,                  0,              STATUS_SUCCESS);
92     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  5,                  5,              STATUS_SUCCESS);
93     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  SHORT_MAX,          SHORT_MAX,      STATUS_SUCCESS);
94     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  SHORT_MAX + 1,      (SHORT)-1,      STATUS_INTEGER_OVERFLOW);
95     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  INT_MAX,            (SHORT)-1,      STATUS_INTEGER_OVERFLOW);
96     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  SHORT_MIN,          SHORT_MIN,      STATUS_SUCCESS);
97     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  SHORT_MIN - 1,      (SHORT)-1,      STATUS_INTEGER_OVERFLOW);
98     TEST_CONVERSION(Int,   INT,   Short,  SHORT,  int,  INT_MIN,            (SHORT)-1,      STATUS_INTEGER_OVERFLOW);
99 
100 #define TEST_ADD(_Name, _Type, _Print, _Value1, _Value2, _Expected, _Status)  do \
101     {                                                                       \
102         _Name ## Result = (_Type)0xfedcba9876543210;                        \
103         Status = Rtl ## _Name ## Add(_Value1, _Value2, & _Name ## Result);  \
104         ok_eq_hex(Status, _Status);                                         \
105         ok_eq_ ## _Print(_Name ## Result, _Expected);                       \
106     } while (0)
107 
108     TEST_ADD(UInt8,     UINT8,      uint,       0,                  0,              0,              STATUS_SUCCESS);
109     TEST_ADD(UInt8,     UINT8,      uint,       5,                  5,              10,             STATUS_SUCCESS);
110     TEST_ADD(UInt8,     UINT8,      uint,       0,                  UINT8_MAX,      UINT8_MAX,      STATUS_SUCCESS);
111     TEST_ADD(UInt8,     UINT8,      uint,       UINT8_MAX,          0,              UINT8_MAX,      STATUS_SUCCESS);
112     TEST_ADD(UInt8,     UINT8,      uint,       UINT8_MAX - 1,      1,              UINT8_MAX,      STATUS_SUCCESS);
113     TEST_ADD(UInt8,     UINT8,      uint,       UINT8_MAX,          1,              (UINT8)-1,      STATUS_INTEGER_OVERFLOW);
114     TEST_ADD(UInt8,     UINT8,      uint,       UINT8_MAX,          UINT8_MAX,      (UINT8)-1,      STATUS_INTEGER_OVERFLOW);
115 
116     TEST_ADD(Int8,      INT8,       int,        0,                  0,              0,              STATUS_SUCCESS);
117     TEST_ADD(Int8,      INT8,       int,        5,                  5,              10,             STATUS_SUCCESS);
118     TEST_ADD(Int8,      INT8,       int,        0,                  INT8_MAX,       INT8_MAX,       STATUS_SUCCESS);
119     TEST_ADD(Int8,      INT8,       int,        INT8_MAX,           0,              INT8_MAX,       STATUS_SUCCESS);
120     TEST_ADD(Int8,      INT8,       int,        INT8_MAX - 1,       1,              INT8_MAX,       STATUS_SUCCESS);
121     TEST_ADD(Int8,      INT8,       int,        INT8_MAX,           1,              (INT8)-1,       STATUS_INTEGER_OVERFLOW);
122     TEST_ADD(Int8,      INT8,       int,        INT8_MAX,           INT8_MAX,       (INT8)-1,       STATUS_INTEGER_OVERFLOW);
123     TEST_ADD(Int8,      INT8,       int,        0,                  -1,             -1,             STATUS_SUCCESS);
124     TEST_ADD(Int8,      INT8,       int,        -1,                 0,              -1,             STATUS_SUCCESS);
125     TEST_ADD(Int8,      INT8,       int,        0,                  INT8_MIN,       INT8_MIN,       STATUS_SUCCESS);
126     TEST_ADD(Int8,      INT8,       int,        INT8_MIN,           0,              INT8_MIN,       STATUS_SUCCESS);
127     TEST_ADD(Int8,      INT8,       int,        INT8_MAX,           INT8_MIN,       (INT8)-1,       STATUS_SUCCESS);
128     TEST_ADD(Int8,      INT8,       int,        INT8_MIN,           -1,             (INT8)-1,       STATUS_INTEGER_OVERFLOW);
129     TEST_ADD(Int8,      INT8,       int,        INT8_MIN,           INT8_MIN,       (INT8)-1,       STATUS_INTEGER_OVERFLOW);
130 
131     TEST_ADD(LongLong,  LONGLONG,   longlong,   0,                  0,              (LONGLONG)0,    STATUS_SUCCESS);
132     TEST_ADD(LongLong,  LONGLONG,   longlong,   5,                  5,              (LONGLONG)10,   STATUS_SUCCESS);
133     TEST_ADD(LongLong,  LONGLONG,   longlong,   0,                  LONGLONG_MAX,   LONGLONG_MAX,   STATUS_SUCCESS);
134     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MAX,       0,              LONGLONG_MAX,   STATUS_SUCCESS);
135     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MAX - 1,   1,              LONGLONG_MAX,   STATUS_SUCCESS);
136     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MAX,       1,              (LONGLONG)-1,   STATUS_INTEGER_OVERFLOW);
137     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MAX,       INT8_MAX,       (LONGLONG)-1,   STATUS_INTEGER_OVERFLOW);
138     TEST_ADD(LongLong,  LONGLONG,   longlong,   0,                  -1,             (LONGLONG)-1,   STATUS_SUCCESS);
139     TEST_ADD(LongLong,  LONGLONG,   longlong,   -1,                 0,              (LONGLONG)-1,   STATUS_SUCCESS);
140     TEST_ADD(LongLong,  LONGLONG,   longlong,   0,                  LONGLONG_MIN,   LONGLONG_MIN,   STATUS_SUCCESS);
141     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MIN,       0,              LONGLONG_MIN,   STATUS_SUCCESS);
142     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MAX,       LONGLONG_MIN,   (LONGLONG)-1,   STATUS_SUCCESS);
143     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MIN,       -1,             (LONGLONG)-1,   STATUS_INTEGER_OVERFLOW);
144     TEST_ADD(LongLong,  LONGLONG,   longlong,   LONGLONG_MIN,       LONGLONG_MIN,   (LONGLONG)-1,   STATUS_INTEGER_OVERFLOW);
145     TEST_ADD(LongLong,  LONGLONG,   longlong,   ULONG_MAX,          1,              0x100000000LL,  STATUS_SUCCESS);
146     TEST_ADD(LongLong,  LONGLONG,   longlong,   ULONG_MAX,          ULONG_MAX,      0x1fffffffeLL,  STATUS_SUCCESS);
147 }
148