Skip to content

Commit

Permalink
Fix const-correctness issues
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
ziggi committed Oct 1, 2018
1 parent 8e67f75 commit 44a235a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
10 changes: 5 additions & 5 deletions foreach.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ Notes:
--------------------------------------------------------------------------------
*/

stock Iter_RandomInternal(count, array[], size)
stock Iter_RandomInternal(count, const array[], size)
{
if (count == 0) {
return ITER_NONE;
Expand Down Expand Up @@ -1472,7 +1472,7 @@ Notes:
--------------------------------------------------------------------------------
*/

stock Iter_FreeInternal(array[], size)
stock Iter_FreeInternal(const array[], size)
{
for (new i = 0; i != size; ++i) {
if (array[i] > size) {
Expand Down Expand Up @@ -1593,7 +1593,7 @@ Notes:
--------------------------------------------------------------------------------
*/

stock Iter_ContainsInternal(array[], value, size)
stock Iter_ContainsInternal(const array[], value, size)
{
return 0 <= value < size && array[value] <= size;
}
Expand Down Expand Up @@ -1636,7 +1636,7 @@ stock Iter_ClearInternal(&count, array[], size)
* </remarks>
*//*------------------------------------------------------------------------**/

stock Iter_IndexInternal(count, array[], start, size, index)
stock Iter_IndexInternal(count, const array[], start, size, index)
{
// If there are no elements in the iterator, we can't ever return the Nth
// item. Also if the parameters are invalid.
Expand Down Expand Up @@ -1696,7 +1696,7 @@ Notes:
--------------------------------------------------------------------------------
*/

stock Iter_PrevInternal(array[], size, slot)
stock Iter_PrevInternal(const array[], size, slot)
{
if (0 <= slot <= size && array[slot] <= size) {
for (new last = slot; last--; ) {
Expand Down
49 changes: 48 additions & 1 deletion test.pwn
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
// generated by "sampctl package generate"

#include <a_samp>
#include "foreach.inc"

#define MAX_TEAMS 5

new
gPlayerScore[MAX_PLAYERS] = {10, ...},
Iterator:Team[MAX_TEAMS]<MAX_PLAYERS>;

stock Team_SetPlayerTeam(playerid, teamid)
{
new current_team = GetPlayerTeam(playerid);
if (current_team != NO_TEAM && current_team != -1) {
Iter_Remove(Team[current_team], playerid);
}

if (teamid != NO_TEAM) {
Iter_Add(Team[teamid], playerid);
}

return SetPlayerTeam(playerid, teamid);
}
#if defined _ALS_SetPlayerTeam
#undef SetPlayerTeam
#else
#define _ALS_SetPlayerTeam
#endif

#define SetPlayerTeam Team_SetPlayerTeam

main() {
// write tests for libraries here and run "sampctl package run"
Iter_Init(Team);

SetPlayerTeam(0, 1);
SetPlayerTeam(1, 1);
SetPlayerTeam(2, 2);
SetPlayerTeam(3, 3);

printf("Iter_Index(Team[1], 1) = %d", Iter_Index(Team[1], 1));
printf("Iter_Last(Team[1]) = %d", Iter_Last(Team[1]));

new scores[MAX_TEAMS];
for (new teamid; teamid < MAX_TEAMS; teamid++) {
foreach (new playerid : Team[teamid]) {
scores[teamid] += gPlayerScore[playerid];
}
}

for (new teamid; teamid < MAX_TEAMS; teamid++) {
printf("Team %d: %d", teamid, scores[teamid]);
}
}

0 comments on commit 44a235a

Please sign in to comment.