'Computer 기타'에 해당되는 글 5건

  1. 2010.07.02 C# UnitTest by csUnit
  2. 2010.03.16 Pthread with Semaphore <posix>
  3. 2010.03.15 Posix Thread 예제 소스
  4. 2010.02.27 자바 Tar 묶음 파일 만들기
  5. 2010.02.27 자바 - 정규표현식 예제입니다.<혼자보기용>

C# UnitTest by csUnit

간단한 테스트 코드 예제 자동 생성된 코드에 class 선언에 public이 안붙는데 붙여주어야 csUnit에 나온다..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UNITcs
{
    public class myStack
    {
        private Stack stack;

        public myStack(){
            stack = new Stack();    
        }

        public void Push(T a)
        {
            stack.Push(a);            
        }


        public T Top()
        {
            return stack.Peek();
        }

        public T Pop()
        {

            return stack.Pop();
        }

        public void ex(int a)
        {

            int d = 3 / a;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using csUnit;

namespace UNITcs
{
    [TestFixture]
    
    public class StackTest
    {
        myStack s = null;

        [SetUp]
        public void init()
        {
            s = new myStack();

        }
        [Test]
        public void PushPopTest()
        {
            s.Push(10);
            s.Push(20);
            s.Push(30);
            s.Push(40);
            s.Push(50);


            int value = s.Top();

            Assert.Equals(50, value);
            Assert.Equals(50, s.Pop());
            Assert.Equals(40, s.Pop());
            Assert.Equals(30, s.Pop());

        }

        [Test]
        [ExpectedException (typeof(System.DivideByZeroException))]
        public void PushPopTest2()
        {

            Console.Write("12312312");

            s.ex(1);
            s.ex(0);
        }
    }
}

Pthread with Semaphore <posix>

아래 Mutex로 동기화 했던걸 Semaphore로 변형 시킨거 위에껀 이름있는 세마포어??? 어래껀 익명 세미포어
#include 
#include 
#include 
#include 
#include  //for   O_CREAT
#include 

using namespace std;
sem_t *mysem;

struct arg
{
    int start;
    int end;
    int time;
    string name;
};


void *cnt(void* a)
{
    struct arg* temp=(struct arg*)a;

    for(int i= temp->start; i < temp->end;i++)
    {
        sem_wait(mysem);
        cout << temp->name <<" : " << i << endl;
        sem_post(mysem);
        sleep(temp->time);
    }
    pthread_exit(NULL);
}
int main()
{
    pthread_t td1,td2,td3;
    struct arg ar1,ar2,ar3;
    void* state;
    ar1.start=0;
    ar1.end=20;
    ar1.name=string("thread1");
    ar1.time=1;

    ar2.start=-10;
    ar2.end=0;
    ar2.name=string("thread2");
    ar2.time=2;

    ar3.start=100;
    ar3.end=110;
    ar3.name=string("thread3");
    ar3.time=3;
    sem_unlink("mysem"); //세마포어 삭제
    if((mysem = sem_open("mysem", O_CREAT, 0777, 1)) == NULL)
// /dev/shm에 해당이름의 세마포어 파일로 생성됨.
// O_CREAT : 파일이 존재하면 따로 생성하지 않음 , 0777 퍼미션
    {
        perror("Sem Open Error");
        return 1;
    }
    pthread_create(&td1,NULL,cnt,(void*)&ar1);
    pthread_create(&td2,NULL,cnt,(void*)&ar2);
    pthread_create(&td3,NULL,cnt,(void*)&ar3);

    pthread_join(td1,&state);
    pthread_join(td2,&state);
    pthread_join(td3,&state);

    return 0;
}
#include 
#include 
#include 
#include 
#include 

using namespace std;
sem_t mysem;

struct arg
{
    int start;
    int end;
    int time;
    string name;
};


void *cnt(void* a)
{
    struct arg* temp=(struct arg*)a;

    for(int i= temp->start; i < temp->end;i++)
    {
        sem_wait(&mysem);
        cout << temp->name <<" : " << i << endl;
        sem_post(&mysem);
        sleep(temp->time);
    }
    pthread_exit(NULL);
}
int main()
{
    pthread_t td1,td2,td3;
    struct arg ar1,ar2,ar3;
    void* state;
    ar1.start=0;
    ar1.end=20;
    ar1.name=string("thread1");
    ar1.time=1;

    ar2.start=-10;
    ar2.end=0;
    ar2.name=string("thread2");
    ar2.time=2;

    ar3.start=100;
    ar3.end=110;
    ar3.name=string("thread3");
    ar3.time=3;
    
    if(sem_init(&mysem,0,1) ==-1)
    {
        cerr <<"Error";
        exit(0);
    }


    pthread_create(&td1,NULL,cnt,(void*)&ar1);
    pthread_create(&td2,NULL,cnt,(void*)&ar2);
    pthread_create(&td3,NULL,cnt,(void*)&ar3);

    pthread_join(td1,&state);
    pthread_join(td2,&state);
    pthread_join(td3,&state);

    return 0;
}

'Computer 기타' 카테고리의 다른 글

C# UnitTest by csUnit  (0) 2010.07.02
Posix Thread 예제 소스  (0) 2010.03.15
자바 Tar 묶음 파일 만들기  (0) 2010.02.27
자바 - 정규표현식 예제입니다.<혼자보기용>  (0) 2010.02.27

Posix Thread 예제 소스

PThread 간단하게 쓰레드 생성하고 간단한 뮤텍스까지 들어있는 소스 입니다.
#include 
#include 
#include 
#include 
#include 

using namespace std;
pthread_mutex_t mutex;

struct arg
{
    int start;
    int end;
    int time;
    string name;
};


void *cnt(void* a)
{
    struct arg* temp=(struct arg*)a;

    for(int i= temp->start; i < temp->end;i++)
    {
        pthread_mutex_lock(&mutex);
        cout << temp->name <<" : " << i << endl;
        pthread_mutex_unlock(&mutex);
        sleep(temp->time);
    }
    pthread_exit(NULL);
}
int main()
{
    pthread_t td1,td2,td3;
    struct arg ar1,ar2,ar3;
    void* state;
    ar1.start=0;
    ar1.end=20;
    ar1.name=string("thread1");
    ar1.time=1;

    ar2.start=-10;
    ar2.end=0;
    ar2.name=string("thread2");
    ar2.time=2;

    ar3.start=100;
    ar3.end=110;
    ar3.name=string("thread3");
    ar3.time=3;
    pthread_mutex_init(&mutex,NULL);
    pthread_create(&td1,NULL,cnt,(void*)&ar1);
    pthread_create(&td2,NULL,cnt,(void*)&ar2);
    pthread_create(&td3,NULL,cnt,(void*)&ar3);

    pthread_join(td1,&state);
    pthread_join(td2,&state);
    pthread_join(td3,&state);

    pthread_mutex_destroy(&mutex);

    return 0;
}

'Computer 기타' 카테고리의 다른 글

C# UnitTest by csUnit  (0) 2010.07.02
Pthread with Semaphore <posix>  (0) 2010.03.16
자바 Tar 묶음 파일 만들기  (0) 2010.02.27
자바 - 정규표현식 예제입니다.<혼자보기용>  (0) 2010.02.27

자바 Tar 묶음 파일 만들기

http://www.trustice.com/java/tar/ 에서 배포하는 tar 라이브러리를 이용하여간단하게 현재 폴더에 있는 jpg파일을 검색해서 Picture.tar로 묶는 프로그램을 만들어 보았습니다.
import com.ice.tar.*;
import java.io.*;
import java.util.regex.*;

public class Main {

    public static void main(String[] args) throws IOException{
        File f = new File("Picture.tar");
        int blockSize = TarBuffer.DEFAULT_BLKSIZE;
        TarArchive tout = new TarArchive(new FileOutputStream(f),blockSize);
        if(tout == null) System.exit(-1);
        TarEntry te=null;
        Pattern p = Pattern.compile("[a-zA-Z0-9_]+\\.jpg");
        File current = new File(".");
        File[] files = current.listFiles();
        for(File str: files)
        {
            Matcher m = p.matcher(str.getName());
            if(m.find())
            {
                te = new TarEntry(str);
                te.setName(str.getName());
              
                System.out.println(te.getName());
                tout.writeEntry(te, true);
            }
        }
        tout.closeArchive();
        System.out.println("묶음 완료");
    }
}

'Computer 기타' 카테고리의 다른 글

C# UnitTest by csUnit  (0) 2010.07.02
Pthread with Semaphore <posix>  (0) 2010.03.16
Posix Thread 예제 소스  (0) 2010.03.15
자바 - 정규표현식 예제입니다.<혼자보기용>  (0) 2010.02.27

자바 - 정규표현식 예제입니다.<혼자보기용>


main5.cpp 라는 파일에서 include한 파일과 주석을 추출하여 출력해주는 소스입니다
import java.util.regex.*;
import java.io.*;
import java.nio.charset.*;
import java.nio.*;
import java.nio.channels.*;


public class Main {

    public static void main(String[] args) throws IOException{
        File f = new File("main5.cpp");
        FileInputStream fis = new FileInputStream(f);
        FileChannel fc = fis.getChannel();


        ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
        Charset cs = Charset.forName("EUC-KR");
        CharsetDecoder cd = cs.newDecoder();
        CharBuffer cb = cd.decode(bb);
        Pattern p = Pattern.compile("#include <([a-zA-Z_]+/?[a-zA-Z_]*(\\.h|\\.hpp)?)>",Pattern.MULTILINE);
        Matcher m = p .matcher(cb);
        while(m.find())
        {
            System.out.println("You include : "+m.group(1));
        }

        p = Pattern.compile("///*.*",Pattern.MULTILINE);
        m = p .matcher(cb);
        while(m.find())
        {
            System.out.println("You comment : "+m.group());
        }
       //System.out.println(cb);

    }

}

'Computer 기타' 카테고리의 다른 글

C# UnitTest by csUnit  (0) 2010.07.02
Pthread with Semaphore <posix>  (0) 2010.03.16
Posix Thread 예제 소스  (0) 2010.03.15
자바 Tar 묶음 파일 만들기  (0) 2010.02.27
prev 1 next