gfx/tests/gtest/TestBufferRotation.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial