tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TestBufferRotation.cpp (6452B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "gtest/gtest.h"
      8 
      9 #include "BufferUnrotate.h"
     10 
     11 using mozilla::gfx::BufferUnrotate;
     12 
     13 static unsigned char* GenerateBuffer(int bytesPerPixel, int width, int height,
     14                                     int stride, int xBoundary, int yBoundary) {
     15  unsigned char* buffer = new unsigned char[stride * height];
     16  for (int y = 0; y < height; y++) {
     17    for (int x = 0; x < width; x++) {
     18      int pos = ((yBoundary + y) % height) * stride +
     19                ((xBoundary + x) % width) * bytesPerPixel;
     20      for (int i = 0; i < bytesPerPixel; i++) {
     21        buffer[pos + i] = (x + y + i * 2) % 256;
     22      }
     23    }
     24  }
     25  return buffer;
     26 }
     27 
     28 static bool CheckBuffer(unsigned char* buffer, int bytesPerPixel, int width,
     29                        int height, int stride) {
     30  int xBoundary = 0;
     31  int yBoundary = 0;
     32  for (int y = 0; y < height; y++) {
     33    for (int x = 0; x < width; x++) {
     34      int pos = ((yBoundary + y) % height) * stride +
     35                ((xBoundary + x) % width) * bytesPerPixel;
     36      for (int i = 0; i < bytesPerPixel; i++) {
     37        if (buffer[pos + i] != (x + y + i * 2) % 256) {
     38          printf("Buffer differs at %i, %i, is %i\n", x, y,
     39                 (int)buffer[pos + i]);
     40          return false;
     41        }
     42      }
     43    }
     44  }
     45  return true;
     46 }
     47 
     48 TEST(Gfx, BufferUnrotateHorizontal)
     49 {
     50  const int NUM_OF_TESTS = 8;
     51  int bytesPerPixelList[2] = {2, 4};
     52  int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99};
     53  int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99};
     54  int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31};
     55  int yBoundary[NUM_OF_TESTS] = {0, 0, 0, 0};
     56 
     57  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
     58    int bytesPerPixel = bytesPerPixelList[bytesPerId];
     59    int stride = 256 * bytesPerPixel;
     60    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
     61      unsigned char* buffer =
     62          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
     63                         xBoundary[testId], yBoundary[testId]);
     64      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
     65                     stride, xBoundary[testId] * bytesPerPixel,
     66                     yBoundary[testId]);
     67 
     68      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
     69                              height[testId], stride));
     70      delete[] buffer;
     71    }
     72  }
     73 }
     74 
     75 TEST(Gfx, BufferUnrotateVertical)
     76 {
     77  const int NUM_OF_TESTS = 8;
     78  int bytesPerPixelList[2] = {2, 4};
     79  int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99};
     80  int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99};
     81  int xBoundary[NUM_OF_TESTS] = {0, 0, 0, 0};
     82  int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31};
     83 
     84  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
     85    int bytesPerPixel = bytesPerPixelList[bytesPerId];
     86    int stride = 256 * bytesPerPixel;
     87    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
     88      unsigned char* buffer =
     89          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
     90                         xBoundary[testId], yBoundary[testId]);
     91      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
     92                     stride, xBoundary[testId] * bytesPerPixel,
     93                     yBoundary[testId]);
     94 
     95      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
     96                              height[testId], stride));
     97      delete[] buffer;
     98    }
     99  }
    100 }
    101 
    102 TEST(Gfx, BufferUnrotateBoth)
    103 {
    104  const int NUM_OF_TESTS = 16;
    105  int bytesPerPixelList[2] = {2, 4};
    106  int width[NUM_OF_TESTS] = {100, 100, 99, 99, 100, 100, 99, 99,
    107                             100, 100, 99, 99, 100, 100, 99, 99};
    108  int height[NUM_OF_TESTS] = {100, 99, 100, 99, 100, 99, 100, 99,
    109                              100, 99, 100, 99, 100, 99, 100, 99};
    110  int xBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 31, 31, 31, 31,
    111                                 30, 30, 30, 30, 31, 31, 31, 31};
    112  int yBoundary[NUM_OF_TESTS] = {30, 30, 30, 30, 30, 30, 30, 30,
    113                                 31, 31, 31, 31, 31, 31, 31, 31};
    114 
    115  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
    116    int bytesPerPixel = bytesPerPixelList[bytesPerId];
    117    int stride = 256 * bytesPerPixel;
    118    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
    119      unsigned char* buffer =
    120          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
    121                         xBoundary[testId], yBoundary[testId]);
    122      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
    123                     stride, xBoundary[testId] * bytesPerPixel,
    124                     yBoundary[testId]);
    125 
    126      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
    127                              height[testId], stride));
    128      delete[] buffer;
    129    }
    130  }
    131 }
    132 
    133 TEST(Gfx, BufferUnrotateUneven)
    134 {
    135  const int NUM_OF_TESTS = 16;
    136  int bytesPerPixelList[2] = {2, 4};
    137  int width[NUM_OF_TESTS] = {10,  100, 99, 39, 100, 40, 99, 39,
    138                             100, 50,  39, 99, 74,  60, 99, 39};
    139  int height[NUM_OF_TESTS] = {100, 39, 10,  99, 10, 99, 40, 99,
    140                              73,  39, 100, 39, 67, 99, 84, 99};
    141  int xBoundary[NUM_OF_TESTS] = {0,  0,  30, 30, 99, 31, 0,  31,
    142                                 30, 30, 30, 30, 31, 31, 31, 38};
    143  int yBoundary[NUM_OF_TESTS] = {30, 30, 0,  30, 0,  30, 0,  30,
    144                                 31, 31, 31, 31, 31, 31, 31, 98};
    145 
    146  for (int bytesPerId = 0; bytesPerId < 2; bytesPerId++) {
    147    int bytesPerPixel = bytesPerPixelList[bytesPerId];
    148    int stride = 256 * bytesPerPixel;
    149    for (int testId = 0; testId < NUM_OF_TESTS; testId++) {
    150      unsigned char* buffer =
    151          GenerateBuffer(bytesPerPixel, width[testId], height[testId], stride,
    152                         xBoundary[testId], yBoundary[testId]);
    153      BufferUnrotate(buffer, width[testId] * bytesPerPixel, height[testId],
    154                     stride, xBoundary[testId] * bytesPerPixel,
    155                     yBoundary[testId]);
    156 
    157      EXPECT_TRUE(CheckBuffer(buffer, bytesPerPixel, width[testId],
    158                              height[testId], stride));
    159      delete[] buffer;
    160    }
    161  }
    162 }