コード例 #1
0
ファイル: test.c プロジェクト: albedium/liblist
END_TEST

START_TEST ( llist_09_list_sort )
{
    int retval;
    llist listToTest = NULL;
    listToTest = llist_create ( trivial_comperator , NULL, test_mt ? MT_SUPPORT_FALSE : MT_SUPPORT_TRUE );

    // Insert a 5 nodes 1..5
    retval = llist_add_node ( listToTest, ( llist_node ) 3, ADD_NODE_REAR );
    retval = llist_add_node ( listToTest, ( llist_node ) 2, ADD_NODE_REAR );
    retval = llist_add_node ( listToTest, ( llist_node ) 1, ADD_NODE_REAR );
    retval = llist_add_node ( listToTest, ( llist_node ) 4, ADD_NODE_REAR );
    retval = llist_add_node ( listToTest, ( llist_node ) 5, ADD_NODE_REAR );

    printf ( "List before sorting: " );
    print_llist ( listToTest );

    retval = llist_sort ( listToTest, SORT_LIST_ASCENDING );
    ck_assert_int_eq ( retval, LLIST_SUCCESS );

    printf ( "List After sorting ascending: " );
    print_llist ( listToTest );

    retval = llist_sort ( listToTest,  SORT_LIST_DESCENDING );
    ck_assert_int_eq ( retval, LLIST_SUCCESS );

    printf ( "List After sorting descending: " );
    print_llist ( listToTest );

    llist_destroy ( listToTest, false, NULL );
}
コード例 #2
0
ファイル: mss_timer.c プロジェクト: solokacher/MedUsa
/**************************************************************************//**
*
* timer_start
*
* @brief      start a mss timer
*
* @param[in]  hdl     timer handle
* @param[in]  tick    number of timer ticks to run
*                     (maximum value is ((sizeof(mss_timer_tick_t)/2)-1) )
* @param[in]  reload  number of ticks of reloading/periodic timer. 0 means
*                     that it is a one shot timer (maximum value is 
*                     ((sizeof(mss_timer_tick_t)/2)-1) )
*
* @return     true if success, false if failed
*
******************************************************************************/
static bool timer_start(mss_timer_t hdl, mss_timer_tick_t tick,
		                mss_timer_tick_t reload)
{
  bool ret = false;
  mss_int_flag_t int_flag;

  // check timer handler
  MSS_DEBUG_CHECK(hdl != MSS_TIMER_INVALID_HDL);

  if((!(tick & MSB_TMR_MASK)) && (tick > 0))
  {
    // disable timer interrupt to enable re-setting the timer
    MSS_ENTER_CRITICAL_SECTION(int_flag);

    // set timer to active state and set timer counter
    hdl->expired_tick = mss_timer_tick_cnt + tick;
    hdl->reload_tick = reload;

    if(!(hdl->state & TIMER_ALL_RUNNING_MASK))
    {
      // put the timer into the active timer linked list
      llist_add_first(active_timer_llist, hdl);
    }

    // set new state
    hdl->state = (reload > 0) ? MSS_TIMER_STATE_RUNNING_PERIODIC :
  		                      MSS_TIMER_STATE_RUNNING_ONE_SHOT;

    // sort the active timer list
    llist_sort(active_timer_llist, timer_cmp);

    // return true
    ret = true;

    MSS_LEAVE_CRITICAL_SECTION(int_flag);
  }

  return ret;
}
コード例 #3
0
ファイル: test.c プロジェクト: kissthink/notes
int main(int argc, char *argv[])
{
    LLIST *handle = NULL, *find = NULL;
    int n;

    if (argc > 1)
        handle = llist_load("./db");
    else
    {
        handle = llist_create(sizeof(int));

        while (1)
        {
            printf("please input num : ");
            scanf("%d", &n);

            if (n == -1)
                break;
            /*llist_append(&n, handle);*/
            /*llist_prepend(&n, handle);*/
            /*llist_insert(&n, PREPEND, handle);*/
            llist_sort_insert(&n, cmp, handle);
        }

        llist_travel(ls, NULL, handle);
        printf("\n");

        n = 9999;
        llist_insert(&n, 3, handle);

        if (!llist_store("./db", handle))
            printf("存储成功!\n");
    }

    llist_travel(ls, NULL, handle);
    printf("\n");

    llist_sort(cmp, handle);

    printf("sort : ");
    llist_travel(ls, NULL, handle);
    printf("\n");

    while (1)
    {
        printf("please input key : ");
        scanf("%d", &n);
        if (n == -1)
            break;

        find = llist_findall(&n, cmp, handle);
        if (find != NULL)
        {
            llist_travel_find(ls, NULL, find);
            printf("\n");
        }
/*
 *        printf("delete = %d\n", llist_delete(&n, cmp, handle));
 *
 *        llist_travel(ls, NULL, handle);
 *        printf("\n");
 */
    }

    llist_destroy(&handle);

    return 0;
}
コード例 #4
0
ファイル: mss_timer.c プロジェクト: solokacher/MedUsa
/**************************************************************************//**
*
* mss_timer_tick
*
* @brief      mss timer tick function - this function is to be called
*             periodically by timer ISR function in mss_hal.c
*
* @param      -
*
* @return     if true, there is a task activated, if false no task is activated
*
******************************************************************************/
bool mss_timer_tick(void)
{
  // local timer tick
  static mss_timer_tick_t timer_tick_cnt = 0;
  // flag indicating whether the timer tick function is already running
  static bool timer_tick_running = false;
  struct mss_timer_tbl_t *youngest_tmr;
  bool ret = false, loop;
  mss_int_flag_t int_flag;

  MSS_ENTER_CRITICAL_SECTION(int_flag);

  if(timer_tick_running == true)
  {
	// return false since this function is already running and now called
	// in different ISR context
    MSS_LEAVE_CRITICAL_SECTION(int_flag);
    return false;
  }

  // set flag to indicate timer tick is already running
  timer_tick_running = true;

  // copy hardware timer count tick to local timer tick
  timer_tick_cnt = mss_timer_tick_cnt;

  // loop in case hardware timer tick interrupt occurs between
  // long active timer list processing
  do
  {
	// if local timer tick is different than hardware timer tick,
	// it means that the hardware timer has fired while the timer tick
	// function is still processing the active timer list. Therefore
	// the timer tick function shall update the local timer and continue
	// processing the active timer list
	if(timer_tick_cnt != mss_timer_tick_cnt)
	{
      // increment timer tick
      timer_tick_cnt++;
	}

	do
	{
      // reset loop flag
      loop = false;

      // check for expired timer
      youngest_tmr = llist_touch_first(active_timer_llist);
	  if(youngest_tmr != NULL)
	  {
		if(youngest_tmr->expired_tick == timer_tick_cnt)
		{
		  // wake up task
		  mss_activate_task_int(youngest_tmr->task_id);

          // change timer state by shifting left one bit the state variable
          // which will change from running to expired in both one-shot and
          // periodic mode or from expired periodic to overflow
          youngest_tmr->state <<= 1;

          // remove timer object from active timer list
          llist_get_first(active_timer_llist);

          // if a periodic timer, returns to the active timer list
          if(youngest_tmr->reload_tick > 0)
          {
            // update timer tick first
            youngest_tmr->expired_tick = timer_tick_cnt +
                                         youngest_tmr->reload_tick;

            // put the timer into the active timer linked list
            llist_add_first(active_timer_llist, youngest_tmr);

            // sort the active timer list
            llist_sort(active_timer_llist, timer_cmp);
          }

          // return true
          ret = true;

          // do another loop for checking simultaneous timer ticks
          loop = true;

          // enable interrupt in between the process
          MSS_LEAVE_CRITICAL_SECTION(int_flag);
          MSS_ENTER_CRITICAL_SECTION(int_flag);
        }
      }
    }while(loop == true);
  }while(timer_tick_cnt != mss_timer_tick_cnt);


  // reset flag
  timer_tick_running = false;

  MSS_LEAVE_CRITICAL_SECTION(int_flag);

  return ret;
}
コード例 #5
0
ファイル: test_int.c プロジェクト: kissthink/notes
int main(void)
{
    int n, ret;
    void *ret1;
    LLIST *handle = NULL;
    LLIST *find = NULL;
    
    handle = llist_create(sizeof(int), NULL, NULL, NULL);

    while (1)
    {
        printf("please input num : ");
        scanf("%d", &n);

        if (n == -1)
            break;

        llist_insert(&n, APPEND, handle);
    }
    
    llist_travel(printf_s, NULL, handle);
    printf("\n");

    printf("insert num : ");
    scanf("%d", &n);
    llist_insert(&n, 3, handle);
    llist_travel(printf_s, NULL, handle);
    printf("\n");

    printf("delete num : ");
    scanf("%d", &n);
    llist_delete(&n, cmp, handle);
    llist_travel(printf_s, NULL, handle);
    printf("\n");

	printf("find num : ");
	scanf("%d", &n);
	ret1 = llist_find(&n, cmp, handle);
    if(ret1 != NULL)
        printf("find = %d\n", *(int*)ret1);
    printf("\n");

    printf("findall num : ");
    scanf("%d", &n);
    find = llist_findall(&n, cmp, handle);
    llist_travel(printf_s, find, handle);
    printf("\n");

    llist_sort(cmp, handle);
    printf("sort:");
    llist_travel(printf_s, NULL, handle);
	printf("\n");

    ret = llist_store("./db", handle);
    if(ret == 0)
        printf("存储成功!\n");

    llist_destroy(&handle);

    return 0;
}