image/test/gtest/TestStreamingLexer.cpp
author Nicholas Nethercote <nnethercote@mozilla.com>
Wed, 28 Oct 2015 01:30:20 -0700
changeset 307033 793133ff5233a8e2cd0a6864b9984f5a9bb31c05
parent 295911 ca467297fa07c6ae3ac42eab2b5f38f2a33152cc
child 315174 2ebafa1c0eee604355b6acbc37270c668f6889f1
permissions -rw-r--r--
Bug 1210291 - Streamline StreamingLexer's handling of terminal states. r=seth. This patch introduces TerminalState and changes LexerTransition::mNextState to be a Variant<State, TerminalState>. This means that SUCCESS and FAILURE no longer need to be part of State. Some things to note: - This simplifies the handling of Lex()'s return value, which is nice. - The patch splits Terminate() into TerminateSuccess() and TerminateFailure(). - |const State& aNextState| wouldn't work for the first arg to LexerTransition's ctor due to errors in Variant construction that I didn't understand. I had to change it to |State aNextState|.

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "mozilla/Vector.h"
#include "StreamingLexer.h"

using namespace mozilla;
using namespace mozilla::image;

enum class TestState
{
  ONE,
  TWO,
  THREE,
  UNBUFFERED
};

void
CheckData(const char* aData, size_t aLength)
{
  EXPECT_TRUE(aLength == 3);
  EXPECT_EQ(1, aData[0]);
  EXPECT_EQ(2, aData[1]);
  EXPECT_EQ(3, aData[2]);
}

LexerTransition<TestState>
DoLex(TestState aState, const char* aData, size_t aLength)
{
  switch (aState) {
    case TestState::ONE:
      CheckData(aData, aLength);
      return Transition::To(TestState::TWO, 3);
    case TestState::TWO:
      CheckData(aData, aLength);
      return Transition::To(TestState::THREE, 3);
    case TestState::THREE:
      CheckData(aData, aLength);
      return Transition::TerminateSuccess();
    default:
      MOZ_CRASH("Unknown TestState");
  }
}

LexerTransition<TestState>
DoLexWithUnbuffered(TestState aState, const char* aData, size_t aLength,
                    Vector<char>& aUnbufferedVector)
{
  switch (aState) {
    case TestState::ONE:
      CheckData(aData, aLength);
      return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
    case TestState::UNBUFFERED:
      EXPECT_TRUE(aLength <= 3);
      aUnbufferedVector.append(aData, aLength);
      return Transition::ContinueUnbuffered(TestState::UNBUFFERED);
    case TestState::TWO:
      CheckData(aUnbufferedVector.begin(), aUnbufferedVector.length());
      return Transition::To(TestState::THREE, 3);
    case TestState::THREE:
      CheckData(aData, aLength);
      return Transition::TerminateSuccess();
    default:
      MOZ_CRASH("Unknown TestState");
  }
}

LexerTransition<TestState>
DoLexWithUnbufferedTerminate(TestState aState, const char* aData, size_t aLength)
{
  switch (aState) {
    case TestState::ONE:
      CheckData(aData, aLength);
      return Transition::ToUnbuffered(TestState::TWO, TestState::UNBUFFERED, 3);
    case TestState::UNBUFFERED:
      return Transition::TerminateSuccess();
    default:
      MOZ_CRASH("Unknown TestState");
  }
}

TEST(ImageStreamingLexer, SingleChunk)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

  // Test delivering all the data at once.
  Maybe<TerminalState> result = lexer.Lex(data, sizeof(data), DoLex);
  EXPECT_TRUE(result.isSome());
  EXPECT_EQ(Some(TerminalState::SUCCESS), result);
}

TEST(ImageStreamingLexer, SingleChunkWithUnbuffered)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
  Vector<char> unbufferedVector;

  // Test delivering all the data at once.
  Maybe<TerminalState> result =
    lexer.Lex(data, sizeof(data),
              [&](TestState aState, const char* aData, size_t aLength) {
      return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
  });
  EXPECT_TRUE(result.isSome());
  EXPECT_EQ(Some(TerminalState::SUCCESS), result);
}

TEST(ImageStreamingLexer, ChunkPerState)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

  // Test delivering in perfectly-sized chunks, one per state.
  for (unsigned i = 0 ; i < 3 ; ++i) {
    Maybe<TerminalState> result = lexer.Lex(data + 3 * i, 3, DoLex);

    if (i == 2) {
      EXPECT_TRUE(result.isSome());
      EXPECT_EQ(Some(TerminalState::SUCCESS), result);
    } else {
      EXPECT_TRUE(result.isNothing());
    }
  }
}

TEST(ImageStreamingLexer, ChunkPerStateWithUnbuffered)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
  Vector<char> unbufferedVector;

  // Test delivering in perfectly-sized chunks, one per state.
  for (unsigned i = 0 ; i < 3 ; ++i) {
    Maybe<TerminalState> result =
      lexer.Lex(data + 3 * i, 3,
                [&](TestState aState, const char* aData, size_t aLength) {
        return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
    });

    if (i == 2) {
      EXPECT_TRUE(result.isSome());
      EXPECT_EQ(Some(TerminalState::SUCCESS), result);
    } else {
      EXPECT_TRUE(result.isNothing());
    }
  }
}

TEST(ImageStreamingLexer, OneByteChunks)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

  // Test delivering in one byte chunks.
  for (unsigned i = 0 ; i < 9 ; ++i) {
    Maybe<TerminalState> result = lexer.Lex(data + i, 1, DoLex);

    if (i == 8) {
      EXPECT_TRUE(result.isSome());
      EXPECT_EQ(Some(TerminalState::SUCCESS), result);
    } else {
      EXPECT_TRUE(result.isNothing());
    }
  }
}

TEST(ImageStreamingLexer, OneByteChunksWithUnbuffered)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };
  Vector<char> unbufferedVector;

  // Test delivering in one byte chunks.
  for (unsigned i = 0 ; i < 9 ; ++i) {
    Maybe<TerminalState> result =
      lexer.Lex(data + i, 1,
                [&](TestState aState, const char* aData, size_t aLength) {
        return DoLexWithUnbuffered(aState, aData, aLength, unbufferedVector);
    });

    if (i == 8) {
      EXPECT_TRUE(result.isSome());
      EXPECT_EQ(Some(TerminalState::SUCCESS), result);
    } else {
      EXPECT_TRUE(result.isNothing());
    }
  }
}

TEST(ImageStreamingLexer, TerminateSuccess)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

  // Test that Terminate is "sticky".
  Maybe<TerminalState> result =
    lexer.Lex(data, sizeof(data),
              [&](TestState aState, const char* aData, size_t aLength) {
      EXPECT_TRUE(aState == TestState::ONE);
      return Transition::TerminateSuccess();
  });
  EXPECT_TRUE(result.isSome());
  EXPECT_EQ(Some(TerminalState::SUCCESS), result);

  result =
    lexer.Lex(data, sizeof(data),
              [&](TestState aState, const char* aData, size_t aLength) {
      EXPECT_TRUE(false);  // Shouldn't get here.
      return Transition::TerminateFailure();
  });
  EXPECT_TRUE(result.isSome());
  EXPECT_EQ(Some(TerminalState::SUCCESS), result);
}

TEST(ImageStreamingLexer, TerminateFailure)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

  // Test that Terminate is "sticky".
  Maybe<TerminalState> result =
    lexer.Lex(data, sizeof(data),
              [&](TestState aState, const char* aData, size_t aLength) {
      EXPECT_TRUE(aState == TestState::ONE);
      return Transition::TerminateFailure();
  });
  EXPECT_TRUE(result.isSome());
  EXPECT_EQ(Some(TerminalState::FAILURE), result);

  result =
    lexer.Lex(data, sizeof(data),
              [&](TestState aState, const char* aData, size_t aLength) {
      EXPECT_TRUE(false);  // Shouldn't get here.
      return Transition::TerminateFailure();
  });
  EXPECT_TRUE(result.isSome());
  EXPECT_EQ(Some(TerminalState::FAILURE), result);
}

TEST(ImageStreamingLexer, TerminateUnbuffered)
{
  StreamingLexer<TestState> lexer(Transition::To(TestState::ONE, 3));
  char data[9] = { 1, 2, 3, 1, 2, 3, 1, 2, 3 };

  // Test that Terminate works during an unbuffered read.
  for (unsigned i = 0 ; i < 9 ; ++i) {
    Maybe<TerminalState> result =
      lexer.Lex(data + i, 1, DoLexWithUnbufferedTerminate);

    if (i > 2) {
      EXPECT_TRUE(result.isSome());
      EXPECT_EQ(Some(TerminalState::SUCCESS), result);
    } else {
      EXPECT_TRUE(result.isNothing());
    }
  }
}