15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A test for ZLib's checksum function.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "rlz/lib/crc32.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Crc32Unittest, ByteTest) {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const char* data;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int len;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Externally calculated at http://crc32-checksum.waraxe.us/
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int crc;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } kData[] = {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"Hello"           ,  5, 0xF7D18982},
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"Google"          ,  6, 0x62B0F067},
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {""                ,  0, 0x0},
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"One more string.", 16, 0x0CA14970},
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {NULL              ,  0, 0x0},
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = 0; kData[i].data; i++)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(kData[i].crc,
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        rlz_lib::Crc32(reinterpret_cast<const unsigned char*>(kData[i].data),
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       kData[i].len));
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST(Crc32Unittest, CharTest) {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const char* data;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Externally calculated at http://crc32-checksum.waraxe.us/
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int crc;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } kData[] = {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"Hello"           , 0xF7D18982},
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"Google"          , 0x62B0F067},
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {""                , 0x0},
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"One more string.", 0x0CA14970},
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {"Google\r\n"      , 0x83A3E860},
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    {NULL              , 0x0},
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int crc;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (int i = 0; kData[i].data; i++) {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(rlz_lib::Crc32(kData[i].data, &crc));
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(kData[i].crc, crc);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
53