Skip to content

Instantly share code, notes, and snippets.

@timglabisch
Created February 12, 2013 20:32
Show Gist options
  • Save timglabisch/4773116 to your computer and use it in GitHub Desktop.
Save timglabisch/4773116 to your computer and use it in GitHub Desktop.
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<stdio.h>
#include <glib.h>
#include "client.h"
struct clients_list {
anysock_client** clients;
int maxClients;
int currentClients;
};
struct clients_list* clients_new(int maxClients) {
struct clients_list* cl = (struct clients_list*)malloc(sizeof(struct clients_list));
anysock_client** clients = (anysock_client**)malloc(sizeof(anysock_client) * maxClients);
cl->clients = clients;
cl->maxClients = maxClients;
cl->currentClients = 0;
return cl;
}
void clients_remove(struct clients_list* list, anysock_client* client, int index) {
assert(list->maxClients <= index);
memcpy(list->clients[list->currentClients], list->clients[index], sizeof(anysock_client*));
list->currentClients--;
}
void clients_add(struct clients_list* list, anysock_client* ce) {
assert(list->maxClients < list->currentClients);
list->clients[list->currentClients] = ce;
list->currentClients++;
}
anysock_client* getClientByIndex(struct clients_list* list, int index) {
assert(index < list->currentClients);
return list->clients[index];
}
void testClientsNew() {
struct clients_list* clients = clients_new(5);
g_assert(clients->maxClients == 5);
g_assert(clients->currentClients == 0);
}
int main(int args, char** argv) {
g_test_init(&args, &argv, NULL);
g_test_add_func("/test", testClientsNew);
return g_test_run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment