LCOV - code coverage report
Current view: top level - src/utils/unittests - bytes.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 15 15 100.0 %
Date: 2025-01-18 12:13:41 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2019  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Purpose: test bytes.h header.
      22             :  */
      23             : #undef NDEBUG
      24             : #include <config.h>
      25             : 
      26             : #include <stdio.h>
      27             : #include <assert.h>
      28             : 
      29             : #ifdef HAVE_STRING_H
      30             : #include <string.h>
      31             : #endif
      32             : 
      33             : #include "tds_sysdep_public.h"
      34             : #include <freetds/bytes.h>
      35             : 
      36             : #define WRITE(buf, off, bytes, endian, val) do { \
      37             :         if ((off % bytes) == 0) TDS_PUT_A ## bytes ## endian(buf+off, val); \
      38             :         else TDS_PUT_UA ## bytes ## endian(buf+off, val); \
      39             : } while(0)
      40             : 
      41             : #define READ(buf, off, bytes, endian) ( \
      42             :         ((off % bytes) == 0) ? TDS_GET_A ## bytes ## endian(buf+off) : \
      43             :         TDS_GET_UA ## bytes ## endian(buf+off) \
      44             : )
      45             : 
      46             : /* check read and write works at a given offset */
      47             : #define CHECK(off, bytes, endian, expected) do { \
      48             :         uint32_t val = READ(bufs.u1.buf, off, bytes, endian); \
      49             :         WRITE(bufs.u2.buf, off, bytes, endian, val); \
      50             :         assert(READ(bufs.u1.buf, off, bytes, endian) == READ(bufs.u2.buf, off, bytes, endian)); \
      51             :         assert(val == expected); \
      52             : } while(0)
      53             : 
      54             : int
      55           8 : main(void)
      56             : {
      57             :         /* this structure make sure buffer are properly aligned */
      58             :         struct {
      59             :                 union {
      60             :                         uint8_t buf[26];
      61             :                         uint32_t a1;
      62             :                 } u1;
      63             :                 union {
      64             :                         uint8_t buf[26];
      65             :                         uint32_t a2;
      66             :                 } u2;
      67             :         } bufs;
      68             :         unsigned n;
      69             : 
      70           8 :         memset(&bufs, 0, sizeof(bufs));
      71         216 :         for (n = 0; n < sizeof(bufs.u1.buf); ++n)
      72         208 :                 bufs.u1.buf[n] = (uint8_t) (123 * n + 67);
      73             : 
      74             :         /* aligned access */
      75           8 :         CHECK(0, 4, LE, 0xb439be43u);
      76           8 :         CHECK(4, 2, LE, 0xaa2fu);
      77           8 :         CHECK(6, 2, BE, 0x25a0u);
      78           8 :         CHECK(8, 4, BE, 0x1b96118cu);
      79             : 
      80           8 :         CHECK(12, 1, LE, 0x7u);
      81             : 
      82             :         /* unaligned access */
      83           8 :         CHECK(13, 2, BE, 0x82fdu);
      84           8 :         CHECK(15, 2, LE, 0xf378u);
      85           8 :         CHECK(17, 4, LE, 0xdf64e96eu);
      86           8 :         CHECK(21, 4, BE, 0x5ad550cbu);
      87             : 
      88           8 :         CHECK(25, 1, BE, 0x46);
      89           8 :         assert(memcmp(bufs.u1.buf, bufs.u2.buf, sizeof(bufs.u1.buf)) == 0);
      90             : 
      91             :         return 0;
      92             : }
      93             : 

Generated by: LCOV version 1.13