Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/include/mafianet/variable_list_delta_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class VariableListDeltaTracker
variableList[nextWriteIndex].lastData=(char*) rakRealloc_Ex(variableList[nextWriteIndex].lastData, temp.GetNumberOfBytesUsed(),_FILE_AND_LINE_);
variableList[nextWriteIndex].byteLength=temp.GetNumberOfBytesUsed();
memcpy(variableList[nextWriteIndex].lastData,temp.GetData(),temp.GetNumberOfBytesUsed());
nextWriteIndex++;
variableList[nextWriteIndex].isDirty=false;
nextWriteIndex++;
return true; // Different because the serialized size is different
}
if (variableList[nextWriteIndex].isDirty==false && memcmp(temp.GetData(),variableList[nextWriteIndex].lastData, variableList[nextWriteIndex].byteLength)==0)
Expand Down
77 changes: 77 additions & 0 deletions Tests/Unit/VariableListDeltaTrackerTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2026, MafiaHub
*
* This source code is licensed under the MIT-style license found in the
* license.txt file in the root directory of this source tree.
*/

#include <gtest/gtest.h>

#include "mafianet/variable_list_delta_tracker.h"
#include "mafianet/string.h"

using namespace MafiaNet;

namespace
{
unsigned char DirtyBit(unsigned int index)
{
return static_cast<unsigned char>(0x80 >> (index & 7));
}
}

TEST(VariableListDeltaTracker, UnchangedValueIsNotWrittenAgain)
{
VariableListDeltaTracker tracker;
tracker.StartWrite();
EXPECT_TRUE(tracker.WriteVar(RakString("a")));
tracker.StartWrite();
EXPECT_FALSE(tracker.WriteVar(RakString("a")));
}

TEST(VariableListDeltaTracker, ResizedValueKeepsTheNextVariableDirty)
{
VariableListDeltaTracker tracker;
tracker.StartWrite();
tracker.WriteVar(RakString("a"));
tracker.WriteVar(5);

unsigned char dirty[1] = { DirtyBit(1) };
tracker.FlagDirtyFromBitArray(dirty);

tracker.StartWrite();
EXPECT_TRUE(tracker.WriteVar(RakString("abc")));
EXPECT_TRUE(tracker.WriteVar(5));
}

TEST(VariableListDeltaTracker, ResizedValueClearsItsOwnDirtyFlag)
{
VariableListDeltaTracker tracker;
tracker.StartWrite();
tracker.WriteVar(RakString("a"));
tracker.WriteVar(5);

unsigned char dirty[1] = { DirtyBit(0) };
tracker.FlagDirtyFromBitArray(dirty);

tracker.StartWrite();
EXPECT_TRUE(tracker.WriteVar(RakString("abc")));
tracker.WriteVar(5);

tracker.StartWrite();
EXPECT_FALSE(tracker.WriteVar(RakString("abc")));
}

TEST(VariableListDeltaTracker, ResizingTheLastVariableStaysInBounds)
{
VariableListDeltaTracker tracker;
tracker.StartWrite();
tracker.WriteVar(RakString("a"));

tracker.StartWrite();
EXPECT_TRUE(tracker.WriteVar(RakString("abc")));
EXPECT_TRUE(tracker.IsPastEndOfList());

tracker.StartWrite();
EXPECT_FALSE(tracker.WriteVar(RakString("abc")));
}
Loading