Skip to content

Instantly share code, notes, and snippets.

@Samuel-IH
Created May 15, 2024 01:54
Show Gist options
  • Save Samuel-IH/15214617e252bca11dd10d2a146e839f to your computer and use it in GitHub Desktop.
Save Samuel-IH/15214617e252bca11dd10d2a146e839f to your computer and use it in GitHub Desktop.
NWScript sort & Unit tests

Incomplete, uncompiled, json sort & unit tests.

// Initializes or continues sorting based on the state; returns TRUE if more sorting is needed
int Sort(json jArray, string sTag)
{
string sLocalVar = "json-array-sort-" + sTag;
json jState = GetLocalJson(OBJECT_SELF, sLocalVar);
if (jState == JsonNull())
{
jState = JsonObject();
JsonObjectSet(jState, "array", jArray);
JsonObjectSet(jState, "currentIndex", JsonInt(0));
JsonObjectSet(jState, "totalLength", JsonInt(JsonGetLength(jArray)));
SetLocalJson(OBJECT_SELF, sLocalVar, jState);
}
int currentIndex = JsonGetInt(JsonObjectGet(jState, "currentIndex"));
if (currentIndex < JsonGetInt(JsonObjectGet(jState, "totalLength")) - 1)
{
return TRUE;
}
return FALSE;
}
// Fetches the first JSON object of the current pair to be compared
json SortGetFirst(string sTag)
{
string sLocalVar = "json-array-sort-" + sTag;
json jState = GetLocalJson(OBJECT_SELF, sLocalVar);
int currentIndex = JsonGetInt(JsonObjectGet(jState, "currentIndex"));
json jArray = JsonObjectGet(jState, "array");
return JsonArrayGet(jArray, currentIndex);
}
// Fetches the second JSON object of the current pair to be compared
json SortGetSecond(string sTag)
{
string sLocalVar = "json-array-sort-" + sTag;
json jState = GetLocalJson(OBJECT_SELF, sLocalVar);
int currentIndex = JsonGetInt(JsonObjectGet(jState, "currentIndex"));
json jArray = JsonObjectGet(jState, "array");
return JsonArrayGet(jArray, currentIndex + 1);
}
// Updates the array based on comparison result, increments index for next pair
void SetSortResult(string sTag, int nResult)
{
string sLocalVar = "json-array-sort-" + sTag;
json jState = GetLocalJson(OBJECT_SELF, sLocalVar);
int currentIndex = JsonGetInt(JsonObjectGet(jState, "currentIndex"));
json jArray = JsonObjectGet(jState, "array");
if (nResult == FALSE)
{
// Swap elements if the order is incorrect
json jTemp = JsonArrayGet(jArray, currentIndex);
JsonArraySet(jArray, currentIndex, JsonArrayGet(jArray, currentIndex + 1));
JsonArraySet(jArray, currentIndex + 1, jTemp);
}
currentIndex++;
JsonObjectSet(jState, "currentIndex", JsonInt(currentIndex));
SetLocalJson(OBJECT_SELF, sLocalVar, jState); // Update state after modification
}
// Retrieves the sorted array after the sorting process is complete
json GetSortResult(string sTag)
{
string sLocalVar = "json-array-sort-" + sTag;
json jState = GetLocalJson(OBJECT_SELF, sLocalVar);
if (jState != JsonNull())
{
json jSortedArray = JsonObjectGet(jState, "array");
DeleteLocalJson(OBJECT_SELF, sLocalVar); // Clean out the variable once done
return jSortedArray;
}
return JsonNull();
}
// Assert function to compare expected and actual results
void AssertEqual(string sExpected, string sActual, string sMessage)
{
if (sExpected != sActual)
{
string sError = "Assertion Failed: " + sMessage + " | Expected: " + sExpected + ", but got: " + sActual;
WriteTimestampedLogEntry(sError);
}
else
{
string sSuccess = "Assertion Passed: " + sMessage;
WriteTimestampedLogEntry(sSuccess);
}
}
// Test function for sorting JSON arrays of integers
void TestSorting(string sJson, string sExpected)
{
string sTag = "localSortTag"; // Use a fixed local tag since tests run sequentially
json jArray = JsonParse(sJson);
while (Sort(jArray, sTag))
{
json jFirst = SortGetFirst(sTag);
json jSecond = SortGetSecond(sTag);
int orderIsCorrect = CompareIntegers(jFirst, jSecond);
SetSortResult(sTag, orderIsCorrect);
}
json jSortedArray = GetSortResult(sTag);
string sSortedJson = JsonDump(jSortedArray, -1); // Dump the sorted JSON without whitespace
AssertEqual(sExpected, sSortedJson, "Sort Test for input: " + sJson);
}
// Main function to run tests
void main()
{
// Test with a properly unsorted array of integers
TestSorting("[4, 1, 3, 2]", "[1, 2, 3, 4]");
// Test with an empty array
TestSorting("[]", "[]");
// Test with a single element
TestSorting("[1]", "[1]");
// Test with an already sorted array
TestSorting("[1, 2, 3, 4]", "[1, 2, 3, 4]");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment