/************************************************************************** v m C r e a t e ** Creates a virtual machine either from scratch (if pVM is NULL on entry) ** or by resizing and reinitializing an existing VM to the specified stack ** sizes. **************************************************************************/ FICL_VM *vmCreate(FICL_VM *pVM, unsigned nPStack, unsigned nRStack) { if (pVM == NULL) { pVM = (FICL_VM *)ficlMalloc(sizeof (FICL_VM)); assert (pVM); memset(pVM, 0, sizeof (FICL_VM)); } if (pVM->pStack) stackDelete(pVM->pStack); pVM->pStack = stackCreate(nPStack); if (pVM->rStack) stackDelete(pVM->rStack); pVM->rStack = stackCreate(nRStack); #if FICL_WANT_FLOAT if (pVM->fStack) stackDelete(pVM->fStack); pVM->fStack = stackCreate(nPStack); #endif pVM->textOut = ficlTextOut; vmReset(pVM); return pVM; }
void test1( int* pfailed) { stack_t myStack; char* scenario; int ret; *pfailed = 0; ret = stackCreate( NULL, 0 ); scenario = "creating an empty stack must fail\n"; checkResult( 0, (ret == -1), pfailed, scenario ); ret = stackCreate( &myStack, 0 ); scenario = "creating a stack of no length must fail\n"; checkResult( 1, (ret == -1), pfailed, scenario ); ret = stackCreate( &myStack, 65536 ); scenario = "creating a very big stack must fail\n"; checkResult( 2, (ret == -1), pfailed, scenario ); ret = stackCreate( &myStack, 1024 ); scenario = "creating the max-size stack must succeed\n"; checkResult( 3, (ret == 0), pfailed, scenario ); scenario = "status of the newly-created stack\n"; checkResult( 4, (myStack.state == STACK_CREATED), pfailed, scenario ); scenario = "new stack's index must be at the beginning\n"; checkResult( 5, (myStack.index == 0), pfailed, scenario ); scenario = "top element OK\n"; checkResult( 6, (myStack.max == 1024), pfailed, scenario ); scenario = "stack is non-empty\n"; checkResult( 7, (myStack.storage != (int *)0), pfailed, scenario ); ret = stackDestroy( NULL ); scenario = "destroying an empty stack must fail\n"; checkResult( 8, (ret == -1), pfailed, scenario ); ret = stackDestroy( &myStack ); scenario = "destroying the non-empty stack should be OK\n"; checkResult( 9, (ret == 0), pfailed, scenario ); scenario = "The destroyed stack should be in a non-created state\n"; checkResult( 10, (myStack.state != STACK_CREATED), pfailed, scenario ); if (*pfailed == 0) printf( "test1 passed.\n"); /* all 10 test cases succeeded ? */ else printf("test1 failed\n"); }
queue_t * queueCreate(int slots){ queue_t * queue=malloc(sizeof(queue_t)); STACK stack1=stackCreate(slots); STACK stack2=stackCreate(slots); (queue->s1)=stack1; (queue->s2)=stack2; return queue; }
void __CUT_BRINGUP__Explode(void) { int ret; printf("Stack test bringup called\n"); ret = stackCreate(&myStack_1, 5); ASSERT((ret == 0), "Stack 1 creation."); ret = stackCreate(&myStack_2, 5); ASSERT((ret == 0), "Stack 2 creation."); }
void main(void) { int k; Queue* q = qCreate(10); for (int i = 0; i < 16; ++i) { qEnqueue(q, i); } while (!qEmpty(q)) { printf("%d ", qDequeue(q)); printf(" (size left = %d)\n", qSize(q)); } for (int i = 0; i < 16; ++i) { qEnqueue(q, i); printf("(size = %d)\n", qSize(q)); } Stack s; stackCreate(&s, 128); for (int i = 0; i < 255; ++i) { stackPush(&s, i); } while (!stackEmpty(&s)) { printf("%d ", stackTop(&s)); stackPop(&s); } }
void RBDestroy(RBTree* tree){ Stack* stack = (Stack*)malloc(sizeof(Stack)); stackCreate(stack, 16); RBNode* current = tree->nil; do{ if(!stackEmpty(stack)){ current = stackTop(stack); stackPop(stack); }else{ current = tree->root; } if(current->right != tree->nil){ stackPush(stack, current->right); } if(current->left != tree->nil){ stackPush(stack, current->left); } free(current); }while(!stackEmpty(stack)); stackDestroy(stack); free(stack); free(tree->root); free(tree->nil); free(tree); }
void test_flush_stack_empty() { stack *stack = stackCreate(); stack = stackFlush(stack); CU_ASSERT_EQUAL(stack -> size, 0); }
void test_pop_stack_empty() { stack *stack = stackCreate(); int *poppedValue = (int*)stackPop(stack); CU_ASSERT_EQUAL(stack -> size, 0); CU_ASSERT_PTR_NULL(poppedValue); }
void test_peek_stack_empty() { stack *stack = stackCreate(); int *position1 = stackPeek(stack); CU_ASSERT_EQUAL(stack -> size, 0); CU_ASSERT_PTR_NULL(position1); }
char * Reverse(char * array){ int i; size_t len = strlen(array); STACK Stack1=(STACK)stackCreate(100); for(i = 0; i <len; i++) stackPush(Stack1,array[i]); for(i = 0; i <len; i++) stackPop(Stack1,&(array[i])); return array; }
/* * RUN THE COLLECTOR * args * Stack* s -- local variable stack * heap** h -- current (pointer to pointer to) from-space * int bd -- are we collecting BigData heap? (bool: 0 false, otherwise true) */ void collect(Stack* s, heap** h, int bd){ heap* to = heapCreate(); weakptrs = stackCreate(); // If stack is empty, return empty heap if(!s){ *h = to; return; } //evacuate things pointed to from the stack, update stack references do{ s->data = evac(s->data, *h, to); }while(s=s->next); // if collecting bigdata heap, no need to scavenge if(bd){ *h = to; return; } //scavenge the to-space scavenge(*h,to); /* * Check weak pointer references. If data is already forwarded, we know * it's got another reference -- copy the forwarding pointer loc. * Otherwise, we collect the data. */ int i; while((i = pop(&weakptrs)) != -1){ if((*h)->heap[to->heap[i+1]] == FWD) to->heap[i+1] = (*h)->heap[to->heap[i+1]+1]; else to->heap[i+1] = -1; } //Change reference of from-space to to-space free(*h); *h = to; //collect big data heap if(!(collectioncount % BIGDATACOLLECT)){ if(!bigdataindex) return; collect(bigdataindex, &bigdataheap, 1); Stack* bdi = bigdataindex; do{ (*h)->heap[bdi->bdloc] = bdi->data; }while(bdi=bdi->next); } }
int main(void){ int i; struct node *top; struct node *top2; //create two stack stackCreate(&top); stackCreate(&top2); //push data to stack stackPush(&top,3); stackPush(&top,5); stackPush(&top,7); stackPush(&top,9); for(i=0;i<4;i++) printf("%d\n",stackPop(&top,&top2)); //to pop top element return 0; }
void test_push_stack_empty() { stack *stack = stackCreate(); int pageNumber = 394; stackPush(stack, &pageNumber); int *tail = (int*)stack -> array -> array[0]; CU_ASSERT_EQUAL(stack -> size, 1); CU_ASSERT_PTR_EQUAL(tail, &pageNumber); }
StackElement *stackRemove(Stack *stack){ if (stack->head == NULL){ stack = stackCreate(); stack->length --; } else{ stack->head = stack->head->next; stack->length --; if (stack->head == NULL){ stack->tail = NULL; stack->length = 0; } } }
int main() { int failed = 0; test1(&failed); stack_t anotherstack; int stackElem; int result = stackCreate( &anotherstack, 8 ); if(result == 0) for(int i=10; i < 50; i+=10) stackPush(&anotherstack, i); while( stackPop(&anotherstack, &stackElem) == 0 ){ printf(" Poped out element : %d \n",stackElem); } return 0; }
STACK queueToStack(queue_t * queue){ STACK stack=stackCreate(queue->s1->cap); int i=0; int in=(queue->s1->index)-1; DATA_TYPE data[queue->s1->cap]; while(dequeue(queue,&data[i])){ i++; } while(stackPush(stack,data[in])){ in--; if(in==-1) break; } return stack; }
void test_peek_stack_filled() { stack *stack = stackCreate(); int value1 = 3; int value2 = 2; int value3 = 1; stack = stackPush(stack, &value1); stack = stackPush(stack, &value2); stack = stackPush(stack, &value3); int *position3 = stackPeek(stack); CU_ASSERT_EQUAL(stack -> size, 3); CU_ASSERT_PTR_EQUAL(position3, &value3); }
void test_pop_stack_filled() { stack *stack = stackCreate(); int value1 = 3; int value2 = 2; int value3 = 1; stack = stackPush(stack, &value1); stack = stackPush(stack, &value2); stack = stackPush(stack, &value3); CU_ASSERT_EQUAL(stack -> size, 3); CU_ASSERT_PTR_EQUAL((int*)stackPop(stack), &value3); CU_ASSERT_PTR_EQUAL(stackPop(stack), &value2); CU_ASSERT_PTR_EQUAL(stackPop(stack), &value1); // Now that we've popped off all values, we need to see if the size has been // updated correctly CU_ASSERT_EQUAL(stack -> size, 0); }
void test_flush_stack_filled() { stack *stack = stackCreate(); int value1 = 1; int value2 = 2; int value3 = 3; stack = stackPush(stack, &value1); stack = stackPush(stack, &value2); stack = stackPush(stack, &value3); stack = stackFlush(stack); int *position1 = (int*)stackPop(stack); CU_ASSERT_PTR_NOT_NULL(stack); CU_ASSERT_EQUAL(stack -> size, 0); CU_ASSERT_PTR_NULL(position1); }
void test_push_stack_filled() { stack *stack = stackCreate(); int value1 = 3; int value2 = 2; int value3 = 1; stack = stackPush(stack, &value1); stack = stackPush(stack, &value2); stack = stackPush(stack, &value3); CU_ASSERT_EQUAL(stack -> size, 3); int *position1 = (int*) stack -> array -> array[2]; int *position2 = (int*) stack -> array -> array[1]; int *position3 = (int*) stack -> array -> array[0]; CU_ASSERT_PTR_EQUAL(position1, &value3); CU_ASSERT_EQUAL(position2, &value2); CU_ASSERT_EQUAL(position3, &value1); }
int* preorderTraversal(struct TreeNode* root, int* returnSize) { *returnSize = 0; if(NULL == root){ return NULL; } Stack* stack = (Stack*)malloc(sizeof(Stack)); stackCreate(stack, 16); int* result = (int*)malloc(sizeof(int)*16); struct TreeNode* current = NULL; do{ if(!stackEmpty(stack)){ current = stackTop(stack); stackPop(stack); }else{ current = root; } result[*returnSize] = current->val; *returnSize = *returnSize + 1; if(*returnSize > 16){ result = (int*)realloc(result, sizeof(int)*(*returnSize+16)); } if(current->right){ stackPush(stack, current->right); } if(current->left){ stackPush(stack, current->left); } }while(!stackEmpty(stack)); stackDestroy(stack); free(stack); return result; }
Test* test1(){ Test* t = malloc(sizeof(Test)); t->s = stackCreate(); push(&(t->s),24); push(&(t->s),13); push(&(t->s),0); collectioncount = 2; int data[] = { 2,4,28 }; int bigdata[] = { 3,1,19,21,6 }; int range[] = { 0,32 }; int lambda[] = { 8,1,6 }; int phantom[] = { 4,0 }; int* soft = malloc(sizeof(int)); *soft = 0; int* weak = malloc(sizeof(int)); *weak = 2; //build test heap t->h = initHeaps(); heap* h = t->h; heapAdd(h,INT,inn); heapAdd(h,BOOL,boo); heapAdd(h,WEAK,weak); heapAdd(h,STRING,str); heapAdd(h,BIGDATA,bigdata); heapAdd(h,LAMBDA,lambda); heapAdd(h,INT,inn); heapAdd(h,RANGE,range); heapAdd(h,DATA,data); heapAdd(h,SOFT,soft); heapAdd(h,BIGDATA,bigdata); heapAdd(h,PHANTOM,phantom); return t; }
int main(int argc, char *argv[]){ char path[20]; char buf[0]; char flake; int fd; int n; stackNode *top; stackCreate(&top); if(!argv[1]) strcpy(path,"data"); else strcpy(path,argv[1]); fd = open(path, O_RDONLY, S_IRWXU); while((n = read(fd, buf, BUFFER_SIZE)) > 0){ flake = buf[0]; stackPush(&top,flake); } //stackDisplay(&top); parse(&top); // if(write(1, buf, n) != n ) // printf("Write Error"); if(n < 0) printf("Read Error \n"); return 0; }
#include "../Catch/include/catch.hpp" #include "stack.h" #include "Macro.h" TEST_CASE("Stack test", "[stack]") { typedef long type_for_test_t; type_for_test_t testElements[] = { -1, 30, 60, 120, -50 }; // Test "create" auto stack = stackCreate(sizeof(type_for_test_t)); REQUIRE(stack); // Test "push" for (auto &testElement : testElements) { bool status = false; CHECK_NOTHROW(status = stackPush(stack, &testElement)); CHECK(status); } // Check "size" CHECK(stackSize(stack) == STATIC_ARRAY_ITEMS_COUNT(testElements)); // Test "pop" and "top" for (int i = STATIC_ARRAY_ITEMS_COUNT(testElements) - 1; i >= 0; -- i) { CHECK(*static_cast<type_for_test_t *>(stackTop(stack)) == testElements[i]); bool status = false; CHECK_NOTHROW(status = stackPop(stack)); CHECK(status); }
int main(void) { int action; char expr[255]; Node *root = NULL, *root2 = NULL; Stack stPost; while (1) { printf("Меню:\n"); printf("1) Ввести выражение\n"); printf("2) Печать исходного выражения\n"); printf("3) Печать преобразованного выражения\n"); printf("4) Печать исходного дерева\n"); printf("5) Печать преобразованного дерева\n"); printf("6) Выход\n"); printf("Выберите действие: "); scanf("%d", &action); switch (action) { case 1: { printf("Введите выражение: "); scanf("%s", expr); treeDestroy(&root); treeDestroy(&root2); stackCreate(&stPost); postOrder(expr, &stPost); treeBuild(&root, &stPost); stackDestroy(&stPost); root2 = treeCopy(&root); treeMoveMinus(&root2); break; } case 2: { printf("Исходное выражение: %s\n", expr); break; } case 3: { LKP(&root2); printf("\n"); break; } case 4: { if (root != NULL) { printf("Дерево исходного выражения\n"); PKL(&root, 0); } else printf("Дерево исходного выражения пусто\n"); break; } case 5: { if (root2 != NULL) { printf("Дерево преобразованного выражения\n"); PKL(&root2, 0); } else printf("Дерево преобразованного выражения пусто\n"); break; } case 6: break; default: { printf("Ошибка. Такого пункта меню не существует\n"); break; } } if (action == 6) break; } treeDestroy(&root); treeDestroy(&root2); return 0; }
void postOrder(const char *str, Stack *st) { int i = 0, isBracket = 0; char tmpCh; Token tk; Stack stOp; stackCreate(&stOp); tk._varOp = '\0'; tk._num = 0; while (str[i] != '\0') { if (isLetter(str[i])) { tk._varOp = str[i]; stackPush(st, tk); } else if (isNumber(str[i])) { tk._varOp = '\0'; tk._num = tk._num * 10 + str[i] - '0'; if (str[i + 1] != '.' && !isNumber(str[i + 1])) { stackPush(st, tk); tk._num = 0; } } else if (isOp(str[i])) { tk._varOp = str[i]; if (str[i] == ')') isBracket = 1; else if (str[i] == '-' && (i == 0 || str[i - 1] == '(')) { tmpCh = tk._varOp; tk._varOp = '\0'; tk._num = 0; stackPush(st, tk); tk._varOp = tmpCh; } while (!stackEmpty(&stOp) && (isOpHigh(stackTop(&stOp)._varOp, str[i]) || isBracket)) { if (stackTop(&stOp)._varOp == '(') isBracket = 0; else stackPush(st, stackTop(&stOp)); stackPop(&stOp); } if (str[i] != ')') stackPush(&stOp, tk); } i++; } while (!stackEmpty(&stOp)) { stackPush(st, stackTop(&stOp)); stackPop(&stOp); } stackDestroy(&stOp); }
/* * Create heaps */ heap* initHeaps(){ dataConsCreate(); bigdataHeapCreate(); bigdataindex = stackCreate(); return heapCreate(); }
void test_create_stack() { stack *stack = stackCreate(); CU_ASSERT_PTR_NOT_NULL(stack); }
Test* createTest(){ Test* t = malloc(sizeof(Test)); t->h = initHeaps(); t->s = stackCreate(); return t; }
int dcommand(int argc, char * argv[]){ struct stack * log = stackCreate(); #ifdef DEBUG printf("dcommand\n"); #endif // Get archive name from command line char * arkName = argv[2]; // File descriptor for existing archive int arkiv; // Make sure 'open' is working if(!strcmp(arkName, "-")){ // If archive name is '-' then read from std_in arkiv = STDIN_FILENO; } else if ((arkiv = open(arkName, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1){ snprintf(errDesc, ERR_STATUS_LENGTH, "opening initial archive %s", arkName); return -1; } /* Make new archive (temporary) to copy to * ... if it succeeds, we will keep temp archive * else we will unlink it. */ int newArkiv; char tmpName [PATH_MAX]; if(!strcmp(arkName, "-")){ newArkiv = STDOUT_FILENO; }else{ snprintf(tmpName, PATH_MAX, "%sXXXXXX", arkName); if ((newArkiv = mkstemp(tmpName)) == -1){ snprintf(errDesc, ERR_STATUS_LENGTH, "making new tmp archive with name %s", tmpName); return -1; } } // Variable to store filesize of current archive off_t fileSize; // Read through archive 'arkiv' // For each file, filesize is stored, then file's name, terminated by null char #ifdef DEBUG printf(" scanning through archive\n"); #endif dreadloop: while(read(arkiv, &fileSize, sizeof(off_t)) > 0){ // File's name char nameBuffer[PATH_MAX]; // Read into nameBuffer until null char int posInNameBuff = 0; while(read(arkiv, nameBuffer + posInNameBuff, 1) == 1 && nameBuffer[posInNameBuff++] != '\0'); // Do we have a valid file? if(strlen(nameBuffer) < 1){ // Abort mission. Roll back. snprintf(errDesc, ERR_STATUS_LENGTH, "reading file '%s' with name less than one char from archive %s", nameBuffer, arkName); return -1; } // Check if this file is in command line for(int i = 3; i < argc; i++){ // If 'zoo' is in command line, also check for 'zoo/' char asDirectoryName[PATH_MAX]; strcpy(asDirectoryName, argv[i]); if(asDirectoryName[strlen(asDirectoryName) - 1] != '/') strcat(asDirectoryName, "/"); if(!strcmp(nameBuffer, argv[i]) || !strcmp(nameBuffer, asDirectoryName) || checkIfParentDirectory(asDirectoryName, nameBuffer)){ #ifdef DEBUG printf(" it is listed in command line args, so skip it\n"); #endif // Skip ahead to next file if(lseek(arkiv, fileSize, SEEK_CUR) == -1){ // Couldn't skip ahead in file. Rollback. return -1; } stackAddFile(log, nameBuffer); goto dreadloop; } } // Not listed - so copy to new file // Add file size and name to archive if(archiveFileSizeAndName(nameBuffer, newArkiv, fileSize) == -1){ return -1; } // Add rest of file if(copyBytes(nameBuffer, arkiv, newArkiv, fileSize) == -1){ return -1; } } // Make sure that all the arg commands were deleted for (int i = 3; i < argc; i++){ if(!stackContainsChild(log, argv[i])){ fprintf(stderr, "Far: cannot find %s\n", argv[i]); } } // Done! Destory stack and switch files stackDestroy(log); char tmpBackupName[PATH_MAX]; snprintf(tmpBackupName, PATH_MAX, "%s.bak", arkName); if(arkiv != STDIN_FILENO && rename(arkName, tmpBackupName) == -1){ // Can't rename first file with .bak. Rollback. snprintf(errDesc, ERR_STATUS_LENGTH, "first step switching archives, renaming %s to %s", arkName, tmpBackupName); unlink(tmpName); return -1; } if(arkiv != STDIN_FILENO && rename(tmpName, arkName) == -1){ // Can't make tmpfile permanent. Rollback. snprintf(errDesc, ERR_STATUS_LENGTH, "second step switching archives, renaming %s to %s", tmpName, arkName); rename(tmpBackupName, arkName); if(arkiv != STDIN_FILENO) unlink(tmpName); return -1; } if(arkiv != STDIN_FILENO && close(arkiv) == -1){ // Can't close original archive. snprintf(errDesc, ERR_STATUS_LENGTH, "closing old archive file %s", arkName); return -1; } if(arkiv != STDIN_FILENO && close(newArkiv) == -1){ // Can't close original archive. snprintf(errDesc, ERR_STATUS_LENGTH, "closing archive file %s", tmpName); return -1; } if(arkiv != STDIN_FILENO && unlink(tmpBackupName) == -1){ snprintf(errDesc, ERR_STATUS_LENGTH, "unlinking original archive, renamed %s", tmpBackupName); } return 0; }